new modules for templates

This commit is contained in:
Stavros Polymenis 2016-10-09 15:21:53 +01:00
parent 6bb0d654df
commit 233a5ffc32
3 changed files with 23 additions and 13 deletions

4
src/file.ml Normal file
View File

@ -0,0 +1,4 @@
let of_filename f =
let open Lwt in
Lwt_io.(open_file ~mode:(Input) f >|= read_lines)
>|= (fun stream -> Lwt_stream.fold (^) stream "")

View File

@ -5,7 +5,7 @@ let logarion_head ?(style="/style.css") t =
link ~rel:[`Stylesheet] ~href:"/style.css" (); link ~rel:[`Stylesheet] ~href:"/style.css" ();
meta ~a:[a_charset "utf-8"] (); meta ~a:[a_charset "utf-8"] ();
] ]
let of_ymd ymd = let of_ymd ymd =
let ymd_title = Ymd.(ymd.meta.title) in let ymd_title = Ymd.(ymd.meta.title) in
let ymd_body = Omd.to_html (Omd.of_string Ymd.(ymd.body)) in let ymd_body = Omd.to_html (Omd.of_string Ymd.(ymd.body)) in
@ -23,17 +23,6 @@ let of_ymd ymd =
|> Format.asprintf "%a" (Tyxml.Html.pp ()) |> Format.asprintf "%a" (Tyxml.Html.pp ())
let of_file_meta_pairs file_metas = let of_file_meta_pairs file_metas =
let mustache_list =
let string s = prerr_endline ("string:"^s); s
and section ~inverted name contents = "section"
and escaped e = prerr_endline ("escaped:"^e); e
and unescaped u = u
and partial p = p
and comment c = c
and concat l = String.concat "," l in
Mustache.of_string "Hello world {{test}}"
|> Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat
in
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 link_item (y,m) = li [a ~a:[a_href (uri_of_string ("/text/" ^ Filename.chop_extension y))] [Unsafe.data Ymd.(m.title)]] in
html (logarion_head "Homepage") html (logarion_head "Homepage")
(body [ (body [
@ -41,7 +30,7 @@ let of_file_meta_pairs file_metas =
div [ div [
h2 [pcdata "Articles"]; h2 [pcdata "Articles"];
ul (List.map link_item file_metas); ul (List.map link_item file_metas);
pcdata mustache_list; pcdata Template.(of_string "test {{test}}" |> fold);
]; ];
]) ])
|> Format.asprintf "%a" (Tyxml.Html.pp ()) |> Format.asprintf "%a" (Tyxml.Html.pp ())

17
src/template.ml Normal file
View File

@ -0,0 +1,17 @@
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
let of_string = Mustache.of_string
let of_file f =
let open Lwt in
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