refactoring and introduction of logarion parameter

This commit is contained in:
Stavros Polymenis 2016-10-17 22:38:48 +01:00
parent b9a1ea7f95
commit 5c3e43dd54
3 changed files with 41 additions and 29 deletions

View File

@ -2,29 +2,32 @@ open Tyxml.Html
let to_string tyxml = Format.asprintf "%a" (Tyxml.Html.pp ()) tyxml let to_string tyxml = Format.asprintf "%a" (Tyxml.Html.pp ()) tyxml
let logarion_head ?(style="/style.css") t = let head ?(style="/style.css") t =
head (title (pcdata t)) [ head (title (pcdata 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 ?text_tpl:(tpl=None) ymd = let logarion_header ?header_tpl:(tpl=None) title =
match tpl with
| Some s -> Unsafe.data Template.(of_string s |> fold_header title)
| None -> header [ h1 [ pcdata title] ]
let logarion_text ?text_tpl:(tpl=None) ymd =
match tpl with
| Some s -> Unsafe.data Template.(of_string s |> fold_text ymd)
| None ->
let ymd_body = Omd.to_html (Omd.of_string Ymd.(ymd.body)) in
details
(summary [Unsafe.data Ymd.(ymd.meta.abstract)])
[time ~a:[a_datetime (Ymd.(rfc_string_of ymd.meta.date.published))] []];
Unsafe.data ymd_body;
footer [p []]
let of_ymd ?text_tpl:(tpl=None) lgrn ymd =
let ymd_title = Ymd.(ymd.meta.title) in let ymd_title = Ymd.(ymd.meta.title) in
html (logarion_head ymd_title) html (head (ymd_title ^ " by " ^ ymd.meta.author.name))
(body [ (body [logarion_header ymd_title; logarion_text ~text_tpl:tpl ymd ])
match tpl with
| Some s -> Unsafe.data Template.(of_string s |> fold_text ymd)
| None ->
let ymd_body = Omd.to_html (Omd.of_string Ymd.(ymd.body)) in
header [
h1 [Unsafe.data ymd_title];
details
(summary [Unsafe.data Ymd.(ymd.meta.abstract)])
[time ~a:[a_datetime (Ymd.(rfc_string_of ymd.meta.date.published))] []];
];
Unsafe.data ymd_body;
footer [p []];
])
|> to_string |> to_string
let article_link (file, meta) = let article_link (file, meta) =
@ -32,10 +35,11 @@ let article_link (file, meta) =
[Unsafe.data Ymd.(meta.title)] [Unsafe.data Ymd.(meta.title)]
] ]
let of_file_meta_pairs ?listing_tpl:(tpl=None) file_meta_pairs = let of_file_meta_pairs ?listing_tpl:(tpl=None) lgrn file_meta_pairs =
html (logarion_head "Homepage") let t = Logarion.(lgrn.title) in
html (head t)
(body [ (body [
header [ h1 [pcdata "Homepage"] ]; logarion_header t;
div [ div [
h2 [pcdata "Articles"]; h2 [pcdata "Articles"];
match tpl with match tpl with
@ -45,16 +49,16 @@ let of_file_meta_pairs ?listing_tpl:(tpl=None) file_meta_pairs =
]) ])
|> to_string |> to_string
let form ymd = let form lgrn ymd =
let input_set title name value = let input_set title name value =
p [ label [ p [ label [
span [pcdata title]; span [pcdata title];
input ~a:[a_name name; a_value value] () input ~a:[a_name name; a_value value] ()
]] ]]
in in
Ymd.(html (logarion_head "Compose") Ymd.(html (head "Compose")
(body [ (body [
header [ h1 [pcdata "Article composition"] ]; logarion_header "Article composition";
div [ div [
form form
~a:[a_method `Post; a_action (uri_of_string "/post"); a_accept_charset ["utf-8"];] ~a:[a_method `Post; a_action (uri_of_string "/post"); a_accept_charset ["utf-8"];]

View File

@ -41,6 +41,12 @@ let fold_entry (file, meta) =
| _ -> prerr_endline ("unknown tag: " ^ e); "" in | _ -> prerr_endline ("unknown tag: " ^ e); "" in
Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat
let fold_header title =
let escaped e = match e with
| "title" -> title
| _ -> prerr_endline ("unknown tag: " ^ e); "" in
Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat
let fold_index ymd_meta_pairs = let fold_index ymd_meta_pairs =
let string s = s in let string s = s in
let section ~inverted name contents = "section" in let section ~inverted name contents = "section" in

View File

@ -16,6 +16,8 @@ let html_response h = `Html h |> respond'
let ymd_or_error y = match y with Some (path, meta) -> Logarion.of_file ("ymd/" ^ path) | None -> Ymd.blank_ymd let ymd_or_error y = match y with Some (path, meta) -> Logarion.of_file ("ymd/" ^ path) | None -> Ymd.blank_ymd
let lgrn = Logarion.({ title = "Test" })
let () = let () =
let (>>=) = Lwt.(>>=) let (>>=) = Lwt.(>>=)
and (>|=) = Lwt.(>|=) in and (>|=) = Lwt.(>|=) in
@ -25,11 +27,11 @@ let () =
let listing_tpl = Some (Logarion.load_file "share/index.mustache") in let listing_tpl = Some (Logarion.load_file "share/index.mustache") in
let text_tpl = Some (Logarion.load_file "share/text.mustache") in let text_tpl = Some (Logarion.load_file "share/text.mustache") in
App.empty App.empty
|> post "/post" (fun req -> ymd_of_req req >>= fun ymd -> L.to_file ymd >>= fun () -> html_response (Html.of_ymd ymd)) |> post "/post" (fun req -> ymd_of_req req >>= fun ymd -> L.to_file ymd >>= fun () -> html_response (Html.of_ymd lgrn ymd))
|> get "/edit/:ttl" (fun r -> ret_param "ttl" r >>= ymdpath >|= ymd >|= Html.form >>= html_response) |> get "/edit/:ttl" (fun r -> ret_param "ttl" r >>= ymdpath >|= ymd >|= Html.form lgrn >>= html_response)
|> get "/new" (fun _ -> return Ymd.blank_ymd >|= Html.form >>= html_response) |> get "/new" (fun _ -> return Ymd.blank_ymd >|= Html.form lgrn >>= html_response)
|> get "/text/:ttl" (fun req -> ret_param "ttl" req >>= ymdpath >|= ymd >|= Html.of_ymd ~text_tpl >>= html_response) |> get "/text/:ttl" (fun req -> ret_param "ttl" req >>= ymdpath >|= ymd >|= Html.of_ymd ~text_tpl lgrn >>= html_response)
|> get "/!/:ttl" (fun req -> ret_param "ttl" req >|= L.latest_file_meta_pair >|= ymd_or_error >|= Html.of_ymd >>= html_response) |> get "/!/:ttl" (fun req -> ret_param "ttl" req >|= L.latest_file_meta_pair >|= ymd_or_error >|= Html.of_ymd lgrn >>= html_response)
|> get "/style.css" (fun _ -> return "ymd/style.css" >|= L.load_file >>= string_response) |> get "/style.css" (fun _ -> return "ymd/style.css" >|= L.load_file >>= string_response)
|> get "/" (fun _ -> return (L.file_meta_pairs ()) >|= Html.of_file_meta_pairs ~listing_tpl >>= html_response) |> get "/" (fun _ -> return (L.file_meta_pairs ()) >|= Html.of_file_meta_pairs ~listing_tpl lgrn >>= html_response)
|> App.run_command |> App.run_command