
Signed-off-by: Aoi Koizumi (古泉 あおい) <novaburst@kalli.st> git-svn-id: file:///srv/svn/repo/kosuzu/trunk@10 eb64cd80-c68d-6f47-b6a3-0ada418499da
68 lines
3.0 KiB
OCaml
68 lines
3.0 KiB
OCaml
open Logarion
|
|
|
|
let is_older source dest = try
|
|
Unix.((stat dest).st_mtime < (stat source).st_mtime) with _-> true
|
|
|
|
let convert cs r (text, files) = match Text.str "Content-Type" text with
|
|
| "" | "text/plain" ->
|
|
let source = List.hd files in
|
|
let dest = Filename.concat r.Conversion.dir (Text.short_id text) in
|
|
List.fold_left
|
|
(fun a f ->
|
|
let dest = dest ^ f.Conversion.ext in
|
|
if is_older source dest then (File_store.file dest (f.Conversion.page r text); true) else false
|
|
|| a)
|
|
false cs
|
|
| x -> Printf.eprintf "Can't convert Content-Type: %s file: %s" x text.Text.title; false
|
|
|
|
let converters types kv =
|
|
let t = [] in
|
|
let t = if ("htm" = types || "all" = types) then
|
|
(let htm = Html.init kv in
|
|
Conversion.{ ext = Html.ext; page = Html.page htm; indices = Html.indices htm })::t
|
|
else t in
|
|
let t = if ("gmi" = types || "all" = types) then
|
|
Conversion.{ ext = Gemini.ext; page = Gemini.page; indices = Gemini.indices}::t else t in
|
|
t
|
|
|
|
let convert_all 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) =
|
|
(Topic_set.to_map ts (Text.set "topics" elt)), elt::ls,
|
|
if convert converters repo r then acc+1 else acc in
|
|
let topics, texts, count = File_store.(fold ~dir ~order:newest fn (empty,[],0)) in
|
|
let topic_roots = try List.rev @@ String_set.list_of_csv (Store.KV.find "Topics" kv)
|
|
with Not_found -> Topic_set.roots topics in
|
|
let repo = Conversion.{ repo with topic_roots; topics; texts } in
|
|
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 fname = Filename.concat dir "index.pck" in
|
|
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? *)
|
|
if Sys.file_exists f then File_store.of_kv_file f else Store.KV.empty in
|
|
let kv = if Store.KV.mem "Title" kv then kv
|
|
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
|
|
|
|
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 noindex = Arg.(value & flag & info ["noindex"]
|
|
~doc:"don't create indices in target format") in
|
|
Term.(const convert_dir $ types $ noindex $ directory),
|
|
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." ]
|