source: code/trunk/src/converters/template.ml@ 1

Last change on this file since 1 was 1, checked in by fox, 9 years ago

initial simple example with omd

File size: 3.5 KB
Line 
1type t = Mustache.t
2
3let of_string = Mustache.of_string
4let of_file f = File.load f |> of_string
5
6let string s = [Html.data s]
7let section ~inverted:_ _name _contents = prerr_endline "Mustache sections unsupported"; []
8let unescaped _elts = prerr_endline "Mustache unescaped not supported; used escaped instead"; []
9let partial ?indent:_ _name _ _ = prerr_endline "Mustache sections unsupported"; []
10let comment _ = [Html.data ""]
11let concat = List.concat
12
13let escaped_index ~from:_ ~n:_ _metas _e = [Html.data "temp"]
14 (* match List.hd e with *)
15 (* | "topics" -> *)
16 (* let topics = *)
17 (* ListLabels.fold_left *)
18 (* ~init:(Logarion.Meta.StringSet.empty) *)
19 (* ~f:(fun a e -> Logarion.Meta.unique_topics a e ) metas *)
20 (* in *)
21 (* Logarion.Meta.StringSet.fold (fun e a -> a ^ "<li><a href=\"/topic/" ^ e ^ "\">" ^ e ^ "</a></li>") topics "" *)
22
23let header_custom template _linker archive =
24 Mustache.fold ~string ~section ~escaped:(Html.Renderer.archive archive) ~unescaped ~partial ~comment ~concat template
25 |> Html.header
26
27let header_default linker archive =
28 Html.(header [title [anchor (linker "/") [data archive.Logarion.Archive.Configuration.title]]])
29
30let meta meta =
31 let open Logarion.Meta in
32 let abstract = meta.abstract in
33 let authors = List.map (fun elt -> elt.Author.name) @@ AuthorSet.elements meta.authors in
34 let date = Date.(pretty_date @@ listing meta.date) in
35 let series = stringset_csv meta.series in
36 let topics = stringset_csv meta.topics in
37 let keywords = stringset_csv meta.keywords in
38 let uuid = Id.to_string meta.uuid in
39 Html.meta ~abstract ~authors ~date ~series ~topics ~keywords ~uuid
40
41let body_custom template note =
42 Mustache.fold ~string ~section ~escaped:(Html.Renderer.note note) ~unescaped ~partial ~comment ~concat template
43 |> Html.note
44
45let body_default note =
46 Html.note
47 [ Html.title [Html.unescaped_data note.Logarion.Note.meta.Logarion.Meta.title]; (* Don't add title if body contains one *)
48 meta note.meta;
49 Html.unescaped_data @@ Omd.to_html @@ Omd.of_string note.Logarion.Note.body ]
50
51let page ~style linker title header body =
52 Html.to_string @@ Html.page ~style linker title header body
53
54let of_config config k = match config with
55 | Error msg -> prerr_endline ("Couldn't load [templates] section;" ^ msg); None
56 | Ok c ->
57 let open Confix.ConfixToml in
58 path c ("templates" / k)
59
60let converter default custom = function
61 | Some p ->
62 if Confix.Config.Path.path_exists p then custom @@ of_file p
63 else (prerr_endline @@ "Couldn't find: " ^ Fpath.to_string p; default)
64 | None -> default
65
66let header_converter config = converter header_default header_custom @@ of_config config "header"
67let body_converter config = converter body_default body_custom @@ of_config config "body"
68
69let default_style = Html.default_style
70
71let page_of_index ~style linker header archive metas =
72 page ~style linker ("Index | " ^ archive.Logarion.Archive.Configuration.title) (header linker archive) (Html.main (Html.listing_index "" metas))
73
74let page_of_log ~style linker header archive metas =
75 page ~style linker ("Log | " ^ archive.Logarion.Archive.Configuration.title) (header linker archive) (Html.main [Html.listing_texts "" metas])
76
77let page_of_note ~style linker header body archive note =
78 page ~style linker note.Logarion.Note.meta.Logarion.Meta.title (header linker archive) (body note)
79
80let page_of_msg ~style linker header archive title msg =
81 page ~style linker title (header linker archive) (Html.div [Html.data msg])
Note: See TracBrowser for help on using the repository browser.