txt publish <id>; file and convert to standard dirs

This commit is contained in:
orbifx 2022-06-16 22:59:30 +01:00
parent 6689127d06
commit d978b5fc3a
5 changed files with 49 additions and 13 deletions

View File

@ -1,5 +1,5 @@
(executable
(name txt)
(public_name txt)
(modules txt authors convert conversion file index last listing new topics html atom gemini pull)
(modules txt authors convert conversion file index last listing new topics html atom gemini publish pull)
(libraries text_parse.converter text_parse.parsers logarion msgpck curl str cmdliner))

View File

@ -1,11 +1,6 @@
let split_filetypes files =
let acc (dirs, files) x = if Sys.is_directory x
then (x::dirs, files) else (dirs, x::files) in
List.fold_left acc ([],[]) files
open Logarion
let file files =
let dirs, files = split_filetypes files in
let dirs, files = File_store.split_filetypes files in
let _link_as_named dir file = Unix.link file (Filename.concat dir file) in
let link_with_id dir file =
match File_store.to_text file with Error s -> prerr_endline s
@ -14,7 +9,7 @@ let file files =
List.iter (fun d -> List.iter (link d) files) dirs
let unfile files =
let dirs, files = split_filetypes files in
let dirs, files = File_store.split_filetypes files in
let unlink dir file = try Unix.unlink (Filename.concat dir file)
with Unix.(Unix_error(ENOENT,_,_))-> () in
List.iter (fun d -> List.iter (unlink d) files) dirs

30
cli/publish.ml Normal file
View File

@ -0,0 +1,30 @@
let targets () =
let home =
try Sys.getenv "txtpubdir" with Not_found ->
try Sys.getenv "HOME" with Not_found -> ""
in
List.filter
(fun x -> try Sys.is_directory (snd x) with Sys_error _ -> false)
[
"htm", home ^ "/public/html";
"gmi", home ^ "/public/gemini";
"", home ^ "/public/gopher";
]
open Logarion
let publish ids =
let predicate t = List.mem t.Text.id ids in
let targets = targets () in
let pub_dirs = List.map (fun x -> snd x) targets in
try File_store.iter ~predicate (fun (_t, p) -> File.file ((List.hd p)::pub_dirs))
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))
targets
open Cmdliner
let term =
let ids = Arg.(value & pos_all string [] & info [] ~docv:"text ids") in
let doc = "convert texts into standart public dirs public_{html,gemini,gopher} if they exists" in
Term.(const publish $ ids), Term.info "publish" ~doc ~man:[ `S "DESCRIPTION"; `P doc ]

View File

@ -14,6 +14,7 @@ let () = match Term.eval_choice default_cmd [
Last.term;
Listing.term;
New.term;
Publish.term;
Pull.term;
Topics.term;
] with `Error _ -> exit 1 | _ -> exit 0

View File

@ -3,7 +3,12 @@ type item_t = t list
type record_t = Text.t * item_t
let extension = ".txt"
let def_dir = try Sys.getenv "LOGARION_DIR" with Not_found -> "."
let def_dir () =
let share = Sys.getenv "HOME" ^ "/.local/share/texts/" in
try Sys.getenv "txtdir" with Not_found ->
match Sys.is_directory share with
| true -> share
| false | exception (Sys_error _) -> "."
let to_string f =
let ic = open_in f in
@ -49,6 +54,11 @@ let fold_valid_text pred it path =
(function None -> Some [path] | Some ps -> Some (path::ps)) it
) else it
let split_filetypes files =
let acc (dirs, files) x = if Sys.is_directory x
then (x::dirs, files) else (dirs, x::files) in
List.fold_left acc ([],[]) files
(* Compare file system nodes to skip reparsing? *)
let list_fs ?(r=false) dir =
let valid_dir f = r && String.get f 0 <> '.' && Sys.is_directory f in
@ -72,19 +82,19 @@ let fold_sort_take ?(predicate=fun _ -> true) ?(number=None) comp flist =
@@ List.fast_sort comp @@ TextMap.bindings
@@ List.fold_left (fold_valid_text predicate) new_iteration flist
let iter ?(r=false) ?(dir=def_dir) ?(predicate=fun _ -> true) ?order ?number fn =
let iter ?(r=false) ?(dir=def_dir ()) ?(predicate=fun _ -> true) ?order ?number fn =
let flist = list_fs ~r dir in match order with
| Some comp -> List.iter fn @@ fold_sort_take ~predicate ~number comp flist
| None -> List.iter fn @@ TextMap.bindings @@
List.fold_left (fold_valid_text predicate) new_iteration flist
let fold ?(r=false) ?(dir=def_dir) ?(predicate=fun _ -> true) ?order ?number fn acc =
let fold ?(r=false) ?(dir=def_dir ()) ?(predicate=fun _ -> true) ?order ?number fn acc =
let flist = list_fs ~r dir in match order with
| Some comp -> List.fold_left fn acc @@ fold_sort_take ~predicate ~number comp flist
| None -> List.fold_left fn acc @@ TextMap.bindings @@
List.fold_left (fold_valid_text predicate) new_iteration flist
let with_id ?(r=false) ?(dir=def_dir) id =
let with_id ?(r=false) ?(dir=def_dir ()) id =
let matched acc path =
match to_text path with
| Error x -> prerr_endline x; acc
@ -125,7 +135,7 @@ let id_filename repo extension text =
let candidate = Filename.concat repo (text.id ^ "." ^ basename ^ extension) in
if Sys.file_exists candidate then Error "Name clash, try again" else Ok candidate
let with_text ?(dir=def_dir) new_text =
let with_text ?(dir=def_dir ()) new_text =
match id_filename dir extension new_text with
| Error _ as e -> e
| Ok path ->