diff --git a/src/html.ml b/src/html.ml index e7992ea..c7a35e5 100644 --- a/src/html.ml +++ b/src/html.ml @@ -1,5 +1,7 @@ open Tyxml.Html +let to_string tyxml = Format.asprintf "%a" (Tyxml.Html.pp ()) tyxml + let logarion_head ?(style="/style.css") t = head (title (pcdata t)) [ link ~rel:[`Stylesheet] ~href:"/style.css" (); @@ -20,20 +22,25 @@ let of_ymd ymd = Unsafe.data ymd_body; footer [p []]; ]) - |> Format.asprintf "%a" (Tyxml.Html.pp ()) + |> to_string -let of_file_meta_pairs file_metas = - let link_item (y,m) = li [a ~a:[a_href (uri_of_string ("/text/" ^ Filename.chop_extension y))] [Unsafe.data Ymd.(m.title)]] in +let article_link (file, meta) = + li [a ~a:[a_href (uri_of_string ("/text/" ^ Filename.chop_extension file))] + [Unsafe.data Ymd.(meta.title)] + ] + +let of_file_meta_pairs ?listing_tpl:(tpl=None) file_meta_pairs = html (logarion_head "Homepage") (body [ header [ h1 [pcdata "Homepage"] ]; div [ h2 [pcdata "Articles"]; - ul (List.map link_item file_metas); - pcdata Template.(of_string "test {{test}}" |> fold); + match tpl with + | Some s -> Unsafe.data Template.(of_string s |> fold_index file_meta_pairs) + | None -> ul (List.map article_link file_meta_pairs); ]; ]) - |> Format.asprintf "%a" (Tyxml.Html.pp ()) + |> to_string let form ymd = let input_set title name value = @@ -42,8 +49,7 @@ let form ymd = input ~a:[a_name name; a_value value] () ]] in - let open Ymd in - html (logarion_head "Compose") + Ymd.(html (logarion_head "Compose") (body [ header [ h1 [pcdata "Article composition"] ]; div [ @@ -71,5 +77,5 @@ let form ymd = ] ] ]; - ]) - |> Format.asprintf "%a" (Tyxml.Html.pp ()) + ])) + |> to_string diff --git a/src/template.ml b/src/template.ml index c0698c0..8879f95 100644 --- a/src/template.ml +++ b/src/template.ml @@ -1,11 +1,4 @@ - -let string s = prerr_endline ("string:"^s); s -let section ~inverted name contents = "section" -let escaped e = prerr_endline ("escaped:"^e); e -let unescaped u = u -let partial p = p -let comment c = c -let concat l = String.concat "," l +open Ymd let of_string = Mustache.of_string @@ -14,4 +7,43 @@ let of_file f = Lwt_io.(open_file ~mode:(Input) f >|= read_lines) >|= (fun stream -> Lwt_stream.fold (^) stream "") -let fold = Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat +let string s = s +let section ~inverted name contents = "section" +let unescaped u = u +let partial p = p +let comment c = c +let concat l = String.concat "" l + +let fold_text ymd = + let escaped e = match e with + | "title" -> meta.title + | "abstract" -> meta.abstract + | "authors" -> meta.authors + | "text" -> + | _ -> prerr_endline ("unknown tag: " ^ e); "" in + Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat + +let fold_entry (file, meta) = + let escaped e = match e with + | "title" -> meta.title + | "abstract" -> meta.abstract + | "authors" -> meta.authors + | _ -> prerr_endline ("unknown tag: " ^ e); "" in + Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat + +let fold_index ymd_meta_pairs = + let string s = s in + let section ~inverted name contents = "section" in + let escaped e = match e with + | "recent_texts_listing" -> + (ListLabels.fold_left + ~init:("