-
Notifications
You must be signed in to change notification settings - Fork 27
/
canopy_content.ml
72 lines (60 loc) · 1.91 KB
/
canopy_content.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
open Canopy_utils
type content_t =
| Markdown of Canopy_article.t
type error_t =
Unknown
| Error of string
| Ok of content_t
let meta_assoc str =
Re.Str.split (Re.Str.regexp "\n") str |>
List.map (fun meta ->
let reg = Re.Str.regexp "\\(.*\\): \\(.*\\)" in
let _ = Re.Str.string_match reg meta 0 in
let key = Re.Str.matched_group 1 meta in
let value = Re.Str.matched_group 2 meta in
key, value)
let of_string ~base_uuid ~uri ~created ~updated ~content =
let splitted_content = Re.Str.bounded_split (Re.Str.regexp "---") content 2 in
match splitted_content with
| [raw_meta;raw_content] ->
begin
match meta_assoc raw_meta with
| meta ->
begin
match assoc_opt "content" meta with
| Some "markdown"
| None ->
Canopy_article.of_string base_uuid meta uri created updated raw_content
|> map_opt (fun article -> Ok (Markdown article)) (Error "Error while parsing article")
| Some _ -> Unknown
end
| exception _ -> Unknown
end
| _ -> Error "No header found"
let to_tyxml = function
| Markdown m ->
let open Canopy_article in
m.title, to_tyxml m
let to_tyxml_listing_entry = function
| Markdown m -> Canopy_article.to_tyxml_listing_entry m
let to_atom cache = function
| Markdown m -> Canopy_article.to_atom cache m
let find_tag tagname = function
| Markdown m ->
List.exists ((=) tagname) m.Canopy_article.tags
let date = function
| Markdown m ->
m.Canopy_article.created
let compare a b = Ptime.compare (date b) (date a)
let updated = function
| Markdown m ->
m.Canopy_article.updated
let tags content_map =
let module S = Set.Make(String) in
let s = KeyMap.fold_articles (
fun _k v s -> match v with
| Markdown m ->
let s' = S.of_list m.Canopy_article.tags in
S.union s s')
content_map S.empty
in S.elements s