Changeset 18 in code for trunk/cli/html.ml
- Timestamp:
- Oct 22, 2022, 8:55:08 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cli/html.ml
r15 r18 8 8 let init kv = 9 9 let open Logarion in 10 let header = match Store.KV.find "HTM-header"kv with10 let to_string key kv = match Store.KV.find key kv with 11 11 | fname -> Some (File_store.to_string fname) 12 12 | exception Not_found -> None in 13 let footer = match Store.KV.find "HTM-footer" kv with 14 | fname -> Some (File_store.to_string fname) 15 | exception Not_found -> None in 13 let header = to_string "HTM-header" kv in 14 let footer = to_string "HTM-footer" kv in 16 15 { templates = { header; footer} } 17 16 … … 28 27 "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>" 29 28 in 30 let footer = match htm.templates.footer with None -> "" | Some x -> replace x in 31 "<!DOCTYPE HTML><html><head><title>" ^ text_title ^ " • " ^ site_title ^ "</title>\n\ 32 <link rel='stylesheet' href='main.css'>\ 33 <link rel='alternate' href='feed.atom' type='application/atom+xml'>\ 34 <meta charset='utf-8'/><meta name='viewport' content='width=device-width, initial-scale=1.0'>\ 35 </head><body>\n" ^ header ^ body ^ footer ^ "</body></html>" 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 36 37 37 38 let topic_link root topic = 38 39 40 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>" 41 42 42 43 module HtmlConverter = struct … … 47 48 48 49 let page htm conversion text = 49 50 51 52 53 54 55 (* 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 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 = 60 let time x = {|<time datetime="|} ^ x ^ {|">|} ^ x ^ "</time>" in 61 let topic_links x = 62 let to_linked t a = 63 let ts = Topic_set.of_string t in 64 sep_append a (List.fold_left (fun a t -> sep_append ~sep:" > " a (topic_link (List.hd ts) t)) "" ts) in 65 String_set.fold to_linked x "" in 66 "<article><header><dl>" 67 ^ opt_kv "Title:" text.title 68 ^ opt_kv "Authors:" authors 69 ^ opt_kv "Date: " (time (Date.(pretty_date @@ listing text.date))) 70 ^ opt_kv "Series: " (str_set "series" text) 71 ^ opt_kv "Topics: " (topic_links (set "topics" text)) 72 ^ opt_kv "Keywords: " keywords 73 ^ opt_kv "Id: " text.id 74 ^ {|</dl></header><pre style="white-space:pre-wrap">|} in 75 wrap conversion htm text.title ((T.of_string text.body header) ^ "</pre></article>") 75 76 76 77 let to_dated_links ?(limit) meta_list = 77 78 79 80 81 82 83 84 85 86 87 88 89 78 let meta_list = match limit with 79 | None -> meta_list 80 | Some limit-> 81 let rec reduced acc i = function 82 | [] -> acc 83 | h::t -> if i < limit then reduced (h::acc) (i+1) t else acc in 84 List.rev @@ reduced [] 0 meta_list 85 in 86 List.fold_left 87 (fun a m -> Printf.sprintf "%s<li> %s <a href=\"%s.htm\">%s</a>" a 88 Logarion.(Date.(pretty_date (listing m.Text.date))) 89 (Logarion.Text.short_id m) m.Logarion.Text.title) 90 "" meta_list 90 91 91 92 let date_index ?(limit) conv htm meta_list = 92 93 94 93 match limit with 94 | Some limit -> wrap conv htm "Index" (to_dated_links ~limit meta_list) 95 | None -> wrap conv htm "Index" (to_dated_links meta_list) 95 96 96 97 let fold_topic_roots topic_roots = 97 98 99 100 98 let list_item root t = "<li>" ^ topic_link root t in 99 "<nav><h2>Main topics</h2>" 100 ^ List.fold_left (fun a x -> a ^ list_item x x) "<ul>" (List.rev topic_roots) 101 ^ "</ul></nav>" 101 102 102 103 let fold_topics topic_map topic_roots metas = 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 104 let open Logarion in 105 let rec unordered_list root topic = 106 List.fold_left (fun a x -> a ^ list_item root x) "<ul>" topic 107 ^ "</ul>" 108 and sub_items root topic = match Topic_set.Map.find_opt topic topic_map with 109 | None -> "" 110 | Some (_, subtopics) -> unordered_list root (String_set.elements subtopics) 111 and list_item root t = 112 let item = 113 if List.exists (fun x -> String_set.mem t (String_set.map Topic_set.topic (Text.set "topics" x))) metas 114 then topic_link root t else String.capitalize_ascii t 115 in 116 "<li>" ^ item ^ sub_items root t 117 in 118 "<nav><h2>Topics</h2>" 119 ^ List.fold_left (fun a x -> a ^ list_item x x) "<ul>" (List.rev topic_roots) 120 ^ "</ul></nav>" 120 121 121 122 let text_item path meta = 122 123 124 125 123 let open Logarion in 124 "<time>" ^ Date.(pretty_date (listing meta.Text.date)) 125 ^ {|</time> <a href="|} ^ path ^ Text.short_id meta ^ {|.htm">|} ^ meta.Text.title 126 ^ "</a><br>" 126 127 127 128 let listing_index topic_map topic_roots path metas = 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | x ->{|<h2 id="|} ^ topic ^ {|">|} ^ String.capitalize_ascii topic ^ "</h2>" ^ x143 144 129 let rec item_group topics = 130 List.fold_left (fun acc topic -> acc ^ sub_groups topic ^ items topic) "" topics 131 and sub_groups topic = match Logarion.Topic_set.Map.find_opt topic topic_map with 132 | None -> "" 133 | Some (_, subtopics) -> item_group (Logarion.String_set.elements subtopics) 134 and items topic = 135 let items = 136 let open Logarion in 137 List.fold_left 138 (fun a e -> 139 if String_set.mem topic (String_set.map (Logarion.Topic_set.topic) (Text.set "Topics" e)) 140 then text_item path e ^ a else a) "" metas in 141 match items with 142 | "" -> "" 143 | x -> {|<h2 id="|} ^ topic ^ {|">|} ^ String.capitalize_ascii topic ^ "</h2>" ^ x 144 in 145 "<nav><h1>Texts</h1>" ^ item_group topic_roots ^ "</nav>" 145 146 146 147 let topic_main_index conv htm topic_roots metas = 147 148 149 150 151 152 153 154 155 148 wrap conv htm "Topics" 149 (fold_topic_roots topic_roots 150 ^ "<nav><h1>Latest</h1><ul>" ^ to_dated_links ~limit:8 metas 151 ^ {|</ul><a href="index.date.htm">More by date</a>|} 152 ^ let peers = Logarion.Store.KV.find "Peers" conv.kv in 153 (if peers = "" then "" else 154 List.fold_left (fun a s -> Printf.sprintf {|%s<li><a href="%s">%s</a>|} a s s) "<h1>Peers</h1><ul>" 155 (Str.split (Str.regexp ";\n") (Logarion.Store.KV.find "Peers" conv.kv)) 156 ^ "</ul>")) 156 157 157 158 let topic_sub_index conv htm topic_map topic_root metas = 158 159 160 (* 161 159 wrap conv htm topic_root 160 (fold_topics topic_map [topic_root] metas 161 (* ^ {|<a href=".atom" id="feed">|}^ String.capitalize_ascii topic_root ^{| feed </a>|}*) 162 ^ listing_index topic_map [topic_root] "" metas) 162 163 163 164 open Logarion
Note:
See TracChangeset
for help on using the changeset viewer.