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

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

Support References field

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