source: code/trunk/cli/html.ml@ 19

Last change on this file since 19 was 19, checked in by fox, 2 years ago

Accept comma separated converter names, separate Atom from Html and Gemini converters.

Note: atom must be called separately now because of the separation. Example

txt convert -t htm,atom xyz

File size: 7.0 KB
RevLine 
[3]1type templates_t = { header: string option; footer: string option }
2type t = { templates : templates_t }
[2]3
[3]4let ext = ".htm"
5let empty_templates = { header = None; footer = None }
6let default_opts = { templates = empty_templates }
7
8let init kv =
9 let open Logarion in
[18]10 let to_string key kv = match Store.KV.find key kv with
[3]11 | fname -> Some (File_store.to_string fname)
12 | exception Not_found -> None in
[18]13 let header = to_string "HTM-header" kv in
14 let footer = to_string "HTM-footer" kv in
[3]15 { templates = { header; footer} }
16
[19]17let wrap conv htm text_title body =
18 let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv
[3]19 with Not_found -> "" in
20 let replace x = let open Str in
21 global_replace (regexp "{{archive-title}}") site_title x
22 |> global_replace (regexp "{{text-title}}") text_title
23 in
24 let header = match htm.templates.header with
25 | Some x -> replace x
26 | None -> "<header><a href='.'>" ^ site_title ^
27 "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>"
28 in
[18]29 let footer = match htm.templates.footer with None -> "" | Some x -> replace x in
30 Printf.sprintf "<!DOCTYPE HTML><html><head><title>%s%s</title>\n\
31 <link rel='stylesheet' href='main.css'>\
32 <link rel='alternate' href='feed.atom' type='application/atom+xml'>\
33 <meta charset='utf-8'/><meta name='viewport' content='width=device-width, initial-scale=1.0'>\
34 </head><body>\n%s%s%s</body></html>"
35 text_title (if site_title <> "" then (" • " ^ site_title) else "")
36 header body footer
[3]37
[2]38let topic_link root topic =
[18]39 let replaced_space = String.map (function ' '->'+' | x->x) in
40 "<a href='index." ^ root ^ ".htm#" ^ replaced_space topic ^ "'>"
41 ^ String.capitalize_ascii topic ^ "</a>"
[2]42
[3]43module HtmlConverter = struct
44 include Converter.Html
45 let angled_uri u a = if String.sub u 0 10 <> "urn:txtid:" then
46 angled_uri u a else angled_uri (String.(sub u 10 (length u - 10)) ^ ext) a
47end
48
49let page htm conversion text =
[18]50 let open Logarion in
51 let open Text in
52 let module T = Parsers.Plain_text.Make (HtmlConverter) in
53 let sep_append ?(sep=", ") a x = match a,x with "",_ -> x | _, "" -> a | _ -> a ^ sep ^ x in
54 let opt_kv key value = if String.length value > 0
55 then "<dt>" ^ key ^ "<dd>" ^ value else "" in
56(* let author acc auth = sep_append acc Person.(auth.name ^ " ") in*)
57 let authors = (Person.Set.to_string text.authors ^ " ") in
58 let keywords = str_set "keywords" text in
59 let header =
[19]60 let time x = Printf.sprintf {|<time datetime="%s">%s</time>|}
61 (Date.rfc_string x) (Date.pretty_date x) in
[18]62 let topic_links x =
63 let to_linked t a =
64 let ts = Topic_set.of_string t in
65 sep_append a (List.fold_left (fun a t -> sep_append ~sep:" > " a (topic_link (List.hd ts) t)) "" ts) in
66 String_set.fold to_linked x "" in
67 "<article><header><dl>"
68 ^ opt_kv "Title:" text.title
69 ^ opt_kv "Authors:" authors
[19]70 ^ opt_kv "Date: " (time (Date.listing text.date))
[18]71 ^ opt_kv "Series: " (str_set "series" text)
72 ^ opt_kv "Topics: " (topic_links (set "topics" text))
73 ^ opt_kv "Keywords: " keywords
74 ^ opt_kv "Id: " text.id
75 ^ {|</dl></header><pre style="white-space:pre-wrap">|} in
76 wrap conversion htm text.title ((T.of_string text.body header) ^ "</pre></article>")
[2]77
78let to_dated_links ?(limit) meta_list =
[18]79 let meta_list = match limit with
80 | None -> meta_list
81 | Some limit->
82 let rec reduced acc i = function
83 | [] -> acc
84 | h::t -> if i < limit then reduced (h::acc) (i+1) t else acc in
85 List.rev @@ reduced [] 0 meta_list
86 in
87 List.fold_left
88 (fun a m -> Printf.sprintf "%s<li> %s <a href=\"%s.htm\">%s</a>" a
89 Logarion.(Date.(pretty_date (listing m.Text.date)))
90 (Logarion.Text.short_id m) m.Logarion.Text.title)
91 "" meta_list
[2]92
[3]93let date_index ?(limit) conv htm meta_list =
[18]94 match limit with
95 | Some limit -> wrap conv htm "Index" (to_dated_links ~limit meta_list)
96 | None -> wrap conv htm "Index" (to_dated_links meta_list)
[2]97
98let fold_topic_roots topic_roots =
[18]99 let list_item root t = "<li>" ^ topic_link root t in
100 "<nav><h2>Main topics</h2>"
101 ^ List.fold_left (fun a x -> a ^ list_item x x) "<ul>" (List.rev topic_roots)
102 ^ "</ul></nav>"
[2]103
104let fold_topics topic_map topic_roots metas =
[18]105 let open Logarion in
106 let rec unordered_list root topic =
107 List.fold_left (fun a x -> a ^ list_item root x) "<ul>" topic
108 ^ "</ul>"
109 and sub_items root topic = match Topic_set.Map.find_opt topic topic_map with
110 | None -> ""
111 | Some (_, subtopics) -> unordered_list root (String_set.elements subtopics)
112 and list_item root t =
113 let item =
114 if List.exists (fun x -> String_set.mem t (String_set.map Topic_set.topic (Text.set "topics" x))) metas
115 then topic_link root t else String.capitalize_ascii t
116 in
117 "<li>" ^ item ^ sub_items root t
118 in
119 "<nav><h2>Topics</h2>"
120 ^ List.fold_left (fun a x -> a ^ list_item x x) "<ul>" (List.rev topic_roots)
121 ^ "</ul></nav>"
[2]122
123let text_item path meta =
[18]124 let open Logarion in
125 "<time>" ^ Date.(pretty_date (listing meta.Text.date))
126 ^ {|</time> <a href="|} ^ path ^ Text.short_id meta ^ {|.htm">|} ^ meta.Text.title
127 ^ "</a><br>"
[2]128
129let listing_index topic_map topic_roots path metas =
[18]130 let rec item_group topics =
131 List.fold_left (fun acc topic -> acc ^ sub_groups topic ^ items topic) "" topics
132 and sub_groups topic = match Logarion.Topic_set.Map.find_opt topic topic_map with
133 | None -> ""
134 | Some (_, subtopics) -> item_group (Logarion.String_set.elements subtopics)
135 and items topic =
136 let items =
137 let open Logarion in
138 List.fold_left
139 (fun a e ->
140 if String_set.mem topic (String_set.map (Logarion.Topic_set.topic) (Text.set "Topics" e))
141 then text_item path e ^ a else a) "" metas in
142 match items with
143 | "" -> ""
144 | x -> {|<h2 id="|} ^ topic ^ {|">|} ^ String.capitalize_ascii topic ^ "</h2>" ^ x
145 in
146 "<nav><h1>Texts</h1>" ^ item_group topic_roots ^ "</nav>"
[2]147
[3]148let topic_main_index conv htm topic_roots metas =
[18]149 wrap conv htm "Topics"
150 (fold_topic_roots topic_roots
151 ^ "<nav><h1>Latest</h1><ul>" ^ to_dated_links ~limit:8 metas
152 ^ {|</ul><a href="index.date.htm">More by date</a>|}
153 ^ let peers = Logarion.Store.KV.find "Peers" conv.kv in
154 (if peers = "" then "" else
155 List.fold_left (fun a s -> Printf.sprintf {|%s<li><a href="%s">%s</a>|} a s s) "<h1>Peers</h1><ul>"
156 (Str.split (Str.regexp ";\n") (Logarion.Store.KV.find "Peers" conv.kv))
157 ^ "</ul>"))
[2]158
[3]159let topic_sub_index conv htm topic_map topic_root metas =
[18]160 wrap conv htm topic_root
161 (fold_topics topic_map [topic_root] metas
162(* ^ {|<a href=".atom" id="feed">|}^ String.capitalize_ascii topic_root ^{| feed </a>|}*)
163 ^ listing_index topic_map [topic_root] "" metas)
[3]164
165let indices htm c =
166 let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in
[19]167 let index_name = try Logarion.Store.KV.find "HTM-index" c.Conversion.kv with Not_found -> "index.html" in
168 if index_name <> "" then file index_name (topic_main_index c htm c.topic_roots c.texts);
[3]169 file "index.date.htm" (date_index c htm c.texts);
170 List.iter
171 (fun root -> file ("index." ^ root ^ ".htm") (topic_sub_index c htm c.topics root c.texts))
[19]172 c.topic_roots
[3]173
[19]174let converter kv =
175 let htm = init kv in
176 Conversion.{ ext; page = Some (page htm); indices = Some (indices htm) }
Note: See TracBrowser for help on using the repository browser.