Reorganise Template and HTML

This commit is contained in:
Stavros Polymenis 2017-12-04 00:09:29 +00:00
parent 381d4a235f
commit 63d915c8be
3 changed files with 92 additions and 112 deletions

View File

@ -12,17 +12,21 @@ let head ~style t =
let default_style = "/static/main.css"
let page ?(style=default_style) blog_url head_title header main =
let page ?(style=default_style) head_title header main =
html (head ~style head_title) (body [ header; main ])
let heading1 = h1
let anchor url content = a ~a:[ a_href (uri_of_string url) ] content
let div ?(style_class="") content =
let a = if style_class <> "" then [a_class [style_class]] else [] in
div ~a content
let unescaped_data = Unsafe.data
let data = pcdata
let title = h1
let header = header
let article = article
let note ~abstract ~author ~date ~series ~topics ~keywords ~uuid ~body =
article [
let meta ~abstract ~author ~date ~series ~topics ~keywords ~uuid =
details
(summary [Unsafe.data abstract])
[
@ -34,22 +38,42 @@ let note ~abstract ~author ~date ~series ~topics ~keywords ~uuid ~body =
div [pcdata ("Topics: " ^ topics)];
div [pcdata ("Keywords: " ^ keywords)];
div [pcdata ("UUID: " ^ uuid)];
];
Unsafe.data body;
]
let anchor url content = a ~a:[ a_href (uri_of_string url) ] content
let note = article
let div ?(style_class="") content =
let a = if style_class <> "" then [a_class [style_class]] else [] in
div ~a content
let listing metas =
let open Html in
let item meta =
let module Meta = Logarion.Meta in
li [
anchor
(Meta.alias meta ^ ".html")
[
div ~style_class:"title" [data meta.Meta.title];
div ~style_class:"time" [data Meta.Date.(pretty_date (last meta.Meta.date))]
]
]
in
ul @@ List.map item metas
let list_unordered = ul
module Renderer = struct
let meta meta e =
let e = List.hd e in
match e with
| "urn_name" -> [unescaped_data @@ "/note/" ^ Logarion.Meta.alias meta]
| "date" | "date_created" | "date_edited" | "date_published" | "date_human" ->
[time @@ [unescaped_data @@ Logarion.Meta.value_with_name meta e]]
| tag -> [unescaped_data @@ Logarion.Meta.value_with_name meta tag]
let list_item content = li [ content ]
let note note e = match List.hd e with
| "body" -> [unescaped_data @@ Omd.to_html @@ Omd.of_string note.Logarion.Note.body]
| _ -> meta note.Logarion.Note.meta e
let unescaped_data = Unsafe.data
let data = pcdata
let archive archive e = match List.hd e with
| "title" -> [h1 [anchor ("index.html") [data archive.Logarion.Archive.Configuration.title]]]
| tag -> prerr_endline ("unknown tag: " ^ tag); [unescaped_data ""]
end
let form blog_url lgrn ymd =
let article_form =

View File

@ -41,12 +41,6 @@ let map_tpl_opt f field ps =
| Some dir, Some bn -> Some (f (of_file (Fpath.append dir bn)))
| _ -> None
let header ps = map_tpl_opt (fun v -> Header v) ps.Configuration.header ps
let note ps = map_tpl_opt (fun v -> Note v) ps.Configuration.note ps
let front ps = map_tpl_opt (fun v -> Front v) ps.Configuration.front ps
let list ps = map_tpl_opt (fun v -> List v) ps.Configuration.list ps
let item ps = map_tpl_opt (fun v -> Item v) ps.Configuration.item ps
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"]
@ -54,42 +48,8 @@ let partial ?indent name _ _ = [Html.data "partials not supported"]
let comment _ = [Html.data ""]
let concat = List.concat
let escaped_meta (meta : Logarion.Meta.t) e =
let open Logarion in
let e = List.hd e in
match e with
| "url" -> "/note/" ^ Meta.alias meta
| "date" | "date_created" | "date_edited" | "date_published" | "date_human" ->
"<time>" ^ Meta.value_with_name meta e ^ "</time>"
| tag -> Meta.value_with_name meta tag
let escaped_note note e = match List.hd e with
| "body" -> [Html.unescaped_data @@ Omd.to_html @@ Omd.of_string note.Logarion.Note.body]
| _ -> [Html.unescaped_data @@ escaped_meta note.Logarion.Note.meta e]
let escaped_header archive e = match List.hd e with
| "title" -> [Html.heading1 [Html.anchor ("index.html") [Html.data archive.Logarion.Archive.Configuration.title]]]
| tag -> prerr_endline ("unknown tag: " ^ tag); [Html.unescaped_data ""]
let anchor_of_meta meta =
let module Meta = Logarion.Meta in
let open Html in
anchor
(Meta.alias meta ^ ".html")
[ div ~style_class:"title" [data meta.Meta.title];
div ~style_class:"time" [data Meta.Date.(pretty_date (last meta.Meta.date))] ]
let listing metas =
let open Html in
list_unordered @@ List.map (fun m -> list_item @@ anchor_of_meta @@ m) metas
let escaped_index ~from ~n metas e = [Html.data "temp"]
(* match List.hd e with *)
(* | "navigation" -> *)
(* "" *)
(* ^ (if from > 0 then ("<a href=\"?p=" ^ string_of_int (pred from) ^ "\">previous</a> | ") else "") *)
(* ^ (if n <= List.length metas then ("<a href=\"?p=" ^ string_of_int (succ from) ^ "\">next</a>") else "") *)
(* | "recent_texts_listing" -> (\* listing metas *\) "" *)
(* | "topics" -> *)
(* let topics = *)
(* ListLabels.fold_left *)
@ -97,39 +57,34 @@ let escaped_index ~from ~n metas e = [Html.data "temp"]
(* ~f:(fun a e -> Logarion.Meta.unique_topics a e ) metas *)
(* in *)
(* Logarion.Meta.StringSet.fold (fun e a -> a ^ "<li><a href=\"/topic/" ^ e ^ "\">" ^ e ^ "</a></li>") topics "" *)
(* | e -> prerr_endline ("unknown tag: " ^ e); "" *)
let header_html template archive = match template with
| Some (Header s) ->
Mustache.fold ~string ~section ~escaped:(escaped_header archive) ~unescaped ~partial ~comment ~concat s
|> (fun x -> Html.header x)
| None -> Html.(header [heading1 [title]])
let header_fold template archive =
Mustache.fold ~string ~section ~escaped:(Html.Renderer.archive archive) ~unescaped ~partial ~comment ~concat template
let note_page ?(header_template=None) ?(note_template=None) ~style url title note =
let note_html = match note_template with
| Some (Note s) ->
Mustache.fold ~string ~section ~escaped:(escaped_note note) ~unescaped ~partial ~comment ~concat s
|> Html.article
| None ->
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
let abstract = note.meta.abstract in
let author = note.meta.author.name in
let date = Date.(pretty_date @@ last note.meta.date) in
let series = stringset_csv note.meta.series in
let topics = stringset_csv note.meta.topics in
let keywords = stringset_csv note.meta.keywords in
let uuid = Id.to_string note.meta.uuid in
let body = Omd.to_html (Omd.of_string note.body) in
Html.note ~abstract ~author ~date ~series ~topics ~keywords ~uuid ~body
in
Html.to_string @@ Html.page ~style url title (header_html header_template url title) note_html
let abstract = meta.abstract in
let author = meta.author.name in
let date = Date.(pretty_date @@ last meta.date) in
let series = stringset_csv meta.series in
let topics = stringset_csv meta.topics in
let keywords = stringset_csv meta.keywords in
let uuid = Id.to_string meta.uuid in
Html.meta ~abstract ~author ~date ~series ~topics ~keywords ~uuid
let listing_page ?(header_template=None) ?(listing_template=None) ~style ~from ~n url title metas =
let listing_html = match listing_template with
| Some (Note s) ->
Mustache.fold ~string ~section ~escaped:(escaped_index ~from ~n metas) ~unescaped ~partial ~comment ~concat s
|> Html.article
| None -> Html.article [listing metas]
in
Html.to_string @@ Html.page ~style url title (header_html header_template url title) listing_html
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 *)
meta note.meta;
Html.unescaped_data @@ Omd.to_html @@ Omd.of_string note.Logarion.Note.body ]
let listing metas = Html.listing metas
let page ~style title header body = Html.to_string @@ Html.page ~style title header body

View File

@ -123,12 +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 page_of_note page =
let title = Note.(page.meta.Meta.title) in
Converters.Template.note_page ~style:"static/main.css" "localhost" title page
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_note_listing =
Converters.Template.listing_page ~style:"static/main.css" ~from:0 ~n:1000 "localhost" "Index"
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 file_creation path content =
@ -137,13 +137,14 @@ let convert directory =
close_out out
in
match create_dir directory |> create_dir_msg ~descr:"export" directory with
| Ok _ ->
(match copy ~recursive:true ".logarion/static" (directory) with
| Ok _ ->
List.iter (fun note -> file_creation (path_of_note note) (page_of_note note)) notes;
file_creation (directory ^ "/index.html") (page_of_note_listing metas);
| Error (`Msg m) -> prerr_endline m)
| Error _ -> ()
| Ok _ ->
match copy ~recursive:true ".logarion/static" (directory) with
| Ok _ ->
let note_write note = file_creation (path_of_note note) (page_of_note note) in
List.iter note_write notes;
file_creation (directory ^ "/index.html") (page_of_listing metas);
| Error (`Msg m) -> prerr_endline m
let convert_term =
let directory =