support for configuring Logarion with Toml

This commit is contained in:
Stavros Polymenis 2016-10-18 23:37:08 +01:00
parent 0d36a25e1f
commit f7a482316f
4 changed files with 46 additions and 26 deletions

View File

@ -1,6 +1,6 @@
OCB_FLAGS = -use-ocamlfind -I src # -I lib
OCB = ocamlbuild $(OCB_FLAGS)
PKGS = opium.unix,omd,str,batteries,tyxml,lens,ptime,ptime.clock.os,re.str,lens.ppx_deriving,mustache
PKGS = toml,opium.unix,omd,str,batteries,tyxml,lens,ptime,ptime.clock.os,re.str,lens.ppx_deriving,mustache
all: web

View File

@ -31,7 +31,7 @@ let logarion_text ?text_tpl:(tpl=None) ymd =
let of_ymd ?text_tpl:(tpl=None) lgrn ymd =
logarion_page
Ymd.(ymd.meta.title ^ " by " ^ ymd.meta.author.name)
Logarion.(lgrn.title)
Logarion.Configuration.(lgrn.title)
(logarion_text ~text_tpl:tpl ymd)
|> to_string
@ -41,7 +41,7 @@ let article_link (file, meta) =
]
let of_file_meta_pairs ?listing_tpl:(tpl=None) lgrn file_meta_pairs =
let t = Logarion.(lgrn.title) in
let t = Logarion.Configuration.(lgrn.title) in
logarion_page
t t
(div [ h2 [pcdata "Articles"];
@ -55,31 +55,34 @@ let form lgrn ymd =
let input_set title name value =
p [ label [ span [pcdata title]; input ~a:[a_name name; a_value value] () ] ]
in
let either a b = if a <> "" then a else b in
logarion_page
"Compose" "Article composition"
Ymd.(div [
form
~a:[a_method `Post; a_action (uri_of_string "/post"); a_accept_charset ["utf-8"];]
[
fieldset
~legend:(legend [pcdata "Article"])
form
~a:[a_method `Post; a_action (uri_of_string "/post"); a_accept_charset ["utf-8"];]
[
input_set "Title" "title" ymd.meta.title;
input_set "Author name" "name" ymd.meta.author.name;
input_set "Author email" "email" ymd.meta.author.email;
input_set "Topics" "topics" (String.concat ", " ymd.meta.topics);
input_set "Categories" "categories" (String.concat ", " ymd.meta.categories);
input_set "Keywords" "keywords" (String.concat ", " ymd.meta.keywords);
input_set "Series" "series" (String.concat ", " ymd.meta.series);
input_set "Abstract" "abstract" ymd.meta.abstract;
p [
label [
span [pcdata"Text"];
textarea ~a:[a_name "body"] (pcdata ymd.body);
fieldset
~legend:(legend [pcdata "Article"])
[
input_set "Title" "title" ymd.meta.title;
input_set "Author name" "name"
(either ymd.meta.author.name Logarion.Configuration.(lgrn.owner));
input_set "Author email" "email"
(either ymd.meta.author.email Logarion.Configuration.(lgrn.email));
input_set "Topics" "topics" (String.concat ", " ymd.meta.topics);
input_set "Categories" "categories" (String.concat ", " ymd.meta.categories);
input_set "Keywords" "keywords" (String.concat ", " ymd.meta.keywords);
input_set "Series" "series" (String.concat ", " ymd.meta.series);
input_set "Abstract" "abstract" ymd.meta.abstract;
p [
label [
span [pcdata"Text"];
textarea ~a:[a_name "body"] (pcdata ymd.body);
];
];
];
p [ button ~a:[a_button_type `Submit] [pcdata "Submit"] ];
p [ button ~a:[a_button_type `Submit] [pcdata "Submit"] ];
]
]
]
])
])
|> to_string

View File

@ -1,4 +1,21 @@
type logarion = { title : string }
module Configuration = struct
type t = {
title : string;
owner : string;
email : string;
}
let of_filename fn =
let result = Toml.Parser.from_filename fn in
match result with
| `Error (str, loc) -> { title = ""; owner = ""; email = "" }
| `Ok tbl ->
let str_of key_name =
let open TomlLenses in
let value_option = get tbl (key "general" |-- table |-- key key_name |-- string) in
match value_option with Some v -> v | None -> "" in
{ title = str_of "title"; owner = str_of "owner"; email = str_of "email" }
end
let load_file f =
let ic = open_in f in

View File

@ -16,7 +16,7 @@ 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 lgrn = Logarion.({ title = "Test" })
let lgrn = Logarion.Configuration.of_filename "logarion.toml"
let () =
let (>>=) = Lwt.(>>=)