From 233a5ffc32f4f6c4772b58f6b2766aa36554bc9f Mon Sep 17 00:00:00 2001 From: Stavros Polymenis Date: Sun, 9 Oct 2016 15:21:53 +0100 Subject: [PATCH] new modules for templates --- src/file.ml | 4 ++++ src/html.ml | 15 ++------------- src/template.ml | 17 +++++++++++++++++ 3 files changed, 23 insertions(+), 13 deletions(-) create mode 100644 src/file.ml create mode 100644 src/template.ml diff --git a/src/file.ml b/src/file.ml new file mode 100644 index 0000000..5852220 --- /dev/null +++ b/src/file.ml @@ -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 "") diff --git a/src/html.ml b/src/html.ml index c3c99f2..e7992ea 100644 --- a/src/html.ml +++ b/src/html.ml @@ -5,7 +5,7 @@ let logarion_head ?(style="/style.css") t = link ~rel:[`Stylesheet] ~href:"/style.css" (); meta ~a:[a_charset "utf-8"] (); ] - + let of_ymd ymd = let ymd_title = Ymd.(ymd.meta.title) 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 ()) 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 html (logarion_head "Homepage") (body [ @@ -41,7 +30,7 @@ let of_file_meta_pairs file_metas = div [ h2 [pcdata "Articles"]; ul (List.map link_item file_metas); - pcdata mustache_list; + pcdata Template.(of_string "test {{test}}" |> fold); ]; ]) |> Format.asprintf "%a" (Tyxml.Html.pp ()) diff --git a/src/template.ml b/src/template.ml new file mode 100644 index 0000000..c0698c0 --- /dev/null +++ b/src/template.ml @@ -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