Accept files & directories for conversion

This commit is contained in:
orbifx 2022-07-30 13:58:02 +01:00
parent cb33b6dcad
commit b30053d16e
2 changed files with 24 additions and 15 deletions

View File

@ -25,7 +25,7 @@ let converters types kv =
Conversion.{ ext = Gemini.ext; page = Gemini.page; indices = Gemini.indices}::t else t in Conversion.{ ext = Gemini.ext; page = Gemini.page; indices = Gemini.indices}::t else t in
t t
let convert_all converters noindex dir id kv = let directory converters noindex dir id kv =
let empty = Topic_set.Map.empty in let empty = Topic_set.Map.empty in
let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in
let fn (ts,ls,acc) ((elt,_) as r) = let fn (ts,ls,acc) ((elt,_) as r) =
@ -38,11 +38,11 @@ let convert_all converters noindex dir id kv =
if not noindex then List.iter (fun c -> c.Conversion.indices repo) converters; if not noindex then List.iter (fun c -> c.Conversion.indices repo) converters;
Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts) Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
let convert_dir types noindex dir = let at_path types noindex path =
match dir with "" -> prerr_endline "unspecified dir" match path with "" -> prerr_endline "unspecified text file or directory"
| dir -> | dir when Sys.file_exists dir && Sys.is_directory dir ->
let fname = Filename.concat dir "index.pck" in let fname = Filename.concat dir "index.pck" in
match Header_pack.of_string @@ File_store.to_string fname with (match Header_pack.of_string @@ File_store.to_string fname with
| Error s -> prerr_endline s | Error s -> prerr_endline s
| Ok { info; _ } -> | Ok { info; _ } ->
let kv = let f = Filename.concat dir ".convert.conf" in (* TODO: better place to store convert conf? *) let kv = let f = Filename.concat dir ".convert.conf" in (* TODO: better place to store convert conf? *)
@ -51,17 +51,26 @@ let convert_dir types noindex dir =
else Store.KV.add "Title" info.Header_pack.title kv in else Store.KV.add "Title" info.Header_pack.title kv in
let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
let cs = converters types kv in let cs = converters types kv in
convert_all cs noindex dir info.Header_pack.id kv directory cs noindex dir info.Header_pack.id kv)
| path when Sys.file_exists path ->
let repo = Conversion.{
id = ""; dir = ""; kv = Store.KV.empty; topic_roots = [];
topics = Topic_set.Map.empty; texts = [] } in
let cs = converters types repo.kv in
(match File_store.to_text path with
| Ok text -> ignore @@ convert cs repo (text, [path])
| Error s -> prerr_endline s)
| path -> Printf.eprintf "Path doesn't exist: %s" path
open Cmdliner open Cmdliner
let term = let term =
let directory = Arg.(value & pos 0 string "" & info [] ~docv:"target directory" let path = Arg.(value & pos 0 string "" & info [] ~docv:"path"
~doc:"Directory to convert") in ~doc:"Text file or directory to convert. Ff directory is provided, it must contain an index.pck (see: txt index)") in
let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"TYPES" let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type"
~doc:"Convert to type") in ~doc:"Convert to file type") in
let noindex = Arg.(value & flag & info ["noindex"] let noindex = Arg.(value & flag & info ["noindex"]
~doc:"don't create indices in target format") in ~doc:"Don't create indices in target format") in
Term.(const convert_dir $ types $ noindex $ directory), Term.(const at_path $ types $ noindex $ path),
Term.info "convert" ~doc:"convert texts" Term.info "convert" ~doc:"convert texts"
~man:[ `S "DESCRIPTION"; `P "Convert texts within a directory to another format. ~man:[ `S "DESCRIPTION"; `P "Convert text or indexed texts within a directory to another format.
Directory must contain an index.pck. Run `txt index` first." ] If path is a directory must contain an index.pck. Run `txt index` first." ]

View File

@ -20,7 +20,7 @@ let publish ids =
with Unix.Unix_error (Unix.EEXIST, _, _) -> (); with Unix.Unix_error (Unix.EEXIST, _, _) -> ();
List.iter (fun t -> List.iter (fun t ->
Index.((load (snd t)) false None None None None); Index.((load (snd t)) false None None None None);
Convert.convert_dir (fst t) false (snd t)) Convert.at_path (fst t) false (snd t))
targets targets
open Cmdliner open Cmdliner