Changeset 3 in code for trunk/cli/html.ml
- Timestamp:
- Apr 15, 2022, 1:17:01 PM (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cli/html.ml
r2 r3 1 let wrap (title:string) (subtitle:string) body = 2 {|<!DOCTYPE HTML>|} 3 ^ {|<html><head><title>|} 4 ^ subtitle ^ " | " ^ title 5 ^ {|</title><link rel="stylesheet" href="main.css">|} 6 ^ {|<link rel="alternate" href="feed.atom" type="application/atom+xml">|} 7 ^ {|<meta charset="utf-8"/>|} 8 ^ {|<meta name="viewport" content="width=device-width, initial-scale=1.0">|} 9 ^ {|</head><body><header><a href=".">|} ^ title 10 ^ {|</a> <nav><a href="feed.atom" id="feed">feed</a></nav></header>|} ^ body 11 ^ "</body></html>" 1 type templates_t = { header: string option; footer: string option } 2 type t = { templates : templates_t } 3 4 let ext = ".htm" 5 let empty_templates = { header = None; footer = None } 6 let default_opts = { templates = empty_templates } 7 8 let init kv = 9 let open Logarion in 10 let header = match Store.KV.find "HTM-header" kv with 11 | fname -> Some (File_store.to_string fname) 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 16 { templates = { header; footer} } 17 18 let wrap c htm text_title body = 19 let site_title = try Logarion.Store.KV.find "Title" c.Conversion.kv 20 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 header = match htm.templates.header with 26 | Some x -> replace x 27 | None -> "<header><a href='.'>" ^ site_title ^ 28 "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>" 29 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>" 12 36 13 37 let topic_link root topic = 14 38 let replaced_space = String.map (function ' '->'+' | x->x) in 15 {|<a href="index.|} ^ root ^ {|.htm#|} ^ replaced_space topic ^ {|">|}39 "<a href='index." ^ replaced_space root ^ ".htm#" ^ replaced_space topic ^ "'>" 16 40 ^ String.capitalize_ascii topic ^ "</a>" 17 41 18 let page archive_title text = 42 module HtmlConverter = struct 43 include Converter.Html 44 let angled_uri u a = if String.sub u 0 10 <> "urn:txtid:" then 45 angled_uri u a else angled_uri (String.(sub u 10 (length u - 10)) ^ ext) a 46 end 47 48 let page htm conversion text = 19 49 let open Logarion in 20 50 let open Text in 21 let module T = Parsers.Plain_text.Make ( Converter.Html) in51 let module T = Parsers.Plain_text.Make (HtmlConverter) in 22 52 let sep_append ?(sep=", ") a x = match a,x with "",_ -> x | _, "" -> a | _ -> a ^ sep ^ x in 23 let opt_kv key value = if String.length value > 0 then "<dt>" ^ key ^ "<dd>" ^ value else "" in 53 let opt_kv key value = if String.length value > 0 54 then "<dt>" ^ key ^ "<dd>" ^ value else "" in 24 55 (* let author acc auth = sep_append acc Person.(auth.name ^ " ") in*) 25 56 let authors = (Person.Set.to_string text.authors ^ " ") in … … 39 70 ^ opt_kv "Topics: " (topic_links (set "topics" text)) 40 71 ^ opt_kv "Keywords: " keywords 41 ^ opt_kv "Id: " (Id.to_string text.uuid)72 ^ opt_kv "Id: " text.id 42 73 ^ {|</dl></header><pre style="white-space:pre-wrap">|} in 43 wrap archive_titletext.title ((T.of_string text.body header) ^ "</pre></article>")74 wrap conversion htm text.title ((T.of_string text.body header) ^ "</pre></article>") 44 75 45 76 let to_dated_links ?(limit) meta_list = … … 58 89 "" meta_list 59 90 60 let date_index ?(limit) titlemeta_list =91 let date_index ?(limit) conv htm meta_list = 61 92 match limit with 62 | Some limit -> wrap title"Index" (to_dated_links ~limit meta_list)63 | None -> wrap title"Index" (to_dated_links meta_list)93 | Some limit -> wrap conv htm "Index" (to_dated_links ~limit meta_list) 94 | None -> wrap conv htm "Index" (to_dated_links meta_list) 64 95 65 96 let fold_topic_roots topic_roots = … … 113 144 "<nav><h1>Texts</h1>" ^ item_group topic_roots ^ "</nav>" 114 145 115 let topic_main_index titletopic_roots metas =116 wrap title"Topics"146 let topic_main_index conv htm topic_roots metas = 147 wrap conv htm "Topics" 117 148 (fold_topic_roots topic_roots 118 149 ^ "<nav><h1>Latest</h1>" ^ to_dated_links ~limit:10 metas 119 150 ^ {|<a href="index.date.htm">More by date</a></nav>|} ) 120 151 121 let topic_sub_index titletopic_map topic_root metas =122 wrap titletopic_root152 let topic_sub_index conv htm topic_map topic_root metas = 153 wrap conv htm topic_root 123 154 (fold_topics topic_map [topic_root] metas 124 155 (* ^ {|<a href=".atom" id="feed">|}^ String.capitalize_ascii topic_root ^{| feed </a>|}*) 125 156 ^ listing_index topic_map [topic_root] "" metas) 157 158 open Logarion 159 let indices htm c = 160 let file name = Logarion.File_store.file (Filename.concat c.Conversion.dir name) in 161 let index_name = try Store.KV.find "HTM-index" c.Conversion.kv with Not_found -> "index.html" in 162 let title = try Store.KV.find "Title" c.Conversion.kv with Not_found -> "" in 163 164 if index_name <> "" then 165 file index_name (topic_main_index c htm c.topic_roots c.texts); 166 167 file "index.date.htm" (date_index c htm c.texts); 168 169 List.iter 170 (fun root -> file ("index." ^ root ^ ".htm") (topic_sub_index c htm c.topics root c.texts)) 171 c.topic_roots; 172 173 let base_url = try 174 let _i = Str.(search_forward (regexp "https?://[^;]*") (Store.KV.find "Locations" c.kv) 0) in 175 Str.(matched_string (Store.KV.find "Locations" c.kv)) 176 with Not_found -> prerr_endline "Missing location for HTTP(S)"; "" in 177 file "feed.atom" (Atom.feed title c.id base_url "text/html" c.texts)
Note:
See TracChangeset
for help on using the changeset viewer.