Move high-level conversion functions to Template for reuse in web server
This commit is contained in:
parent
2d260a37de
commit
ae986e4566
@ -20,11 +20,11 @@ let escaped_index ~from ~n metas e = [Html.data "temp"]
|
|||||||
(* in *)
|
(* in *)
|
||||||
(* Logarion.Meta.StringSet.fold (fun e a -> a ^ "<li><a href=\"/topic/" ^ e ^ "\">" ^ e ^ "</a></li>") topics "" *)
|
(* 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
|
Mustache.fold ~string ~section ~escaped:(Html.Renderer.archive archive) ~unescaped ~partial ~comment ~concat template
|
||||||
|> Html.header
|
|> Html.header
|
||||||
|
|
||||||
let header archive =
|
let header_default archive =
|
||||||
Html.(header [title [anchor "index.html" [data archive.Logarion.Archive.Configuration.title]]])
|
Html.(header [title [anchor "index.html" [data archive.Logarion.Archive.Configuration.title]]])
|
||||||
|
|
||||||
let meta meta =
|
let meta meta =
|
||||||
@ -39,11 +39,11 @@ let meta meta =
|
|||||||
let uuid = Id.to_string meta.uuid in
|
let uuid = Id.to_string meta.uuid in
|
||||||
Html.meta ~abstract ~author ~date ~series ~topics ~keywords ~uuid
|
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
|
Mustache.fold ~string ~section ~escaped:(Html.Renderer.note note) ~unescaped ~partial ~comment ~concat template
|
||||||
|> Html.note
|
|> Html.note
|
||||||
|
|
||||||
let body note =
|
let body_default note =
|
||||||
Html.note
|
Html.note
|
||||||
[ Html.title [Html.unescaped_data note.Logarion.Note.meta.Logarion.Meta.title]; (* Don't add title if body contains one *)
|
[ Html.title [Html.unescaped_data note.Logarion.Note.meta.Logarion.Meta.title]; (* Don't add title if body contains one *)
|
||||||
meta note.meta;
|
meta note.meta;
|
||||||
@ -52,14 +52,24 @@ let body note =
|
|||||||
let listing metas = Html.listing metas
|
let listing metas = Html.listing metas
|
||||||
|
|
||||||
let page ~style title header body = Html.to_string @@ Html.page ~style title header body
|
let page ~style title header body = Html.to_string @@ Html.page ~style title header body
|
||||||
|
|
||||||
let of_config config k = match config with
|
let of_config config k = match config with
|
||||||
| Error msg -> prerr_endline "Couldn't load [templates] section"; None
|
| Error msg -> prerr_endline "Couldn't load [templates] section"; None
|
||||||
| Ok c ->
|
| Ok c ->
|
||||||
let open Confix.ConfixToml in
|
let open Confix.ConfixToml in
|
||||||
path c ("templates" / k)
|
path c ("templates" / k)
|
||||||
|
|
||||||
let template_fn default template = function
|
let converter default custom = function
|
||||||
| Some p -> if Confix.Config.Path.path_exists p then template @@ of_file p
|
| Some p ->
|
||||||
else (prerr_endline @@ "Couldn't find: " ^ Fpath.to_string p; default)
|
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
|
| 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)
|
||||||
|
@ -124,15 +124,11 @@ let convert directory =
|
|||||||
let metas = File.to_list L.meta_lens archive.store in
|
let metas = File.to_list L.meta_lens archive.store in
|
||||||
|
|
||||||
let template_config = toml_config in
|
let template_config = toml_config in
|
||||||
let header = Converters.Template.(template_fn header header_fold @@ of_config template_config "header") in
|
let module T = Converters.Template in
|
||||||
let body = Converters.Template.(template_fn body body_fold @@ of_config template_config "body") in
|
let header = T.header_converter template_config in
|
||||||
let page_of_note note =
|
let body = T.body_converter template_config in
|
||||||
let title = Note.(note.meta.Meta.title) in
|
let page_of_listing metas = T.page_of_listing header T.listing config metas in
|
||||||
Converters.Template.page ~style:"static/main.css" title (header config) (body note)
|
let page_of_note note = T.page_of_note header body config note in
|
||||||
in
|
|
||||||
let page_of_listing metas =
|
|
||||||
Converters.Template.(page ~style:"static/main.css" "Index" (header config) (listing metas))
|
|
||||||
in
|
|
||||||
let path_of_note note = directory ^ "/" ^ Meta.string_alias Note.(note.meta.Meta.title) ^ ".html" in
|
let path_of_note note = directory ^ "/" ^ Meta.string_alias Note.(note.meta.Meta.title) ^ ".html" in
|
||||||
let file_creation path content =
|
let file_creation path content =
|
||||||
let out = open_out path in
|
let out = open_out path in
|
||||||
|
Loading…
x
Reference in New Issue
Block a user