Changeset 13 in code


Ignore:
Timestamp:
Jul 30, 2022, 12:58:02 PM (3 years ago)
Author:
fox
Message:

Accept files & directories for conversion

Location:
trunk/cli
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/convert.ml

    r10 r13  
    2626        t
    2727
    28 let convert_all converters noindex dir id kv =
     28let directory converters noindex dir id kv =
    2929        let empty = Topic_set.Map.empty in
    3030        let repo = Conversion.{ id; dir; kv; topic_roots = []; topics = empty; texts = [] } in
     
    3939        Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts)
    4040
    41 let convert_dir types noindex dir =
    42         match dir with "" -> prerr_endline "unspecified dir"
    43         | dir ->
     41let at_path types noindex path =
     42        match path with "" -> prerr_endline "unspecified text file or directory"
     43        | dir when Sys.file_exists dir && Sys.is_directory dir ->
    4444                let fname = Filename.concat dir "index.pck" in
    45                 match Header_pack.of_string @@ File_store.to_string fname with
     45                (match Header_pack.of_string @@ File_store.to_string fname with
    4646                | Error s -> prerr_endline s
    4747                | Ok { info; _ } ->
     
    5252                        let kv = Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in
    5353                        let cs = converters types kv in
    54                         convert_all cs noindex dir info.Header_pack.id kv
     54                        directory cs noindex dir info.Header_pack.id kv)
     55        | path when Sys.file_exists path ->
     56                let repo = Conversion.{
     57                        id = ""; dir = ""; kv = Store.KV.empty; topic_roots = [];
     58                        topics = Topic_set.Map.empty; texts = [] } in
     59                let cs = converters types repo.kv in
     60                (match File_store.to_text path with
     61                | Ok text -> ignore @@ convert cs repo (text, [path])
     62                | Error s -> prerr_endline s)
     63        | path -> Printf.eprintf "Path doesn't exist: %s" path
    5564
    5665open Cmdliner
    5766let term =
    58         let directory = Arg.(value & pos 0 string "" & info [] ~docv:"target directory"
    59                 ~doc:"Directory to convert") in
    60         let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"TYPES"
    61          ~doc:"Convert to type") in
     67        let path = Arg.(value & pos 0 string "" & info [] ~docv:"path"
     68                ~doc:"Text file or directory to convert. Ff directory is provided, it must contain an index.pck (see: txt index)") in
     69        let types = Arg.(value & opt string "all" & info ["t"; "type"] ~docv:"output type"
     70                ~doc:"Convert to file type") in
    6271        let noindex = Arg.(value & flag & info ["noindex"]
    63                 ~doc:"don't create indices in target format") in
    64         Term.(const convert_dir $ types $ noindex $ directory),
     72                ~doc:"Don't create indices in target format") in
     73        Term.(const at_path $ types $ noindex $ path),
    6574        Term.info "convert" ~doc:"convert texts"
    66                 ~man:[ `S "DESCRIPTION"; `P "Convert texts within a directory to another format.
    67                 Directory must contain an index.pck. Run `txt index` first." ]
     75                ~man:[ `S "DESCRIPTION"; `P "Convert text or indexed texts within a directory to another format.
     76                If path is a directory must contain an index.pck. Run `txt index` first." ]
  • trunk/cli/publish.ml

    r12 r13  
    2121        List.iter (fun t ->
    2222                Index.((load (snd t)) false None None None None);
    23                 Convert.convert_dir (fst t) false (snd t))
     23                Convert.at_path (fst t) false (snd t))
    2424                targets
    2525
Note: See TracChangeset for help on using the changeset viewer.