Experimenting with new method for selectiong Template functions

This commit is contained in:
Stavros Polymenis 2017-12-04 19:03:45 +00:00
parent 63d915c8be
commit 7e445b3603
2 changed files with 20 additions and 42 deletions

View File

@ -1,46 +1,8 @@
type t = Mustache.t
module Configuration = struct
type path_t = Fpath.t option
type paths_t = {
dir : path_t;
header : path_t;
note : path_t;
front : path_t;
list : path_t;
item : path_t;
}
let of_config config =
let open Confix.ConfixToml in
let p k = path config ("templates" / k) in
{
dir = p "dir";
header = p "header";
note = p "note";
front = p "front";
list = p "list";
item = p "item";
}
end
type header_t = Header of t
type footer_t = Footer of t
type list_t = List of t
type item_t = Item of t
type note_t = Note of t
type front_t = Front of t
let of_string = Mustache.of_string
let of_file f = File.load f |> of_string
let map_tpl_opt f field ps =
let open Configuration in
match ps.dir, field with
| Some dir, Some bn -> Some (f (of_file (Fpath.append dir bn)))
| _ -> None
let string s = [Html.data s]
let section ~inverted name contents = [Html.unescaped_data ("section " ^ String.concat "." name)]
let unescaped elts = [Html.unescaped_data "use escaped instead"]
@ -60,13 +22,11 @@ let escaped_index ~from ~n metas e = [Html.data "temp"]
let header_fold template archive =
Mustache.fold ~string ~section ~escaped:(Html.Renderer.archive archive) ~unescaped ~partial ~comment ~concat template
|> Html.header
let header archive =
Html.(header [title [anchor "index.html" [data archive.Logarion.Archive.Configuration.title]]])
let note_fold template note =
Mustache.fold ~string ~section ~escaped:(Html.Renderer.note note) ~unescaped ~partial ~comment ~concat template
let meta meta =
let open Logarion.Note in
let open Logarion.Meta in
@ -79,6 +39,10 @@ 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 =
Mustache.fold ~string ~section ~escaped:(Html.Renderer.note note) ~unescaped ~partial ~comment ~concat template
|> Html.note
let body note =
Html.note
[ Html.title [Html.unescaped_data note.Logarion.Note.meta.Logarion.Meta.title]; (* Don't add title if body contains one *)
@ -88,3 +52,14 @@ 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)
| None -> default

View File

@ -123,9 +123,12 @@ let convert directory =
let notes = File.to_list L.note_lens archive.store in
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))
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))