diff --git a/cli/convert.ml b/cli/convert.ml index 1f12fd9..a7971c3 100644 --- a/cli/convert.ml +++ b/cli/convert.ml @@ -39,8 +39,7 @@ let directory converters noindex repo = Printf.printf "Converted: %d Indexed: %d\n" count (List.length texts) let load_kv dir = - let conf = Filename.concat dir ".convert.conf" in (* TODO: better name? *) - let kv = if Sys.file_exists conf then File_store.of_kv_file conf else Store.KV.empty in + let kv = File_store.of_kv_file () in let idx = Filename.concat dir "index.pck" in if not (Sys.file_exists idx) then kv else match Header_pack.of_string @@ File_store.to_string (idx) with @@ -48,7 +47,7 @@ let load_kv dir = | Ok { info; peers; _ } -> let kv = if Store.KV.mem "Id" kv then kv else Store.KV.add "Id" info.Header_pack.id kv 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 kv = if Store.KV.mem "Locations" kv then kv else Store.KV.add "Locations" (String.concat ";\n" info.Header_pack.locations) kv in let kv = Store.KV.add "Peers" (String.concat ";\n" Header_pack.(to_str_list peers)) kv in kv diff --git a/cli/html.ml b/cli/html.ml index 1aa6e5d..3b7c232 100644 --- a/cli/html.ml +++ b/cli/html.ml @@ -27,7 +27,7 @@ let wrap conv htm text_title body = then "feed.atom" else "" in let header = match htm.templates.header with | Some x -> replace x - | None -> Printf.(sprintf "
%s
%s" site_title + | None -> Printf.(sprintf "
%s%s
" site_title (if feed <> "" then sprintf "" feed else "")) in let footer = match htm.templates.footer with None -> "" | Some x -> replace x in diff --git a/cli/publish.ml b/cli/publish.ml index 3ac4d5f..583e6b6 100644 --- a/cli/publish.ml +++ b/cli/publish.ml @@ -1,26 +1,28 @@ let targets () = - let home = - try Sys.getenv "txtpubdir" with Not_found -> - try Sys.getenv "HOME" with Not_found -> "" + let kv = Logarion.File_store.of_kv_file () in + let pub_dir = + try Logarion.Store.KV.find "Pubdir" kv with Not_found -> + try Sys.getenv "txtpubdir" with Not_found -> "" in - List.filter - (fun x -> try Sys.is_directory (snd x) with Sys_error _ -> false) - [ - "htm", home ^ "/public_html/txt"; - "gmi", home ^ "/public_gemini/txt"; - "", home ^ "/public_gopher/txt"; - ] + let exists_dir dir = Sys.is_directory (Filename.concat pub_dir dir) in + List.filter (fun x -> try exists_dir (snd x) with Sys_error _ -> false) [ + "htm,atom", "public_html/"; + "gmi,gmi-atom", "public_gemini/"; + "", "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 -> + File_store.iter ~predicate (fun (_t, p) -> + try File.file ((List.hd p)::pub_dirs) + with Unix.Unix_error (Unix.EEXIST, _, _) -> ()); + List.iter (fun t -> Printf.eprintf "%s %s\n" (fst t) (snd t); Index.((load (snd t)) false None None None None); - Convert.at_path (fst t) false (snd t)) + Convert.at_path (fst t) false (snd t); + prerr_endline (snd t)) targets open Cmdliner diff --git a/lib/file_store.ml b/lib/file_store.ml index 707c970..e8a5616 100644 --- a/lib/file_store.ml +++ b/lib/file_store.ml @@ -3,11 +3,18 @@ type item_t = t list type record_t = Text.t * item_t let extension = ".txt" -let def_dir () = try Sys.getenv "txtdir" with Not_found -> + +let txtdir () = try Sys.getenv "txtdir" with Not_found -> let share = Filename.concat (Sys.getenv "HOME") ".local/share/texts/" in match Sys.is_directory share with true -> share | false | exception (Sys_error _) -> "." +let cfgpath () = match "txt.conf" with + | filepath when Sys.file_exists filepath -> filepath + | _ -> match Filename.concat (Sys.getenv "HOME") ".config/txt/txt.conf" with + | filepath when Sys.file_exists filepath -> filepath + | _ -> "" + let to_string f = let ic = open_in f in let s = really_input_string ic (in_channel_length ic) in @@ -80,13 +87,13 @@ 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=txtdir ()) ?(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=txtdir ()) ?(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 @@ @@ -121,7 +128,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=txtdir ()) new_text = match id_filename dir extension new_text with | Error _ as e -> e | Ok path -> @@ -133,10 +140,11 @@ module Config = struct let key_value k v a = Store.KV.add k (String.trim v) a end -let of_kv_file path = +let of_kv_file ?(path=cfgpath ()) () = let open Text_parse in let subsyntaxes = Parsers.Key_value.[| (module Make (Config) : Parser.S with type t = Config.t); (module Make (Config)); |] in let of_string text acc = Parser.parse subsyntaxes { text; pos = 0; right_boundary = String.length text - 1 } acc in - of_string (to_string @@ path) Store.KV.empty + if path <> "" then of_string (to_string @@ path) Store.KV.empty + else Store.KV.empty diff --git a/lib/peers.ml b/lib/peers.ml index 513dc8c..1fc5f33 100644 --- a/lib/peers.ml +++ b/lib/peers.ml @@ -1,4 +1,4 @@ -let text_dir = Filename.concat (File_store.def_dir ()) "peers" +let text_dir = Filename.concat (File_store.txtdir ()) "peers" let fold fn init = match Sys.readdir text_dir with | exception (Sys_error msg) -> prerr_endline msg