refactored mapping of string to key-ed values

This commit is contained in:
Stavros Polymenis 2017-01-15 01:36:12 +00:00
parent 20780c2d76
commit f6f75bd082
2 changed files with 19 additions and 32 deletions

View File

@ -25,46 +25,16 @@ let concat l = String.concat "" l
let fold_text ymd =
let open Ymd in
let open Ymd.Meta in
let escaped e = match e with
| "title" -> ymd.meta.title
| "abstract" -> ymd.meta.abstract
| "author_name" -> ymd.meta.author.Author.name
| "author_email" -> ymd.meta.author.Author.email
| "date_edited" -> Date.(rfc_string ymd.meta.date.edited)
| "date_published" -> Date.(rfc_string ymd.meta.date.published)
| "date_human" -> Date.(pretty_date @@ last ymd.meta.date)
| "date" -> Date.(rfc_string @@ last ymd.meta.date)
| "topics" -> String.concat ", " ymd.meta.topics;
| "categories" -> CategorySet.to_csv ymd.meta.categories;
| "keywords" -> String.concat ", " ymd.meta.keywords;
| "series" -> String.concat ", " ymd.meta.series;
| "body" -> Omd.to_html @@ Omd.of_string ymd.body
| "uuid" -> Id.to_string ymd.meta.uuid
| _ -> prerr_endline ("unknown tag: " ^ e); "" in
| tag -> Meta.value_with_name ymd.meta tag in
Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat
let fold_entry (entry : Logarion.Entry.t) =
let open Logarion.Entry in
let meta = entry.attributes in
let open Ymd in
let open Ymd.Meta in
let escaped e = match e with
| "url" -> "/text/" ^ slug entry
| "title" -> meta.title
| "abstract" -> meta.abstract
| "author_name" -> meta.author.Author.name
| "author_email" -> meta.author.Author.email
| "date_edited" -> Date.(rfc_string meta.date.edited)
| "date_published" -> Date.(rfc_string meta.date.published)
| "date_human" -> Date.(pretty_date @@ last meta.date)
| "date" -> Date.(rfc_string @@ last meta.date)
| "topics" -> String.concat ", " meta.topics;
| "categories" -> CategorySet.to_csv meta.categories;
| "keywords" -> String.concat ", " meta.keywords;
| "series" -> String.concat ", " meta.series;
| "uuid" -> Id.to_string meta.uuid
| _ -> prerr_endline ("unknown tag: " ^ e); "" in
| tag -> Ymd.Meta.value_with_name entry.attributes tag in
Mustache.fold ~string ~section ~escaped ~unescaped ~partial ~comment ~concat
let fold_header blog_url title =

View File

@ -86,6 +86,23 @@ module Meta = struct
abstract = "";
uuid;
}
let value_with_name (meta as m) =
function
| "title" -> m.title
| "abstract" -> m.abstract
| "author_name" -> m.author.Author.name
| "author_email" -> m.author.Author.email
| "date" -> Date.(rfc_string @@ last m.date)
| "date_edited" -> Date.(rfc_string m.date.edited)
| "date_published"-> Date.(rfc_string m.date.published)
| "date_human" -> Date.(pretty_date @@ last m.date)
| "topics" -> String.concat ", " m.topics;
| "categories" -> CategorySet.to_csv m.categories;
| "keywords" -> String.concat ", " m.keywords;
| "series" -> String.concat ", " m.series;
| "uuid" -> Id.to_string m.uuid
| e -> raise @@ Invalid_argument e
end
type ymd = {