From ae986e4566a1fa622693426eb4d381aa46900039 Mon Sep 17 00:00:00 2001 From: Stavros Polymenis Date: Thu, 7 Dec 2017 22:47:26 +0000 Subject: [PATCH] Move high-level conversion functions to Template for reuse in web server --- src/converters/template.ml | 26 ++++++++++++++++++-------- src/logarion_cli.ml | 14 +++++--------- 2 files changed, 23 insertions(+), 17 deletions(-) diff --git a/src/converters/template.ml b/src/converters/template.ml index cddc7de..c27089f 100644 --- a/src/converters/template.ml +++ b/src/converters/template.ml @@ -20,11 +20,11 @@ let escaped_index ~from ~n metas e = [Html.data "temp"] (* in *) (* Logarion.Meta.StringSet.fold (fun e a -> a ^ "
  • " ^ e ^ "
  • ") topics "" *) -let header_fold template archive = +let header_custom template archive = Mustache.fold ~string ~section ~escaped:(Html.Renderer.archive archive) ~unescaped ~partial ~comment ~concat template |> Html.header -let header archive = +let header_default archive = Html.(header [title [anchor "index.html" [data archive.Logarion.Archive.Configuration.title]]]) let meta meta = @@ -39,11 +39,11 @@ let meta meta = let uuid = Id.to_string meta.uuid in Html.meta ~abstract ~author ~date ~series ~topics ~keywords ~uuid -let body_fold template note = +let body_custom template note = Mustache.fold ~string ~section ~escaped:(Html.Renderer.note note) ~unescaped ~partial ~comment ~concat template |> Html.note -let body note = +let body_default note = Html.note [ Html.title [Html.unescaped_data note.Logarion.Note.meta.Logarion.Meta.title]; (* Don't add title if body contains one *) meta note.meta; @@ -52,14 +52,24 @@ let body note = let listing metas = Html.listing metas let page ~style title header body = Html.to_string @@ Html.page ~style title header body - + let of_config config k = match config with | Error msg -> prerr_endline "Couldn't load [templates] section"; None | Ok c -> let open Confix.ConfixToml in path c ("templates" / k) -let template_fn default template = function - | Some p -> if Confix.Config.Path.path_exists p then template @@ of_file p - else (prerr_endline @@ "Couldn't find: " ^ Fpath.to_string p; default) +let converter default custom = function + | Some p -> + if Confix.Config.Path.path_exists p then custom @@ of_file p + else (prerr_endline @@ "Couldn't find: " ^ Fpath.to_string p; default) | None -> default + +let header_converter config = converter header_default header_custom @@ of_config config "header" +let body_converter config = converter body_default body_custom @@ of_config config "body" + +let page_of_listing header listing archive metas = + page ~style:"static/main.css" "Index" (header archive) (listing metas) + +let page_of_note header body archive note = + page ~style:"static/main.css" note.Logarion.Note.meta.Logarion.Meta.title (header archive) (body note) diff --git a/src/logarion_cli.ml b/src/logarion_cli.ml index 1ddf375..d139288 100644 --- a/src/logarion_cli.ml +++ b/src/logarion_cli.ml @@ -124,15 +124,11 @@ let convert directory = let metas = File.to_list L.meta_lens archive.store in let template_config = toml_config in - let header = Converters.Template.(template_fn header header_fold @@ of_config template_config "header") in - let body = Converters.Template.(template_fn body body_fold @@ of_config template_config "body") in - let page_of_note note = - let title = Note.(note.meta.Meta.title) in - Converters.Template.page ~style:"static/main.css" title (header config) (body note) - in - let page_of_listing metas = - Converters.Template.(page ~style:"static/main.css" "Index" (header config) (listing metas)) - in + let module T = Converters.Template in + let header = T.header_converter template_config in + let body = T.body_converter template_config in + let page_of_listing metas = T.page_of_listing header T.listing config metas in + let page_of_note note = T.page_of_note header body config note in let path_of_note note = directory ^ "/" ^ Meta.string_alias Note.(note.meta.Meta.title) ^ ".html" in let file_creation path content = let out = open_out path in