diff --git a/cli/convert.ml b/cli/convert.ml index 4bae97a..55b1f72 100644 --- a/cli/convert.ml +++ b/cli/convert.ml @@ -25,7 +25,7 @@ let converters types kv = Conversion.{ ext = Gemini.ext; page = Gemini.page; indices = Gemini.indices}::t else t in t -let convert_all converters noindex dir id kv = +let directory converters noindex dir id kv = let empty = Topic_set.Map.empty in let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in 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; Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts) -let convert_dir types noindex dir = - match dir with "" -> prerr_endline "unspecified dir" - | dir -> +let at_path types noindex path = + match path with "" -> prerr_endline "unspecified text file or directory" + | dir when Sys.file_exists dir && Sys.is_directory dir -> 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 | Ok { info; _ } -> 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 let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) 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 let term = - let directory = Arg.(value & pos 0 string "" & info [] ~docv:"target directory" - ~doc:"Directory to convert") in - let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"TYPES" - ~doc:"Convert to type") in + let path = Arg.(value & pos 0 string "" & info [] ~docv:"path" + ~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:"output type" + ~doc:"Convert to file type") in let noindex = Arg.(value & flag & info ["noindex"] - ~doc:"don't create indices in target format") in - Term.(const convert_dir $ types $ noindex $ directory), + ~doc:"Don't create indices in target format") in + Term.(const at_path $ types $ noindex $ path), Term.info "convert" ~doc:"convert texts" - ~man:[ `S "DESCRIPTION"; `P "Convert texts within a directory to another format. - Directory must contain an index.pck. Run `txt index` first." ] + ~man:[ `S "DESCRIPTION"; `P "Convert text or indexed texts within a directory to another format. + If path is a directory must contain an index.pck. Run `txt index` first." ] diff --git a/cli/publish.ml b/cli/publish.ml index 0e39ac8..3ac4d5f 100644 --- a/cli/publish.ml +++ b/cli/publish.ml @@ -20,7 +20,7 @@ let publish ids = with Unix.Unix_error (Unix.EEXIST, _, _) -> (); List.iter (fun t -> 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 open Cmdliner