Changeset 20 in code
- Timestamp:
- Oct 30, 2022, 2:48:02 PM (2 years ago)
- Location:
- trunk/cli
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/cli/conversion.ml
r19 r20 1 1 open Logarion 2 2 type t = { 3 id: string; dir: string; 3 id: string; 4 dir: string; 4 5 kv: string Store.KV.t; 5 6 topic_roots: string list; … … 13 14 indices: (t -> unit) option; 14 15 } 16 17 let empty () = { 18 id = ""; dir = ""; 19 kv = Store.KV.empty; 20 topic_roots = []; 21 topics = Topic_set.Map.empty; 22 texts = [] 23 } -
trunk/cli/convert.ml
r19 r20 1 1 open Logarion 2 2 3 let is_older source dest = try 4 Unix.((stat dest).st_mtime < (stat source).st_mtime) with _-> true3 (*TODO: move to converters (style, feed checks)*) 4 let is_older s d = try Unix.((stat d).st_mtime < (stat s).st_mtime) with _-> true 5 5 6 6 let convert cs r (text, files) = match Text.str "Content-Type" text with … … 27 27 t 28 28 29 let directory converters noindex dir id kv = 30 let empty = Topic_set.Map.empty in 31 let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in 29 let directory converters noindex repo = 32 30 let fn (ts,ls,acc) ((elt,_) as r) = 33 31 (Topic_set.to_map ts (Text.set "topics" elt)), elt::ls, 34 32 if convert converters repo r then acc+1 else acc in 35 let topics, texts, count = File_store.(fold ~dir ~order:newest fn (empty,[],0)) in 36 let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" kv) 33 let topics, texts, count = 34 File_store.(fold ~dir:repo.Conversion.dir ~order:newest fn (Topic_set.Map.empty,[],0)) in 35 let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" repo.kv) 37 36 with Not_found -> Topic_set.roots topics in 38 37 let repo = Conversion.{ repo with topic_roots; topics; texts } in … … 40 39 Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts) 41 40 42 let at_path types noindex path = 43 match path with "" -> prerr_endline "unspecified text file or directory" 44 | dir when Sys.file_exists dir && Sys.is_directory dir -> 45 let fname = Filename.concat dir "index.pck" in 46 (match Header_pack.of_string @@ File_store.to_string fname with 47 | Error s -> prerr_endline s 41 let load_kv dir = 42 let conf = Filename.concat dir ".convert.conf" in (* TODO: better name? *) 43 let kv = if Sys.file_exists conf then File_store.of_kv_file conf else Store.KV.empty in 44 let idx = Filename.concat dir "index.pck" in 45 if not (Sys.file_exists idx) then kv else 46 match Header_pack.of_string @@ File_store.to_string (idx) with 47 | Error s -> prerr_endline s; kv 48 48 | Ok { info; peers; _ } -> 49 let kv = let f = Filename.concat dir ".convert.conf" in (* TODO: better place to store convert conf? *) 50 if Sys.file_exists f then File_store.of_kv_file f else Store.KV.empty in 49 let kv = if Store.KV.mem "Id" kv then kv else Store.KV.add "Id" info.Header_pack.id kv in 51 50 let kv = if Store.KV.mem "Title" kv then kv else Store.KV.add "Title" info.Header_pack.title kv in 52 51 let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in 53 52 let kv = Store.KV.add "Peers" (String.concat ";\n" Header_pack.(to_str_list peers)) kv in 54 let cs = converters types kv in 55 directory cs noindex dir info.Header_pack.id kv) 53 kv 54 55 let at_path types noindex path = match path with 56 | "" -> prerr_endline "unspecified text file or directory" 56 57 | path when Sys.file_exists path -> 57 let repo = Conversion.{ 58 id = ""; dir = ""; kv = Store.KV.empty; topic_roots = []; 59 topics = Topic_set.Map.empty; texts = [] } in 60 let cs = converters types repo.kv in 61 (match File_store.to_text path with 62 | Ok text -> ignore @@ convert cs repo (text, [path]) 63 | Error s -> prerr_endline s) 58 if Sys.is_directory path then ( 59 let kv = load_kv path in 60 let repo = { (Conversion.empty ()) with dir = path; kv } in 61 directory (converters types kv) noindex repo 62 ) else ( 63 match File_store.to_text path with 64 | Error s -> prerr_endline s 65 | Ok text -> 66 let repo = { (Conversion.empty ()) with dir = ""; kv = load_kv "" } in 67 ignore @@ convert (converters types repo.kv) repo (text, [path]) 68 ) 64 69 | path -> Printf.eprintf "Path doesn't exist: %s" path 65 70 … … 67 72 let term = 68 73 let path = Arg.(value & pos 0 string "" & info [] ~docv:"path" 69 ~doc:"Text file or directory to convert. Ff directory is provided, it must contain an index.pck (see: txt index)") in74 ~doc:"Text file or directory to convert. If directory is provided, it must contain an index.pck (see: txt index)") in 70 75 let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type" 71 76 ~doc:"Convert to file type") in -
trunk/cli/html.ml
r19 r20 1 1 type templates_t = { header: string option; footer: string option } 2 type t = { templates : templates_t }2 type t = { templates : templates_t; style : string } 3 3 4 4 let ext = ".htm" 5 5 let empty_templates = { header = None; footer = None } 6 let default_opts = { templates = empty_templates }6 let default_opts = { templates = empty_templates; style = "" } 7 7 8 8 let init kv = … … 13 13 let header = to_string "HTM-header" kv in 14 14 let footer = to_string "HTM-footer" kv in 15 { templates = { header; footer} } 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 } 16 18 17 19 let wrap conv htm text_title body = 18 let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv 19 with Not_found -> "" in 20 let site_title = try Logarion.Store.KV.find "Title" conv.Conversion.kv with Not_found -> "" in 20 21 let replace x = let open Str in 21 global_replace (regexp "{{archive-title}}") site_title x22 global_replace (regexp "{{archive-title}}") site_title x 22 23 |> global_replace (regexp "{{text-title}}") text_title 23 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 24 28 let header = match htm.templates.header with 25 | Some x -> replace x26 | None -> "<header><a href='.'>" ^ site_title ^27 "</a><nav><a href='feed.atom' id='feed'>feed</a></nav></header>"29 | Some x -> replace x 30 | None -> Printf.(sprintf "<header><a href='.'>%s</a></header>%s" site_title 31 (if feed <> "" then sprintf "<nav><a href='%s' id='feed'>feed</a></nav>" feed else "")) 28 32 in 29 33 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'>\ 34 Printf.sprintf "<!DOCTYPE HTML><html><head><title>%s%s</title>\n%s\n%s\ 33 35 <meta charset='utf-8'/><meta name='viewport' content='width=device-width, initial-scale=1.0'>\ 34 36 </head><body>\n%s%s%s</body></html>" 35 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 "") 36 40 header body footer 37 41 … … 151 155 ^ "<nav><h1>Latest</h1><ul>" ^ to_dated_links ~limit:8 metas 152 156 ^ {|</ul><a href="index.date.htm">More by date</a>|} 153 ^ let peers = Logarion.Store.KV.find "Peers" conv.kvin157 ^ let peers = try Logarion.Store.KV.find "Peers" conv.kv with Not_found -> "" in 154 158 (if peers = "" then "" else 155 159 List.fold_left (fun a s -> Printf.sprintf {|%s<li><a href="%s">%s</a>|} a s s) "<h1>Peers</h1><ul>"
Note:
See TracChangeset
for help on using the changeset viewer.