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