Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 9d2fc4a283a972448a1727c9612ac021ebd9473c
Timestamp: Fri, 09 Jul 2021 19:49:51 +0000 (3 years ago)

+48 -42 +/-4 browse
move tree into module
1diff --git a/bin/note.ml b/bin/note.ml
2index fb08fc7..9f8ce33 100644
3--- a/bin/note.ml
4+++ b/bin/note.ml
5 @@ -37,17 +37,17 @@ module Display = struct
6 noop )))
7
8 let rec convert_tree tree =
9- let (Note.Tree (note, others)) = tree in
10+ let (Note.Tree.Tree (note, others)) = tree in
11 let title =
12 "[" ^ ((note |> Note.frontmatter).path |> Filename.basename) ^ "]"
13 in
14 Hierarchical.Tree (title, List.map ~f:convert_tree others)
15
16 let convert_rows ~columns tree : row list =
17- let (Note.Tree (_, others)) = tree in
18+ let (Note.Tree.Tree (_, others)) = tree in
19 others
20 |> List.map ~f:(fun other ->
21- let (Note.Tree (note, _)) = other in
22+ let (Note.Tree.Tree (note, _)) = other in
23 note)
24 |> to_rows ~columns
25
26 @@ -143,7 +143,7 @@ is provided then all notes will be listed.
27 paths
28 |> List.map ~f:(fun path -> options |> Note.load ~path)
29 |> List.iter ~f:(fun notes ->
30- let note = notes |> Note.fst in
31+ let note = notes |> Note.Tree.fst in
32 note |> Note.to_string |> print_endline)]
33
34 let create_note =
35 diff --git a/lib/note.ml b/lib/note.ml
36index dd75a78..6be04a2 100644
37--- a/lib/note.ml
38+++ b/lib/note.ml
39 @@ -43,16 +43,10 @@ end
40
41 type t = { frontmatter : Frontmatter.t; content : string }
42
43- and tree = Tree of (t * tree list)
44-
45 let frontmatter note = note.frontmatter
46
47 let content note = note.content
48
49- let fst tree =
50- let (Tree (note, _)) = tree in
51- note
52-
53 let root_template =
54 {|
55 ---
56 @@ -64,10 +58,6 @@ tags: []
57 # This is a Note!
58 |}
59
60- let rec flatten ?(accm = []) tree =
61- let (Tree (note, others)) = tree in
62- List.fold ~init:(note :: accm) ~f:(fun accm note -> flatten ~accm note) others
63-
64 let rec extract_structured_data (accm : Ezjsonm.value list) (doc : Omd.doc) :
65 Ezjsonm.value list =
66 match doc with
67 @@ -118,21 +108,35 @@ let of_string ?(path = None) content =
68 { frontmatter; content }
69 else { frontmatter = Frontmatter.empty; content }
70
71- let rec resolve_manifest ~path manifest =
72- let items =
73- match manifest |> Manifest.list ~path with
74- | [] -> []
75- | items ->
76- items
77- |> List.map ~f:(fun item ->
78- let path = item.path in
79- let slug = item.slug |> Slug.to_string in
80- let note =
81- In_channel.read_all slug |> of_string ~path:(Some path)
82- in
83- Tree (note, manifest |> resolve_manifest ~path))
84- in
85- items
86+ module Tree = struct
87+ type tree = Tree of (t * tree list)
88+
89+ let rec flatten ?(accm = []) tree =
90+ let (Tree (note, others)) = tree in
91+ List.fold ~init:(note :: accm)
92+ ~f:(fun accm note -> flatten ~accm note)
93+ others
94+
95+ let fst tree =
96+ let (Tree (note, _)) = tree in
97+ note
98+
99+ let rec resolve_manifest ~path manifest =
100+ let items =
101+ match manifest |> Manifest.list ~path with
102+ | [] -> []
103+ | items ->
104+ items
105+ |> List.map ~f:(fun item ->
106+ let path = item.path in
107+ let slug = item.slug |> Slug.to_string in
108+ let note =
109+ In_channel.read_all slug |> of_string ~path:(Some path)
110+ in
111+ Tree (note, manifest |> resolve_manifest ~path))
112+ in
113+ items
114+ end
115
116 (* high level adapter *)
117 module Adapter = struct
118 @@ -166,7 +170,7 @@ module Adapter = struct
119 root
120 | _ -> failwith "not found")
121 in
122- Tree (root, manifest |> resolve_manifest ~path)
123+ Tree.Tree (root, manifest |> Tree.resolve_manifest ~path)
124
125 let find ~path options =
126 let manifest = options.state_dir |> Manifest.load_or_init in
127 diff --git a/lib/note.mli b/lib/note.mli
128index 6b680bc..d811946 100644
129--- a/lib/note.mli
130+++ b/lib/note.mli
131 @@ -6,12 +6,6 @@ end
132 type t
133 (* a note represented as a tuple of frontmatter and raw text content *)
134
135- type tree = Tree of (t * tree list)
136- (* notes arranged in a hierarchical structure *)
137-
138- val fst : tree -> t
139- (* return the top level note of a given tree *)
140-
141 val to_string : t -> string
142 (* return a note with frontmatter and content *)
143
144 @@ -27,8 +21,16 @@ val frontmatter : t -> Frontmatter.t
145 val content : t -> string
146 (* get the raw text content without frontmatter heading *)
147
148- val flatten : ?accm:t list -> tree -> t list
149- (* flatten a tree into a list of notes *)
150+ module Tree : sig
151+ type tree = Tree of (t * tree list)
152+ (* notes stored in a b-tree like data structure *)
153+
154+ val fst : tree -> t
155+ (* return the top level note of a given tree *)
156+
157+ val flatten : ?accm:t list -> tree -> t list
158+ (* flatten a tree into a list of notes *)
159+ end
160
161 (*
162 * high level adapter options for interaction from the CLI
163 @@ -40,7 +42,7 @@ type options = {
164 }
165 (* runtime options for interacting with the filesystem and manifest document*)
166
167- val load : path:string -> options -> tree
168+ val load : path:string -> options -> Tree.tree
169 (* load all notes below the given path *)
170
171 val find : path:string -> options -> t option
172 diff --git a/test/note_test.ml b/test/note_test.ml
173index ea4270e..db2edb4 100644
174--- a/test/note_test.ml
175+++ b/test/note_test.ml
176 @@ -31,17 +31,17 @@ let adapter () =
177 let tree = options |> Note.load ~path:"/" in
178 Alcotest.(check int)
179 "initialized" 1
180- (tree |> Note.flatten ~accm:[] |> List.length);
181+ (tree |> Note.Tree.flatten ~accm:[] |> List.length);
182 options |> Note.create ~content:(Some "bar") ~path:"/fuu";
183 let tree = options |> Note.load ~path:"/" in
184 Alcotest.(check int)
185 "note added" 2
186- (tree |> Note.flatten ~accm:[] |> List.length);
187+ (tree |> Note.Tree.flatten ~accm:[] |> List.length);
188 options |> Note.remove ~path:"/fuu";
189 let tree = options |> Note.load ~path:"/" in
190 Alcotest.(check int)
191 "note removed" 1
192- (tree |> Note.flatten ~accm:[] |> List.length)
193+ (tree |> Note.Tree.flatten ~accm:[] |> List.length)
194
195 let suggest_path () =
196 let options : Note.options =