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