Source file rss.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
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
open Util

(* I definitely should learn more about Tyxml... or Syndic. *)

let pp_empty ppf = Format.fprintf ppf ""

let pp_opt pp ppf = function
  | Some x -> Format.fprintf ppf "%a" pp x
  | None -> pp_empty ppf
;;

let pp_string = Format.pp_print_string
let pp_int = Format.pp_print_int
let pp_bool = Format.pp_print_bool

type attribute =
  | A : (string * 'a * (Format.formatter -> 'a -> unit)) -> attribute
  | AOpt :
      (string * 'a option * (Format.formatter -> 'a -> unit))
      -> attribute

type body = B : ('a * (Format.formatter -> 'a -> unit)) -> body

let unpack_attribute s = function
  | A (key, x, f) -> Format.asprintf {|%s %s="%a"|} s key f x
  | AOpt (key, Some x, f) -> Format.asprintf {|%s %s="%a"|} s key f x
  | AOpt (_, None, _) -> ""
;;

let unpack_body s = function
  | B (x, f) -> Format.asprintf "%s%a" s f x
;;

let attr k v p = A (k, v, p)
let attr_opt k v p = AOpt (k, v, p)
let body v p = B (v, p)

let pp_attr ppf attr =
  let s = List.fold_left unpack_attribute "" attr in
  Format.fprintf ppf "%s" s
;;

let pp_body ppf body =
  let s = List.fold_left unpack_body "" body in
  Format.fprintf ppf "%s" s
;;

let xml_node name ?(a = []) ppf content =
  match content with
  | [] -> Format.fprintf ppf "<%s%a />" name pp_attr a
  | content ->
    Format.fprintf ppf "<%s%a>%a</%s>" name pp_attr a pp_body content name
;;

let pp_title ppf x = xml_node "title" ppf [ body x pp_string ]
let pp_link ppf x = xml_node "link" ppf [ body x pp_string ]
let pp_description ppf x = xml_node "description" ppf [ body x pp_string ]
let pp_url ppf x = xml_node "url" ppf [ body x pp_string ]
let pp_width ppf x = xml_node "width" ppf [ body x pp_int ]
let pp_height ppf x = xml_node "height" ppf [ body x pp_int ]

let pp_pub_date ?(default_time = 10, 0, 0) ppf x =
  xml_node "pubDate" ppf [ body x $ Date.pp_rfc822 ~default_time ]
;;

let pp_author ppf x = xml_node "author" ppf [ body x pp_string ]
let pp_comments ppf x = xml_node "comments" ppf [ body x pp_string ]
let pp_copyright ppf x = xml_node "copyright" ppf [ body x pp_string ]
let pp_docs ppf x = xml_node "docs" ppf [ body x pp_string ]
let pp_generator ppf x = xml_node "generator" ppf [ body x pp_string ]
let pp_ttl ppf x = xml_node "ttl" ppf [ body x pp_int ]
let pp_webmaster ppf x = xml_node "webMaster" ppf [ body x pp_string ]

let pp_feed_link ppf x =
  xml_node
    "atom:link"
    ppf
    ~a:
      [ attr "href" x pp_string
      ; attr "rel" "self" pp_string
      ; attr "type" "application/rss+xml" pp_string
      ]
    []
;;

let pp_last_build_date ?(default_time = 10, 0, 0) ppf x =
  xml_node "pubDate" ppf [ body x $ Date.pp_rfc822 ~default_time ]
;;

let pp_managing_editor ppf x =
  xml_node "managingEditor" ppf [ body x pp_string ]
;;

module Image = struct
  type t =
    { title : string
    ; link : string
    ; url : string
    ; height : int option
    ; width : int option
    ; description : string option
    }

  let make ?description ?width ?height ~title ~link ~url () =
    { title; link; url; height; width; description }
  ;;

  let pp ppf img =
    xml_node
      "image"
      ppf
      [ body img.title pp_title
      ; body img.link pp_link
      ; body img.url pp_url
      ; body img.description $ pp_opt pp_description
      ; body img.width $ pp_opt pp_width
      ; body img.height $ pp_opt pp_height
      ]
  ;;

  let equal a b =
    String.equal a.title b.title
    && String.equal a.link b.link
    && String.equal a.url b.url
    && Option.equal Int.equal a.width b.width
    && Option.equal Int.equal a.height b.height
    && Option.equal String.equal a.description b.description
  ;;
end

module Category = struct
  type t =
    { category : string
    ; domain : string option
    }

  let make ?domain ~category () = { category; domain }

  let pp ppf category =
    xml_node
      "category"
      ppf
      ~a:[ attr_opt "domain" category.domain pp_string ]
      [ body category.category pp_string ]
  ;;

  let expand = List.map (fun c -> body c pp)

  let equal a b =
    String.equal a.category b.category
    && Option.equal String.equal a.domain b.domain
  ;;
end

module Enclosure = struct
  type t =
    { length : int
    ; media_type : Mime.t
    ; url : string
    }

  let make ~url ~media_type ~length () = { length; media_type; url }

  let pp ppf enclosure =
    xml_node
      "enclosure"
      ppf
      ~a:
        [ attr "url" enclosure.url pp_string
        ; attr "length" enclosure.url pp_string
        ; attr "type" enclosure.media_type Mime.pp
        ]
      []
  ;;

  let equal a b =
    Int.equal a.length b.length
    && Mime.equal a.media_type b.media_type
    && String.equal a.url b.url
  ;;
end

module Guid = struct
  type t =
    { url : string
    ; is_permalink : bool
    }

  let make ?(is_permalink = false) ~url () = { is_permalink; url }
  let permalink url = make ~is_permalink:true ~url ()
  let link url = make ~is_permalink:false ~url ()

  let pp ppf guid =
    xml_node
      "guid"
      ppf
      ~a:[ attr "isPermaLink" guid.is_permalink pp_bool ]
      [ body guid.url pp_string ]
  ;;

  let equal a b =
    String.equal a.url b.url && Bool.equal a.is_permalink b.is_permalink
  ;;
end

module Source = struct
  type t =
    { url : string
    ; title : string
    }

  let make ~url ~title () = { url; title }

  let pp ppf source =
    xml_node
      "source"
      ppf
      ~a:[ attr "url" source.url pp_string ]
      [ body source.title pp_string ]
  ;;

  let equal a b = String.equal a.title b.title && String.equal a.url b.url
end

module Item = struct
  type t =
    { title : string
    ; link : string
    ; pub_date : Date.t
    ; description : string
    ; guid : Guid.t
    ; author : string option
    ; categories : Category.t list
    ; comments : string option
    ; enclosure : Enclosure.t option
    ; source : Source.t option
    }

  let make
    ?author
    ?(categories = [])
    ?comments
    ?enclosure
    ?source
    ~title
    ~link
    ~pub_date
    ~description
    ~guid
    ()
    =
    { title
    ; link
    ; pub_date
    ; description
    ; guid
    ; author
    ; categories
    ; comments
    ; enclosure
    ; source
    }
  ;;

  let pp ?(default_time = 10, 0, 0) ppf entry =
    xml_node
      "item"
      ppf
      ([ body entry.title pp_title
       ; body entry.link pp_link
       ; body entry.pub_date $ pp_pub_date ~default_time
       ; body entry.description pp_description
       ; body entry.guid Guid.pp
       ; body entry.author $ pp_opt pp_author
       ; body entry.comments $ pp_opt pp_comments
       ; body entry.enclosure $ pp_opt Enclosure.pp
       ; body entry.source $ pp_opt Source.pp
       ]
      @ Category.expand entry.categories)
  ;;

  let equal a b =
    String.equal a.title b.title
    && String.equal a.link b.link
    && Date.equal a.pub_date b.pub_date
    && String.equal a.description b.description
    && Guid.equal a.guid b.guid
    && Option.equal String.equal a.author b.author
    && Preface.List.equal Category.equal a.categories b.categories
    && Option.equal String.equal a.comments b.comments
    && Option.equal Enclosure.equal a.enclosure b.enclosure
    && Option.equal Source.equal a.source b.source
  ;;

  let expand = List.map (fun c -> body c pp)
end

module Cloud = struct
  type protocol =
    | Xml_rpc
    | Soap
    | Http_post

  type t =
    { domain : string
    ; port : int
    ; path : string
    ; register_procedure : string
    ; protocol : protocol
    }

  let make ~domain ~port ~path ~register_procedure ~protocol () =
    { domain; port; path; register_procedure; protocol }
  ;;

  let equal_protocol a b =
    match a, b with
    | Xml_rpc, Xml_rpc -> true
    | Soap, Soap -> true
    | Http_post, Http_post -> true
    | _ -> false
  ;;

  let pp_protocol ppf x =
    Format.fprintf
      ppf
      "%s"
      (match x with
       | Xml_rpc -> "xml-rpc"
       | Soap -> "soap"
       | Http_post -> "http-post")
  ;;

  let pp ppf cloud =
    xml_node
      "cloud"
      ppf
      ~a:
        [ attr "domain" cloud.domain pp_string
        ; attr "port" cloud.port pp_int
        ; attr "path" cloud.path pp_string
        ; attr "registerProcedure" cloud.register_procedure pp_string
        ; attr "protocol" cloud.protocol pp_protocol
        ]
      []
  ;;

  let equal a b =
    String.equal a.domain b.domain
    && Int.equal a.port b.port
    && String.equal a.path b.path
    && String.equal a.register_procedure b.register_procedure
    && equal_protocol a.protocol b.protocol
  ;;
end

module Channel = struct
  type t =
    { title : string
    ; description : string
    ; link : string
    ; feed_link : string
    ; pub_date : Date.t option
    ; last_build_date : Date.t option
    ; category : Category.t option
    ; image : Image.t option
    ; cloud : Cloud.t option
    ; copyright : string option
    ; docs : string option
    ; generator : string option
    ; managing_editor : string option
    ; ttl : int option
    ; webmaster : string option
    ; items : Item.t list
    }

  let make
    ?pub_date
    ?last_build_date
    ?category
    ?image
    ?cloud
    ?copyright
    ?docs
    ?generator
    ?managing_editor
    ?ttl
    ?webmaster
    ~title
    ~link
    ~feed_link
    ~description
    items
    =
    { pub_date
    ; last_build_date
    ; category
    ; image
    ; cloud
    ; copyright
    ; docs
    ; generator
    ; managing_editor
    ; ttl
    ; webmaster
    ; title
    ; link
    ; feed_link
    ; description
    ; items
    }
  ;;

  let pp ?(default_time = 10, 0, 0) ppf channel =
    xml_node
      "channel"
      ppf
      ([ body channel.title pp_title
       ; body channel.link pp_link
       ; body channel.feed_link pp_feed_link
       ; body channel.description pp_description
       ; body channel.pub_date $ pp_opt (pp_pub_date ~default_time)
       ; body channel.last_build_date
         $ pp_opt (pp_last_build_date ~default_time)
       ; body channel.category $ pp_opt Category.pp
       ; body channel.image $ pp_opt Image.pp
       ; body channel.cloud $ pp_opt Cloud.pp
       ; body channel.copyright $ pp_opt pp_copyright
       ; body channel.docs $ pp_opt pp_docs
       ; body channel.generator $ pp_opt pp_generator
       ; body channel.managing_editor $ pp_opt pp_managing_editor
       ; body channel.ttl $ pp_opt pp_ttl
       ; body channel.webmaster $ pp_opt pp_webmaster
       ]
      @ Item.expand
          (List.sort
             (fun x y -> Date.compare y.Item.pub_date x.Item.pub_date)
             channel.items))
  ;;

  let equal a b =
    Option.equal Date.equal a.pub_date b.pub_date
    && Option.equal Date.equal a.last_build_date b.last_build_date
    && Option.equal Category.equal a.category b.category
    && Option.equal Image.equal a.image b.image
    && Option.equal Cloud.equal a.cloud b.cloud
    && Option.equal String.equal a.copyright b.copyright
    && Option.equal String.equal a.docs b.docs
    && Option.equal String.equal a.generator b.generator
    && Option.equal String.equal a.managing_editor b.managing_editor
    && Option.equal Int.equal a.ttl b.ttl
    && Option.equal String.equal a.webmaster b.webmaster
    && String.equal a.title b.title
    && String.equal a.link b.link
    && String.equal a.feed_link b.feed_link
    && String.equal a.description b.description
    && Preface.List.equal Item.equal a.items b.items
  ;;

  let pp_rss
    ?(xml_version = "1.0")
    ?(encoding = "UTF-8")
    ?(default_time = 10, 0, 0)
    ppf
    =
    Format.fprintf
      ppf
      "<?xml version=%S encoding=%S ?><rss version=\"2.0\" \
       xmlns:atom=\"http://www.w3.org/2005/Atom\">%a</rss>"
      xml_version
      encoding
      (pp ~default_time)
  ;;

  let to_rss ?xml_version ?encoding ?(default_time = 10, 0, 0) =
    Format.asprintf "%a" $ pp_rss ?xml_version ?encoding ~default_time
  ;;
end