OCaml library for JSONfeed parsing and creation
1(** Feed items in a JSON Feed. *)
2
3type content =
4 [ `Html of string
5 | `Text of string
6 | `Both of string * string
7 ]
8
9type t = {
10 id : string;
11 content : content;
12 url : string option;
13 external_url : string option;
14 title : string option;
15 summary : string option;
16 image : string option;
17 banner_image : string option;
18 date_published : Ptime.t option;
19 date_modified : Ptime.t option;
20 authors : Author.t list option;
21 tags : string list option;
22 language : string option;
23 attachments : Attachment.t list option;
24}
25
26let create ~id ~content ?url ?external_url ?title ?summary ?image ?banner_image
27 ?date_published ?date_modified ?authors ?tags ?language ?attachments () =
28 {
29 id;
30 content;
31 url;
32 external_url;
33 title;
34 summary;
35 image;
36 banner_image;
37 date_published;
38 date_modified;
39 authors;
40 tags;
41 language;
42 attachments;
43 }
44
45let id t = t.id
46let content t = t.content
47let url t = t.url
48let external_url t = t.external_url
49let title t = t.title
50let summary t = t.summary
51let image t = t.image
52let banner_image t = t.banner_image
53let date_published t = t.date_published
54let date_modified t = t.date_modified
55let authors t = t.authors
56let tags t = t.tags
57let language t = t.language
58let attachments t = t.attachments
59
60let content_html t =
61 match t.content with
62 | `Html html -> Some html
63 | `Text _ -> None
64 | `Both (html, _) -> Some html
65
66let content_text t =
67 match t.content with
68 | `Html _ -> None
69 | `Text text -> Some text
70 | `Both (_, text) -> Some text
71
72let equal a b =
73 (* Items are equal if they have the same ID *)
74 a.id = b.id
75
76let compare a b =
77 (* Compare by publication date, with items without dates considered older *)
78 match a.date_published, b.date_published with
79 | None, None -> 0
80 | None, Some _ -> -1 (* Items without dates are "older" *)
81 | Some _, None -> 1
82 | Some da, Some db -> Ptime.compare da db
83
84let pp_content ppf = function
85 | `Html html ->
86 Format.fprintf ppf "HTML (%d chars)" (String.length html)
87 | `Text text ->
88 Format.fprintf ppf "Text (%d chars)" (String.length text)
89 | `Both (html, text) ->
90 Format.fprintf ppf "Both (HTML: %d chars, Text: %d chars)"
91 (String.length html) (String.length text)
92
93let pp ppf t =
94 match t.date_published, t.title with
95 | Some date, Some title ->
96 (* Use Ptime's date formatting *)
97 let (y, m, d), _ = Ptime.to_date_time date in
98 Format.fprintf ppf "[%04d-%02d-%02d] %s (%s)" y m d title t.id
99 | Some date, None ->
100 let (y, m, d), _ = Ptime.to_date_time date in
101 Format.fprintf ppf "[%04d-%02d-%02d] %s" y m d t.id
102 | None, Some title ->
103 Format.fprintf ppf "%s (%s)" title t.id
104 | None, None ->
105 Format.fprintf ppf "%s" t.id