Commit
+52 -4 +/-3 browse
1 | diff --git a/bin/note.ml b/bin/note.ml |
2 | index 9f8ce33..e6a9031 100644 |
3 | --- a/bin/note.ml |
4 | +++ b/bin/note.ml |
5 | @@ -70,6 +70,23 @@ module Display = struct |
6 | |> List.iter ~f:(fun row -> print_endline row) |
7 | end |
8 | |
9 | + module Encoding = struct |
10 | + let to_stdout ~encoding tree = |
11 | + match encoding with |
12 | + | `Raw -> |
13 | + let note = tree |> Note.Tree.fst in |
14 | + note |> Note.to_string |> print_endline |
15 | + | `Json -> |
16 | + tree |> Note.Tree.to_json |> Ezjsonm.wrap |> Ezjsonm.to_string |
17 | + |> print_endline |
18 | + | `Yaml -> |
19 | + tree |> Note.Tree.to_json |> Ezjsonm.wrap |> Yaml.to_string_exn |
20 | + |> print_endline |
21 | + | `Html -> |
22 | + let note = tree |> Note.Tree.fst in |
23 | + note |> Note.to_html |> print_endline |
24 | + end |
25 | + |
26 | module Args = struct |
27 | let list_style = |
28 | let styles = |
29 | @@ -94,6 +111,17 @@ module Args = struct |
30 | options |> Note.Completion.suggest_tags ~hint:part) |
31 | (fun filter -> filter) |
32 | |
33 | + let encoding = |
34 | + let encodings = |
35 | + Config.Encoding.all |> List.map ~f:Config.Encoding.to_string |
36 | + in |
37 | + Command.Arg_type.create |
38 | + ~complete:(fun _ ~part -> |
39 | + encodings |
40 | + |> List.filter ~f:(fun encoding -> |
41 | + String.is_substring ~substring:part encoding)) |
42 | + Config.Encoding.of_string |
43 | + |
44 | let config_key = |
45 | let keys = List.map ~f:Config.Key.to_string Config.Key.all in |
46 | Command.Arg_type.create |
47 | @@ -137,14 +165,16 @@ List one or more notes that match the filter criteria, if no filter criteria |
48 | is provided then all notes will be listed. |
49 | |}) |
50 | [%map_open |
51 | - let paths = anon (sequence ("path" %: Args.path)) in |
52 | + let paths = anon (sequence ("path" %: Args.path)) |
53 | + and encoding = flag "encoding" (optional Args.encoding) ~doc:"encoding" in |
54 | fun () -> |
55 | let paths = match paths with [] -> [ "/" ] | paths -> paths in |
56 | + let encoding = |
57 | + match encoding with Some encoding -> encoding | None -> `Raw |
58 | + in |
59 | paths |
60 | |> List.map ~f:(fun path -> options |> Note.load ~path) |
61 | - |> List.iter ~f:(fun notes -> |
62 | - let note = notes |> Note.Tree.fst in |
63 | - note |> Note.to_string |> print_endline)] |
64 | + |> List.iter ~f:(Encoding.to_stdout ~encoding)] |
65 | |
66 | let create_note = |
67 | let open Command.Let_syntax in |
68 | diff --git a/lib/note.ml b/lib/note.ml |
69 | index 7443ba5..f5e53d7 100644 |
70 | --- a/lib/note.ml |
71 | +++ b/lib/note.ml |
72 | @@ -87,6 +87,9 @@ let to_json note = |
73 | ("data", data); |
74 | ] |
75 | |
76 | + let to_html note = |
77 | + note.content |> Omd.of_string |> Omd.to_html |
78 | + |
79 | let to_string note = |
80 | let yaml = Yaml.to_string_exn (Frontmatter.to_json note.frontmatter) in |
81 | "\n---\n" ^ yaml ^ "\n---\n" ^ note.content |
82 | @@ -124,6 +127,15 @@ module Tree = struct |
83 | let (Tree (note, _)) = tree in |
84 | note |
85 | |
86 | + let note_to_json = to_json |
87 | + |
88 | + let rec to_json tree = |
89 | + let (Tree (root, others)) = tree in |
90 | + Ezjsonm.dict [ |
91 | + ("note", (root |> note_to_json)) ; |
92 | + ("descendants", others |> List.map ~f:to_json |> Ezjsonm.list (fun a -> a)) |
93 | + ] |
94 | + |
95 | let rec resolve_manifest ~path manifest = |
96 | match manifest |> Manifest.list ~path with |
97 | | [] -> [] |
98 | diff --git a/lib/note.mli b/lib/note.mli |
99 | index 33b631d..0108019 100644 |
100 | --- a/lib/note.mli |
101 | +++ b/lib/note.mli |
102 | @@ -15,6 +15,9 @@ val of_string : ?path:string option -> string -> t |
103 | val to_json : t -> Ezjsonm.value |
104 | (* get a note as json data with structured data extracted from it *) |
105 | |
106 | + val to_html : t -> string |
107 | + (* return the html form of a note *) |
108 | + |
109 | val frontmatter : t -> Frontmatter.t |
110 | (* get decoded frontmatter structure *) |
111 | |
112 | @@ -30,6 +33,9 @@ module Tree : sig |
113 | |
114 | val flatten : tree -> t list |
115 | (* flatten a tree into a list of notes *) |
116 | + |
117 | + val to_json : tree -> Ezjsonm.value |
118 | + (* return a json representation of each note and it's descendants *) |
119 | end |
120 | |
121 | (* |