[2] | 1 | type t = string
|
---|
[3] | 2 | type item_t = t list
|
---|
[2] | 3 | type record_t = Text.t * item_t
|
---|
| 4 |
|
---|
| 5 | let extension = ".txt"
|
---|
[21] | 6 |
|
---|
| 7 | let txtdir () = try Sys.getenv "txtdir" with Not_found ->
|
---|
[16] | 8 | let share = Filename.concat (Sys.getenv "HOME") ".local/share/texts/" in
|
---|
| 9 | match Sys.is_directory share with true -> share
|
---|
| 10 | | false | exception (Sys_error _) -> "."
|
---|
[2] | 11 |
|
---|
[21] | 12 | let cfgpath () = match "txt.conf" with
|
---|
| 13 | | filepath when Sys.file_exists filepath -> filepath
|
---|
| 14 | | _ -> match Filename.concat (Sys.getenv "HOME") ".config/txt/txt.conf" with
|
---|
| 15 | | filepath when Sys.file_exists filepath -> filepath
|
---|
| 16 | | _ -> ""
|
---|
| 17 |
|
---|
[2] | 18 | let to_string f =
|
---|
| 19 | let ic = open_in f in
|
---|
[3] | 20 | let s = really_input_string ic (in_channel_length ic) in
|
---|
[2] | 21 | close_in ic;
|
---|
[3] | 22 | s
|
---|
[2] | 23 |
|
---|
[3] | 24 | let fold_file_line fn init file = match open_in file with
|
---|
| 25 | | exception (Sys_error msg) -> prerr_endline msg; init
|
---|
| 26 | | file ->
|
---|
| 27 | let rec read acc = match input_line file with
|
---|
| 28 | | "" as s | s when String.get s 0 = '#' -> read acc
|
---|
| 29 | | s -> read (fn s acc)
|
---|
| 30 | | exception End_of_file -> close_in file; acc
|
---|
| 31 | in read init
|
---|
[2] | 32 |
|
---|
[3] | 33 | let file path str = let o = open_out path in output_string o str; close_out o
|
---|
[2] | 34 |
|
---|
| 35 | let to_text path =
|
---|
| 36 | if Filename.extension path = extension then
|
---|
| 37 | (to_string path |> Text.of_string |> Result.map_error (fun m -> path ^": "^ m))
|
---|
[3] | 38 | else Error (Printf.sprintf "Not txt: %s" path)
|
---|
[2] | 39 |
|
---|
| 40 | let newest (a,_pa) (b,_pb) = Text.newest a b
|
---|
| 41 | let oldest (a,_pa) (b,_pb) = Text.oldest a b
|
---|
| 42 |
|
---|
[3] | 43 | let list_iter fn dir paths =
|
---|
| 44 | let link f = match to_text (Filename.concat dir f) with
|
---|
| 45 | | Ok t -> fn dir t f | Error s -> prerr_endline s in
|
---|
[2] | 46 | List.iter link paths
|
---|
| 47 |
|
---|
[3] | 48 | module TextMap = Map.Make(Text)
|
---|
[2] | 49 |
|
---|
[3] | 50 | type iteration_t = item_t TextMap.t
|
---|
| 51 | let new_iteration = TextMap.empty
|
---|
[2] | 52 |
|
---|
[3] | 53 | (*let iter_valid_text pred fn path =*)
|
---|
| 54 | (* match to_text path with Error _ -> () | Ok t -> if pred t then fn (t, p)*)
|
---|
| 55 |
|
---|
| 56 | let fold_valid_text pred it path =
|
---|
| 57 | match to_text path with Error _ -> it
|
---|
| 58 | | Ok t -> if pred t then (TextMap.update t
|
---|
| 59 | (function None -> Some [path] | Some ps -> Some (path::ps)) it
|
---|
| 60 | ) else it
|
---|
| 61 |
|
---|
[6] | 62 | let split_filetypes files =
|
---|
| 63 | let acc (dirs, files) x = if Sys.is_directory x
|
---|
| 64 | then (x::dirs, files) else (dirs, x::files) in
|
---|
| 65 | List.fold_left acc ([],[]) files
|
---|
| 66 |
|
---|
[3] | 67 | (* Compare file system nodes to skip reparsing? *)
|
---|
| 68 | let list_fs ?(r=false) dir =
|
---|
| 69 | let valid_dir f = r && String.get f 0 <> '.' && Sys.is_directory f in
|
---|
| 70 | let expand_dir d = Array.(to_list @@ map (Filename.concat d) (Sys.readdir d)) in
|
---|
[2] | 71 | let rec loop result = function
|
---|
[3] | 72 | | f::fs when valid_dir f -> expand_dir f |> List.append fs |> loop result
|
---|
[2] | 73 | | f::fs -> loop (f::result) fs
|
---|
[3] | 74 | | [] -> result in
|
---|
| 75 | let dirs = if dir = "." then Array.to_list (Sys.readdir dir) else
|
---|
| 76 | if not r then expand_dir dir else [dir] in
|
---|
| 77 | loop [] dirs
|
---|
[2] | 78 |
|
---|
| 79 | let list_take n =
|
---|
| 80 | let rec take acc n = function [] -> []
|
---|
| 81 | | x::_ when n = 1 -> x::acc
|
---|
| 82 | | x::xs -> take (x::acc) (n-1) xs
|
---|
| 83 | in take [] n
|
---|
| 84 |
|
---|
[3] | 85 | let fold_sort_take ?(predicate=fun _ -> true) ?(number=None) comp flist =
|
---|
| 86 | (match number with None -> (fun x -> x) | Some n -> list_take n)
|
---|
| 87 | @@ List.fast_sort comp @@ TextMap.bindings
|
---|
| 88 | @@ List.fold_left (fold_valid_text predicate) new_iteration flist
|
---|
[2] | 89 |
|
---|
[21] | 90 | let iter ?(r=false) ?(dir=txtdir ()) ?(predicate=fun _ -> true) ?order ?number fn =
|
---|
[3] | 91 | let flist = list_fs ~r dir in match order with
|
---|
| 92 | | Some comp -> List.iter fn @@ fold_sort_take ~predicate ~number comp flist
|
---|
| 93 | | None -> List.iter fn @@ TextMap.bindings @@
|
---|
| 94 | List.fold_left (fold_valid_text predicate) new_iteration flist
|
---|
[2] | 95 |
|
---|
[21] | 96 | let fold ?(r=false) ?(dir=txtdir ()) ?(predicate=fun _ -> true) ?order ?number fn acc =
|
---|
[3] | 97 | let flist = list_fs ~r dir in match order with
|
---|
| 98 | | Some comp -> List.fold_left fn acc @@ fold_sort_take ~predicate ~number comp flist
|
---|
| 99 | | None -> List.fold_left fn acc @@ TextMap.bindings @@
|
---|
| 100 | List.fold_left (fold_valid_text predicate) new_iteration flist
|
---|
| 101 |
|
---|
| 102 | let with_dir ?(descr="") ?(perm=0o740) dir =
|
---|
| 103 | let mkdir dir = match Unix.mkdir dir perm with
|
---|
| 104 | | exception Unix.Unix_error (EEXIST, _, _) -> ()
|
---|
| 105 | | exception Unix.Unix_error (code, _fn, arg) ->
|
---|
| 106 | failwith @@ Printf.sprintf "Error %s making %s dir: %s"
|
---|
| 107 | (Unix.error_message code) descr arg
|
---|
| 108 | | _ -> () in
|
---|
| 109 | let rec mkeach path = function [] | [""] -> () | ""::t -> mkeach path t
|
---|
| 110 | | hd::t -> let d = Filename.concat path hd in mkdir d; mkeach d t in
|
---|
| 111 | mkeach
|
---|
| 112 | (if Filename.is_relative dir then "" else "/")
|
---|
| 113 | (String.split_on_char '/' dir)
|
---|
[2] | 114 |
|
---|
[3] | 115 | let rec with_dirs = function [] -> () | (d, descr)::tl -> with_dir ~descr d; with_dirs tl
|
---|
[2] | 116 |
|
---|
| 117 | let versioned_basename_of_title ?(version=0) repo extension (title : string) =
|
---|
| 118 | let basename = Text.string_alias title in
|
---|
| 119 | let rec next version =
|
---|
[3] | 120 | let candidate = Filename.concat repo
|
---|
| 121 | (basename ^ "." ^ string_of_int version ^ extension) in
|
---|
[2] | 122 | if Sys.file_exists candidate then next (succ version) else candidate
|
---|
| 123 | in
|
---|
| 124 | next version
|
---|
| 125 |
|
---|
[3] | 126 | let id_filename repo extension text =
|
---|
[32] | 127 | let description = match Text.alias text with "" -> "" | x -> "." ^ x in
|
---|
| 128 | let candidate = Filename.concat repo (text.id ^ description ^ extension) in
|
---|
[2] | 129 | if Sys.file_exists candidate then Error "Name clash, try again" else Ok candidate
|
---|
| 130 |
|
---|
[21] | 131 | let with_text ?(dir=txtdir ()) new_text =
|
---|
[3] | 132 | match id_filename dir extension new_text with
|
---|
| 133 | | Error _ as e -> e
|
---|
| 134 | | Ok path ->
|
---|
| 135 | try file path (Text.to_string new_text); Ok (path, new_text)
|
---|
| 136 | with Sys_error s -> Error s
|
---|
[2] | 137 |
|
---|
| 138 | module Config = struct
|
---|
[3] | 139 | type t = string Store.KV.t
|
---|
| 140 | let key_value k v a = Store.KV.add k (String.trim v) a
|
---|
[2] | 141 | end
|
---|
| 142 |
|
---|
[21] | 143 | let of_kv_file ?(path=cfgpath ()) () =
|
---|
[2] | 144 | let open Text_parse in
|
---|
[3] | 145 | let subsyntaxes = Parsers.Key_value.[|
|
---|
| 146 | (module Make (Config) : Parser.S with type t = Config.t); (module Make (Config)); |] in
|
---|
| 147 | let of_string text acc =
|
---|
| 148 | Parser.parse subsyntaxes { text; pos = 0; right_boundary = String.length text - 1 } acc in
|
---|
[21] | 149 | if path <> "" then of_string (to_string @@ path) Store.KV.empty
|
---|
| 150 | else Store.KV.empty
|
---|