source: code/trunk/cmd/txt/convert.ml@ 74

Last change on this file since 74 was 73, checked in by Izuru Yakumo, 8 months ago

Because sweet girls are the best, officially rebranding Logarion to Kosuzu

Signed-off-by: Izuru Yakumo <yakumo.izuru@…>

File size: 4.3 KB
Line 
1open Kosuzu
2
3let is_older s d = try Unix.((stat d).st_mtime < (stat s).st_mtime) with _-> true
4
5let convert cs r (text, files) = match Text.str "Content-Type" text with
6 | "" | "text/plain" ->
7 let source = List.hd files in
8 let dest = Filename.concat r.Conversion.dir (Text.short_id text) in
9 List.fold_left (fun a f ->
10 match f.Conversion.page with None -> false || a
11 | Some page ->
12 let dest = dest ^ f.Conversion.ext in
13 (if is_older source dest || Conversion.Rel.Id_map.mem text.Text.id r.relations
14 then (File_store.file dest (page r text); true) else false)
15 || a)
16 false cs
17 | x -> Printf.eprintf "Can't convert Content-Type: %s file: %s" x text.Text.title; false
18
19let converters types kv =
20 let n = String.split_on_char ',' types in
21 let t = [] in
22 let t = if List.(mem "all" n || mem "htm" n) then (Html.converter kv)::t else t in
23 let t = if List.(mem "all" n || mem "atom" n) then (Atom.converter "text/html")::t else t in
24 let t = if List.(mem "all" n || mem "gmi" n) then (Gemini.converter)::t else t in
25 let t = if List.(mem "all" n || mem "gmi-atom" n) then (Atom.converter "text/gemini")::t else t in
26 t
27
28let directory converters noindex repo =
29 let order = File_store.oldest in
30 let repo =
31 let open Conversion in
32 let rels = File_store.fold ~dir:repo.dir ~order Rel.acc_txt Rel.empty_map in
33 let relations = Peers.fold Rel.acc_pck rels in
34 { repo with relations } in
35 let acc (ts,ls,acc) ((elt,_) as r) = Topic_set.to_map ts (Text.set "topics" elt), elt::ls,
36 if convert converters repo r then acc+1 else acc in
37 let topics, texts, count =
38 File_store.fold ~dir:repo.Conversion.dir ~order acc (Topic_set.Map.empty, [], 0) in
39 let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" repo.kv)
40 with Not_found -> Topic_set.roots topics in
41 let repo = Conversion.{ repo with topic_roots; topics; texts = List.rev texts } in
42 if not noindex then
43 List.iter (fun c -> match c.Conversion.indices with None -> () | Some f -> f repo) converters;
44 Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
45
46let load_kv dir =
47 let kv = File_store.of_kv_file () in
48 let idx = Filename.concat dir "index.pck" in
49 if not (Sys.file_exists idx) then kv else
50 match Header_pack.of_string @@ File_store.to_string (idx) with
51 | Error s -> prerr_endline s; kv
52 | Ok { info; peers; _ } ->
53 let kv = if Store.KV.mem "Id" kv then kv else Store.KV.add "Id" info.Header_pack.id kv in
54 let kv = if Store.KV.mem "Title" kv then kv else Store.KV.add "Title" info.Header_pack.title kv in
55 let kv = if Store.KV.mem "Locations" kv then kv else Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
56 let kv = Store.KV.add "Peers" (String.concat ";\n" Header_pack.(to_str_list peers)) kv in
57 kv
58
59let at_path types noindex path = match path with
60 | "" -> prerr_endline "unspecified text file or directory"
61 | path when Sys.file_exists path ->
62 if Sys.is_directory path then (
63 let kv = load_kv path in
64 let repo = { (Conversion.empty ()) with dir = path; kv } in
65 directory (converters types kv) noindex repo
66 ) else (
67 match File_store.to_text path with
68 | Error s -> prerr_endline s
69 | Ok text ->
70 let dir = "." in
71 let open Conversion in
72 let relations = File_store.(fold ~dir ~order:newest Rel.acc_txt Rel.empty_map) in
73 let repo = { (Conversion.empty ()) with dir; kv = load_kv ""; relations } in
74 ignore @@ convert (converters types repo.kv) repo (text, [path])
75 )
76 | path -> Printf.eprintf "Path doesn't exist: %s" path
77
78open Cmdliner
79
80let path = Arg.(value & pos 0 string "" & info [] ~docv:"path" ~doc:"Text file or directory to convert. If directory is provided, it must contain an index.pck (see: txt index)")
81let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type" ~doc:"Convert to file type")
82let noindex = Arg.(value & flag & info ["noindex"] ~doc:"Don't create indices in target format")
83
84let convert_t = Term.(const at_path $ types $ noindex $ path)
85
86let cmd =
87 let doc = "Convert texts" in
88 let man = [
89 `S Manpage.s_description;
90 `P "Convert text or indexed texts within a directory to another format.";
91 `P "If path is a directory must contain an index.pck.";
92 `P "Run `txt index` first." ]
93 in
94 let info = Cmd.info "convert" ~doc ~man in
95 Cmd.v info convert_t
Note: See TracBrowser for help on using the repository browser.