From f5d90ac31ae29cd666afa4787fd81be37f51e470 Mon Sep 17 00:00:00 2001 From: Jon Sterling Date: Tue, 19 Aug 2025 15:19:41 +0100 Subject: [PATCH] remove duplicated code --- lib/compiler/Forest.ml | 92 ------------------------------------- lib/compiler/Forest.mli | 32 ------------- lib/frontend/Html_client.ml | 2 +- 3 files changed, 1 insertion(+), 125 deletions(-) diff --git a/lib/compiler/Forest.ml b/lib/compiler/Forest.ml index 291f995..81b9ff0 100644 --- a/lib/compiler/Forest.ml +++ b/lib/compiler/Forest.ml @@ -150,95 +150,3 @@ let analyse_resource graphs = function | T.Article article -> analyse_article graphs article | T.Asset asset -> analyse_asset graphs asset | _ -> () - -let get_article - : URI.t -> _ t -> T.content T.article option -= fun uri forest -> - match find_opt forest uri with - | None -> None - | Some (T.Asset _) -> - Logs.debug (fun m -> m "%a is an asset, not an article" URI.pp uri); - None - | Some (T.Syndication _) -> - Logs.debug (fun m -> m "%a is a syndication, not an article" URI.pp uri); - None - | Some (T.Article article) -> Some article - -let rec get_expanded_title ?scope ?(flags = T.{empty_when_untitled = false}) (frontmatter : _ T.frontmatter) forest = - let short_title = - match frontmatter.title with - | Some content -> content - | None when not flags.empty_when_untitled -> - begin - match frontmatter.uri with - | Some uri -> T.Content [T.Uri uri] - | _ -> T.Content [T.Text "Untitled"] - end - | _ -> T.Content [] - in - Option.value ~default: short_title @@ - match frontmatter.designated_parent with - | Some parent_uri when not (Option.equal URI.equal scope frontmatter.designated_parent) -> - let@ parent = Option.map @~ get_article parent_uri forest in - let parent_title = get_expanded_title parent.frontmatter forest in - let parent_link = T.Link {href = parent_uri; content = parent_title} in - let chevron = T.Text " › " in - T.map_content (fun xs -> parent_link :: chevron :: xs) short_title - | _ -> None - -let section_symbol = "§" - -let get_content_of_transclusion (transclusion : T.transclusion) forest = - match transclusion.target with - | Full flags -> - let@ article = Option.map @~ get_article transclusion.href forest in - T.Content [T.Section (T.article_to_section article ~flags)] - | Mainmatter -> - let@ article = Option.map @~ get_article transclusion.href forest in - article.mainmatter - | Title flags -> - Option.some @@ - begin - match get_article transclusion.href forest with - | None -> T.Content [T.Uri transclusion.href] - | Some article -> get_expanded_title ~flags article.frontmatter forest - end - | Taxon -> - let@ article = Option.map @~ get_article transclusion.href forest in - let default = T.Content [T.Text section_symbol] in - Option.value ~default article.frontmatter.taxon - -let get_title_or_content_of_vertex ?(not_found = fun _ -> None) vertex forest = - match vertex with - | T.Content_vertex content -> Some content - | T.Uri_vertex uri -> - begin - match get_article uri forest with - | Some article -> article.frontmatter.title - | None -> not_found uri - end - -let get_all_articles resources = - let extract_article = function - | T.Article a -> Some a - | _ -> None - in - resources - |> to_seq_values - |> Seq.filter_map extract_article - |> List.of_seq - -let get_all_assets resources = - let extract_asset = function - | T.Asset a -> Some a - | _ -> None - in - resources - |> to_seq_values - |> Seq.filter_map extract_asset - |> List.of_seq - -let get_all_resources resources = - resources - |> to_seq_values - |> List.of_seq diff --git a/lib/compiler/Forest.mli b/lib/compiler/Forest.mli index f733445..8d9d27e 100644 --- a/lib/compiler/Forest.mli +++ b/lib/compiler/Forest.mli @@ -25,39 +25,7 @@ val analyse_resource : (module Forest_graphs.S) -> T.content T.resource -> unit - When encountering a {{!Forester_core.Types.Datalog_script}[Datalog_script script]}, it runs the script and records the results in {{!Forester_core.Forest_graphs.S}[graphs]}. *) -val get_article : - key -> - T.content T.resource t -> - T.content T.article option - val run_datalog_query : (module Forest_graphs.S) -> (string, Forester_core.Vertex.t) Dx.query -> Forester_core.Vertex_set.t - -val get_expanded_title : - ?scope: key -> - ?flags: T.title_flags -> - T.content T.frontmatter -> - T.content T.resource t -> - T.content - -val get_content_of_transclusion : - T.transclusion -> - T.content T.resource t -> - T.content option - -val get_title_or_content_of_vertex : - ?not_found: (key -> T.content option) -> - T.content T.vertex -> - T.content T.resource t -> - T.content option - -val get_all_articles : - 'a T.resource t -> 'a T.article list - -val get_all_assets : - 'a T.resource t -> T.asset list - -val get_all_resources : - 'a t -> 'a list diff --git a/lib/frontend/Html_client.ml b/lib/frontend/Html_client.ml index e37efd6..e1b6455 100644 --- a/lib/frontend/Html_client.ml +++ b/lib/frontend/Html_client.ml @@ -31,7 +31,7 @@ let route uri = URI.to_string uri let get_expanded_title frontmatter forest = let scope = Scope.read () in - Forest.get_expanded_title ?scope ~flags: T.{empty_when_untitled = true} frontmatter forest + State.get_expanded_title ?scope ~flags: T.{empty_when_untitled = true} frontmatter forest let render_xml_qname qname = let qname = Xmlns.normalise_qname qname in -- 2.51.2