source: code/trunk/cli/convert.ml@ 19

Last change on this file since 19 was 19, checked in by fox, 3 years ago

Accept comma separated converter names, separate Atom from Html and Gemini converters.

Note: atom must be called separately now because of the separation. Example

txt convert -t htm,atom xyz

File size: 3.8 KB
Line 
1open Logarion
2
3let is_older source dest = try
4 Unix.((stat dest).st_mtime < (stat source).st_mtime) with _-> true
5
6let convert cs r (text, files) = match Text.str "Content-Type" text with
7 | "" | "text/plain" ->
8 let source = List.hd files in
9 let dest = Filename.concat r.Conversion.dir (Text.short_id text) in
10 List.fold_left
11 (fun a f ->
12 match f.Conversion.page with None -> false || a
13 | Some page ->
14 let dest = dest ^ f.Conversion.ext in
15 (if is_older source dest then (File_store.file dest (page r text); true) else false)
16 || a)
17 false cs
18 | x -> Printf.eprintf "Can't convert Content-Type: %s file: %s" x text.Text.title; false
19
20let converters types kv =
21 let n = String.split_on_char ',' types in
22 let t = [] in
23 let t = if List.(mem "all" n || mem "htm" n) then (Html.converter kv)::t else t in
24 let t = if List.(mem "all" n || mem "atom" n) then (Atom.converter "text/html")::t else t in
25 let t = if List.(mem "all" n || mem "gmi" n) then (Gemini.converter)::t else t in
26 let t = if List.(mem "all" n || mem "gmi-atom" n) then (Atom.converter "text/gemini")::t else t in
27 t
28
29let 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
32 let fn (ts,ls,acc) ((elt,_) as r) =
33 (Topic_set.to_map ts (Text.set "topics" elt)), elt::ls,
34 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)
37 with Not_found -> Topic_set.roots topics in
38 let repo = Conversion.{ repo with topic_roots; topics; texts } in
39 if not noindex then List.iter (fun c -> match c.Conversion.indices with None -> () | Some f -> f repo) converters;
40 Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
41
42let 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
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
51 let kv = if Store.KV.mem "Title" kv then kv else Store.KV.add "Title" info.Header_pack.title kv in
52 let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
53 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)
56 | 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)
64 | path -> Printf.eprintf "Path doesn't exist: %s" path
65
66open Cmdliner
67let term =
68 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)") in
70 let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type"
71 ~doc:"Convert to file type") in
72 let noindex = Arg.(value & flag & info ["noindex"]
73 ~doc:"Don't create indices in target format") in
74 Term.(const at_path $ types $ noindex $ path),
75 Term.info "convert" ~doc:"convert texts"
76 ~man:[ `S "DESCRIPTION"; `P "Convert text or indexed texts within a directory to another format.
77 If path is a directory must contain an index.pck. Run `txt index` first." ]
Note: See TracBrowser for help on using the repository browser.