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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
type days = Mon | Tue | Wed | Thu | Fri | Sat | Sun
type cloud_protocol = Xml_rpc | Soap | Http_post
module X = struct
open Xml
let title x = leaf ~name:"title" (escape x)
let link x = leaf ~name:"link" (Some x)
let description x = leaf ~name:"description" (cdata x)
let url x = Attr.string ~key:"url" x
end
module Cloud = struct
type t = {
domain : string
; port : int
; path : string
; register_procedure : string
; protocol : cloud_protocol
}
let protocol_to_string = function
| Xml_rpc -> "xml-rpc"
| Soap -> "soap"
| Http_post -> "http-post"
let make ~protocol ~domain ~port ~path ~register_procedure =
{ protocol; domain; port; path; register_procedure }
let to_xml { protocol; domain; port; path; register_procedure } =
Xml.node ~name:"cloud"
~attr:
Xml.Attr.
[
string ~key:"protocol" @@ protocol_to_string protocol
; string ~key:"domain" domain
; int ~key:"port" port
; string ~key:"path" path
; string ~key:"registerProcedure" register_procedure
]
[]
end
module Image = struct
type t = {
title : string
; link : string
; url : string
; height : int option
; width : int option
; description : string option
}
let make ~title ~link ?description ?width ?height ~url () =
let width = Option.map (fun x -> x |> Int.min 144 |> Int.max 1) width
and height = Option.map (fun x -> x |> Int.min 400 |> Int.max 1) height in
{ title; link; url; height; width; description }
let to_xml { title; link; url; height; width; description } =
Xml.node ~name:"image"
[
Xml.leaf ~name:"url" (Some url)
; X.title title
; X.link link
; Xml.may_leaf ~finalize:Xml.cdata ~name:"description" Fun.id description
; Xml.may_leaf ~name:"width" string_of_int width
; Xml.may_leaf ~name:"height" string_of_int height
]
end
module Skip_hours = struct
let to_xml l =
Xml.node ~name:"skipHours"
(List.map
(fun x ->
let x = x |> Int.min 23 |> Int.max 0 in
Xml.leaf ~name:"hour" (Some (string_of_int x)))
l)
end
module Skip_days = struct
module S = Set.Make (struct
type t = days
let to_int = function
| Mon -> 0
| Tue -> 1
| Wed -> 2
| Thu -> 3
| Fri -> 4
| Sat -> 5
| Sun -> 6
let compare a b =
let a = to_int a and b = to_int b in
Int.compare a b
end)
let to_string x =
Some
(match x with
| Mon -> "Monday"
| Tue -> "Tuesday"
| Wed -> "Wednesday"
| Thu -> "Thursday"
| Fri -> "Friday"
| Sat -> "Saturday"
| Sun -> "Sunday")
let to_xml l =
let s = S.of_list l |> S.to_list in
Xml.node ~name:"skipDays"
(List.map (fun x -> Xml.leaf ~name:"day" (to_string x)) s)
end
module Enclosure = struct
type t = { length : int; media_type : Media_type.t; url : string }
let make ~url ~media_type ~length =
let length = Int.max 0 length in
{ length; media_type; url }
let to_xml { length; media_type; url } =
Xml.leaf ~name:"enclosure"
~attr:
Xml.Attr.
[
X.url url
; int ~key:"length" length
; make ~key:"type" Media_type.to_string media_type
]
None
end
module Guid = struct
type t = { value : string; is_permalink : bool }
type strategy = Given of t | From_link | From_title
let from_link = From_link
let from_title = From_title
let create ~is_permalink value = { value; is_permalink }
let make ~is_permalink value = Given (create ~is_permalink value)
let to_xml { value; is_permalink } =
Xml.leaf ~indent:false ~name:"guid"
~attr:Xml.Attr.[ bool ~key:"isPermaLink" is_permalink ]
(Some value)
end
module Source = struct
type t = { title : string; url : string }
let make ~title ~url = { title; url }
let to_xml { title; url } =
Xml.leaf ~name:"source" ~attr:[ X.url url ] (Some title)
end
module Item = struct
type t = {
title : string
; link : string
; description : string
; author : Person.t option
; categories : Category.t list
; comments : string option
; enclosure : Enclosure.t option
; guid : Guid.t option
; pub_date : Datetime.t option
; source : Source.t option
}
let make ?author ?(categories = []) ? ?enclosure ?guid ?pub_date
?source ~title ~link ~description () =
let guid =
Option.map
(function
| Guid.Given g -> g
| Guid.From_link -> Guid.create ~is_permalink:true link
| Guid.From_title -> Guid.create ~is_permalink:false title)
guid
in
{
title
; link
; description
; author
; categories
; comments
; enclosure
; guid
; pub_date
; source
}
let to_xml
{
title
; link
; description
; author
; categories
;
; enclosure
; guid
; pub_date
; source
} =
Xml.node ~name:"item"
([
X.title title
; X.link link
; X.description description
; Xml.may_leaf ~name:"author" Person.to_rss2 author
; Xml.may_leaf ~name:"comments" Fun.id comments
; Xml.may Enclosure.to_xml enclosure
; Xml.may Guid.to_xml guid
; Xml.may_leaf ~name:"pubDate" Datetime.to_string pub_date
; Xml.may Source.to_xml source
]
@ List.map Category.to_rss2 categories)
end
module Channel = struct
type 'a t = {
title : string
; link : string
; url : string
; description : string
; language : Lang.t option
; copyright : string option
; managing_editor : Person.t option
; webmaster : Person.t option
; pub_date : Datetime.t option
; last_build_date : Datetime.t option
; categories : Category.t list
; generator : Generator.t option
; cloud : Cloud.t option
; ttl : int option
; image : Image.t option
; text_input : Text_input.t option
; skip_hours : int list option
; skip_days : days list option
; items : 'a list
}
let make ?language ?copyright ?managing_editor ?webmaster ?pub_date
?last_build_date ?(categories = []) ?generator ?cloud ?ttl ?image
?text_input ?skip_hours ?skip_days ~title ~link url ~description items =
let image = Option.map (fun f -> f ~title ~link) image in
{
title
; link
; url
; description
; language
; copyright
; managing_editor
; webmaster
; pub_date
; last_build_date
; categories
; generator
; cloud
; ttl
; image
; text_input
; skip_hours
; skip_days
; items
}
let to_xml f
{
title
; link
; url
; description
; language
; copyright
; managing_editor
; webmaster
; pub_date
; last_build_date
; categories
; generator
; cloud
; ttl
; image
; text_input
; skip_hours
; skip_days
; items
} =
Xml.node ~name:"channel"
([
X.title title
; X.link link
; X.description description
; Xml.leaf ~ns:"atom" ~name:"link"
~attr:
Xml.Attr.
[
string ~key:"href" url
; string ~key:"rel" "self"
; string ~key:"type" "application/rss+xml"
]
None
; Xml.may_leaf ~name:"language" Lang.to_string language
; Xml.may_leaf ~name:"copyright" Fun.id copyright
; Xml.may_leaf ~name:"managingEditor" Person.to_rss2 managing_editor
; Xml.may_leaf ~name:"webMaster" Person.to_rss2 webmaster
; Xml.may_leaf ~name:"pubDate" Datetime.to_string pub_date
; Xml.may_leaf ~name:"lastBuildDate" Datetime.to_string last_build_date
;
Xml.leaf ~name:"docs"
(Some "https://www.rssboard.org/rss-specification")
; Xml.may Generator.to_rss2 generator
; Xml.may Cloud.to_xml cloud
; Xml.may_leaf ~name:"ttl" string_of_int ttl
; Xml.may Image.to_xml image
; Xml.may Text_input.to_rss2 text_input
; Xml.may Skip_hours.to_xml skip_hours
; Xml.may Skip_days.to_xml skip_days
]
@ List.map Category.to_rss2 categories
@ List.map (fun x -> x |> f |> Item.to_xml) items)
end
type cloud = Cloud.t
type enclosure = Enclosure.t
type guid = Guid.t
type guid_strategy = Guid.strategy
type source = Source.t
type image = Image.t
type item = Item.t
let cloud = Cloud.make
let guid_from_title = Guid.from_title
let guid_from_link = Guid.from_link
let guid = Guid.make
let source = Source.make
let image = Image.make
let enclosure = Enclosure.make
let item = Item.make
let feed ?encoding ?standalone ?language ?copyright ?managing_editor ?webmaster
?pub_date ?last_build_date ?categories ?(generator = Generator.yocaml)
?cloud ?ttl ?image ?text_input ?skip_hours ?skip_days ~title ~link ~url
~description f items =
let channel =
Channel.make ?language ?copyright ?managing_editor ?webmaster ?pub_date
?last_build_date ?categories ~generator ?cloud ?ttl ?image ?text_input
?skip_hours ?skip_days ~title ~link url ~description items
in
let nodes = [ Channel.to_xml f channel ] in
Xml.document ?encoding ?standalone ~version:"1.0"
(Xml.node ~name:"rss"
~attr:
Xml.Attr.
[
string ~ns:"xmlns" ~key:"atom" "http://www.w3.org/2005/Atom"
; string ~key:"version" "2.0"
]
nodes)
let from ?encoding ?standalone ?language ?copyright ?managing_editor ?webmaster
?pub_date ?last_build_date ?categories ?generator ?cloud ?ttl ?image
?text_input ?skip_hours ?skip_days ~title ~site_url ~feed_url ~description f
=
Yocaml.Task.lift (fun articles ->
let feed =
feed ?encoding ?standalone ?language ?copyright ?managing_editor
?webmaster ?pub_date ?last_build_date ?categories ?generator ?cloud
?ttl ?image ?text_input ?skip_hours ?skip_days ~title ~link:site_url
~url:feed_url ~description f articles
in
Xml.to_string feed)
let from_articles ?encoding ?standalone ?language ?copyright ?managing_editor
?webmaster ?pub_date ?categories ?generator ?cloud ?ttl ?image ?text_input
?skip_hours ?skip_days ~title ~site_url ~feed_url ~description () =
Yocaml.Task.lift (fun articles ->
let last_build_date =
List.fold_left
(fun acc (_, elt) ->
let open Yocaml.Archetype in
let b = Yocaml.Archetype.Article.date elt in
match acc with
| None -> Some b
| Some a -> if Datetime.compare a b > 0 then Some a else Some b)
None articles
|> Option.map Datetime.make
in
let feed =
feed ?encoding ?standalone ?language ?copyright ?managing_editor
?webmaster ?pub_date ?last_build_date ?categories ?generator ?cloud
?ttl ?image ?text_input ?skip_hours ?skip_days ~title ~link:site_url
~url:feed_url ~description
(fun (path, article) ->
let open Yocaml.Archetype.Article in
let title = title article in
let link = site_url ^ Yocaml.Path.to_string path in
let guid = guid_from_link in
let description =
Option.value ~default:"no description" (synopsis article)
in
let pub_date = Datetime.make (date article) in
item ~title ~link ~guid ~description ~pub_date ())
articles
in
Xml.to_string feed)