Move high-level conversion functions to Template for reuse in web server

This commit is contained in:
Stavros Polymenis 2017-12-07 22:47:26 +00:00
parent 2d260a37de
commit ae986e4566
2 changed files with 23 additions and 17 deletions

View File

@ -20,11 +20,11 @@ let escaped_index ~from ~n metas e = [Html.data "temp"]
(* in *)
(* Logarion.Meta.StringSet.fold (fun e a -> a ^ "<li><a href=\"/topic/" ^ e ^ "\">" ^ e ^ "</a></li>") 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;
@ -59,7 +59,17 @@ let of_config config k = match config with
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
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)

View File

@ -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