package syndic

  1. Overview
  2. Docs

Source file syndic_atom.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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
open Syndic_common.XML
open Syndic_common.Util
module XML = Syndic_xml
module Error = Syndic_error
module Date = Syndic_date

let atom_ns = "http://www.w3.org/2005/Atom"
let xhtml_ns = "http://www.w3.org/1999/xhtml"
let namespaces = [atom_ns]

type rel = Alternate | Related | Self | Enclosure | Via | Link of Uri.t

type link =
  { href: Uri.t
  ; rel: rel
  ; type_media: string option
  ; hreflang: string option
  ; title: string
  ; length: int option }

let link ?type_media ?hreflang ?(title = "") ?length ?(rel = Alternate) href =
  {href; rel; type_media; hreflang; title; length}

type link' =
  [ `HREF of Uri.t
  | `Rel of string
  | `Type of string
  | `HREFLang of string
  | `Title of string
  | `Length of string ]

(* The actual XML content is supposed to be inside a <div> which is NOT part of
   the content. *)
let rec get_xml_content xml0 = function
  | XML.Data (_, s) :: tl ->
      if only_whitespace s then get_xml_content xml0 tl
      else xml0 (* unexpected *)
  | XML.Node (_pos, tag, data) :: tl when tag_is tag "div" ->
      let is_space =
        List.for_all
          (function XML.Data (_, s) -> only_whitespace s | _ -> false)
          tl
      in
      if is_space then data else xml0
  | _ -> xml0

let no_namespace = Some ""
let rm_namespace _ = no_namespace

(* For HTML, the spec says the whole content needs to be escaped
   http://tools.ietf.org/html/rfc4287#section-3.1.1.2 (some feeds use <![CDATA[
   ]]>) so a single data item should be present. If not, assume the HTML was
   properly parsed and convert it back to a string as it should. *)
let get_html_content html =
  match html with
  | [XML.Data (_, d)] -> d
  | h ->
      (* It is likely that, when the HTML was parsed, the Atom namespace was
         applied. Remove it. *)
      String.concat "" (List.map (XML.to_string ~ns_prefix:rm_namespace) h)

type text_construct =
  | Text of string
  | Html of Uri.t option * string
  | Xhtml of Uri.t option * XML.t list

let text_construct_of_xml ~xmlbase
    ((_pos, (_tag, attr), data) : XML.pos * XML.tag * t list) =
  let xmlbase = xmlbase_of_attr ~xmlbase attr in
  match find (fun a -> attr_is a "type") attr with
  | Some (_, "html") -> Html (xmlbase, get_html_content data)
  | Some (_, "application/xhtml+xml") | Some (_, "xhtml") ->
      Xhtml (xmlbase, get_xml_content data data)
  | _ -> Text (get_leaf data)

type author = {name: string; uri: Uri.t option; email: string option}

let empty_author = {name= ""; uri= None; email= None}
let not_empty_author a = a.name <> "" || a.uri <> None || a.email <> None
let author ?uri ?email name = {uri; email; name}

type person' = [`Name of string | `URI of Uri.t | `Email of string]

let make_person datas ~pos:_ (l : [< person'] list) =
  (* element atom:name { text } *)
  let name =
    match find (function `Name _ -> true | _ -> false) l with
    | Some (`Name s) -> s
    | _ ->
        (* The spec mandates that <author><name>name</name></author> but
           several feeds just do <author>name</author> *)
        get_leaf datas
  in
  (* element atom:uri { atomUri }? *)
  let uri =
    match find (function `URI _ -> true | _ -> false) l with
    | Some (`URI u) -> Some u
    | _ -> None
  in
  (* element atom:email { atomEmailAddress }? *)
  let email =
    match find (function `Email _ -> true | _ -> false) l with
    | Some (`Email e) -> Some e
    | _ -> None
  in
  ({name; uri; email} : author)

let make_author datas ~pos a = `Author (make_person datas ~pos a)

let person_name_of_xml ~xmlbase:_ (_pos, _tag, datas) =
  `Name (try get_leaf datas with Not_found -> "")

(* mandatory ? *)

let person_uri_of_xml ~xmlbase (pos, _tag, datas) =
  try `URI (XML.resolve ~xmlbase (Uri.of_string (get_leaf datas)))
  with Not_found ->
    raise
      (Error.Error (pos, "The content of <uri> MUST be a non-empty string"))

let person_email_of_xml ~xmlbase:_ (_pos, _tag, datas) =
  `Email (try get_leaf datas with Not_found -> "")

(* mandatory ? *)

(* {[ atomAuthor = element atom:author { atomPersonConstruct } ]} where

   atomPersonConstruct = atomCommonAttributes, (element atom:name { text } &
   element atom:uri { atomUri }? & element atom:email { atomEmailAddress }? &
   extensionElement * )

   This specification assigns no significance to the order of appearance of the
   child elements in a Person construct. *)
let person_data_producer =
  [ ("name", person_name_of_xml)
  ; ("uri", person_uri_of_xml)
  ; ("email", person_email_of_xml) ]

let author_of_xml ~xmlbase ((_, _, datas) as xml) =
  generate_catcher ~namespaces ~data_producer:person_data_producer
    (make_author datas) ~xmlbase xml

type uri = Uri.t option * string
type person = [`Email of string | `Name of string | `URI of uri] list

let person_data_producer' =
  [ ("name", dummy_of_xml ~ctor:(fun ~xmlbase:_ a -> `Name a))
  ; ("uri", dummy_of_xml ~ctor:(fun ~xmlbase a -> `URI (xmlbase, a)))
  ; ("email", dummy_of_xml ~ctor:(fun ~xmlbase:_ a -> `Email a)) ]

let author_of_xml' =
  generate_catcher ~namespaces ~data_producer:person_data_producer'
    (fun ~pos:_ x -> `Author x )

type category = {term: string; scheme: Uri.t option; label: string option}

let category ?scheme ?label term = {scheme; label; term}

type category' = [`Term of string | `Scheme of Uri.t | `Label of string]

let make_category ~pos (l : [< category'] list) =
  (* attribute term { text } *)
  let term =
    match find (function `Term _ -> true | _ -> false) l with
    | Some (`Term t) -> t
    | _ ->
        raise
          (Error.Error (pos, "Category elements MUST have a 'term' attribute"))
  in
  (* attribute scheme { atomUri }? *)
  let scheme =
    match find (function `Scheme _ -> true | _ -> false) l with
    | Some (`Scheme u) -> Some u
    | _ -> None
  in
  (* attribute label { text }? *)
  let label =
    match find (function `Label _ -> true | _ -> false) l with
    | Some (`Label l) -> Some l
    | _ -> None
  in
  `Category ({term; scheme; label} : category)

let scheme_of_xml ~xmlbase a = `Scheme (XML.resolve ~xmlbase (Uri.of_string a))

(* atomCategory = element atom:category { atomCommonAttributes, attribute term
   { text }, attribute scheme { atomUri }?, attribute label { text }?,
   undefinedContent } *)
let category_attr_producer =
  [ ("term", fun ~xmlbase:_ a -> `Term a)
  ; ("label", fun ~xmlbase:_ a -> `Label a) ]

let category_of_xml =
  let attr_producer = ("scheme", scheme_of_xml) :: category_attr_producer in
  generate_catcher ~attr_producer make_category

let category_of_xml' =
  let attr_producer =
    ("scheme", fun ~xmlbase:_ a -> `Scheme a) :: category_attr_producer
  in
  generate_catcher ~attr_producer (fun ~pos:_ x -> `Category x)

let make_contributor datas ~pos a = `Contributor (make_person datas ~pos a)

let contributor_of_xml ~xmlbase ((_, _, datas) as xml) =
  generate_catcher ~namespaces ~data_producer:person_data_producer
    (make_contributor datas) ~xmlbase xml

let contributor_of_xml' =
  generate_catcher ~namespaces ~data_producer:person_data_producer'
    (fun ~pos:_ x -> `Contributor x )

type generator = {version: string option; uri: Uri.t option; content: string}

let generator ?uri ?version content = {uri; version; content}

type generator' = [`URI of Uri.t | `Version of string | `Content of string]

let make_generator ~pos (l : [< generator'] list) =
  (* text *)
  let content =
    match find (function `Content _ -> true | _ -> false) l with
    | Some (`Content c) -> c
    | _ ->
        raise
          (Error.Error
             (pos, "The content of <generator> MUST be a non-empty string"))
  in
  (* attribute version { text }? *)
  let version =
    match find (function `Version _ -> true | _ -> false) l with
    | Some (`Version v) -> Some v
    | _ -> None
  in
  (* attribute uri { atomUri }? *)
  let uri =
    match find (function `URI _ -> true | _ -> false) l with
    | Some (`URI u) -> Some u
    | _ -> None
  in
  `Generator ({version; uri; content} : generator)

(* URI, if present, MUST be an IRI reference [RFC3987]. The definition of "IRI"
   excludes relative references but we resolve it anyway in case this is not
   respected by the generator. *)
let generator_uri_of_xml ~xmlbase a =
  `URI (XML.resolve ~xmlbase (Uri.of_string a))

(* atomGenerator = element atom:generator { atomCommonAttributes, attribute uri
   { atomUri }?, attribute version { text }?, text } *)
let generator_of_xml =
  let attr_producer =
    [("version", fun ~xmlbase:_ a -> `Version a); ("uri", generator_uri_of_xml)]
  in
  let leaf_producer ~xmlbase:_ _pos data = `Content data in
  generate_catcher ~attr_producer ~leaf_producer make_generator

let generator_of_xml' =
  let attr_producer =
    [ ("version", fun ~xmlbase:_ a -> `Version a)
    ; ("uri", fun ~xmlbase a -> `URI (xmlbase, a)) ]
  in
  let leaf_producer ~xmlbase:_ _pos data = `Content data in
  generate_catcher ~attr_producer ~leaf_producer (fun ~pos:_ x -> `Generator x)

type icon = Uri.t

let make_icon ~pos (l : Uri.t list) =
  (* (atomUri) *)
  let uri =
    match l with
    | u :: _ -> u
    | [] ->
        raise
          (Error.Error (pos, "The content of <icon> MUST be a non-empty string"))
  in
  `Icon uri

(* atomIcon = element atom:icon { atomCommonAttributes, } *)
let icon_of_xml =
  let leaf_producer ~xmlbase _pos data =
    XML.resolve ~xmlbase (Uri.of_string data)
  in
  generate_catcher ~leaf_producer make_icon

let icon_of_xml' =
  let leaf_producer ~xmlbase _pos data = `URI (xmlbase, data) in
  generate_catcher ~leaf_producer (fun ~pos:_ x -> `Icon x)

type id = Uri.t

let make_id ~pos (l : string list) =
  (* (atomUri) *)
  let id =
    match l with
    | u :: _ -> Uri.of_string u
    | [] ->
        raise
          (Error.Error (pos, "The content of <id> MUST be a non-empty string"))
  in
  `ID id

(* atomId = element atom:id { atomCommonAttributes, (atomUri) } *)
let id_of_xml, id_of_xml' =
  let leaf_producer ~xmlbase:_ _pos data = data in
  ( generate_catcher ~leaf_producer make_id
  , generate_catcher ~leaf_producer (fun ~pos:_ x -> `ID x) )

let rel_of_string s =
  match String.lowercase_ascii (String.trim s) with
  | "alternate" -> Alternate
  | "related" -> Related
  | "self" -> Self
  | "enclosure" -> Enclosure
  | "via" -> Via
  | uri ->
      (* RFC 4287 § 4.2.7.2: the use of a relative reference other than a
         simple name is not allowed. Thus no need to resolve against xml:base. *)
      Link (Uri.of_string uri)

let make_link ~pos (l : [< link'] list) =
  (* attribute href { atomUri } *)
  let href =
    match find (function `HREF _ -> true | _ -> false) l with
    | Some (`HREF u) -> u
    | _ ->
        raise (Error.Error (pos, "Link elements MUST have a 'href' attribute"))
  in
  (* attribute rel { atomNCName | atomUri }? *)
  let rel =
    match find (function `Rel _ -> true | _ -> false) l with
    | Some (`Rel r) -> rel_of_string r
    | _ -> Alternate
    (* cf. RFC 4287 § 4.2.7.2 *)
  in
  (* attribute type { atomMediaType }? *)
  let type_media =
    match find (function `Type _ -> true | _ -> false) l with
    | Some (`Type t) -> Some t
    | _ -> None
  in
  (* attribute hreflang { atomLanguageTag }? *)
  let hreflang =
    match find (function `HREFLang _ -> true | _ -> false) l with
    | Some (`HREFLang l) -> Some l
    | _ -> None
  in
  (* attribute title { text }? *)
  let title =
    match find (function `Title _ -> true | _ -> false) l with
    | Some (`Title s) -> s
    | _ -> ""
  in
  (* attribute length { text }? *)
  let length =
    match find (function `Length _ -> true | _ -> false) l with
    | Some (`Length i) -> Some (int_of_string i)
    | _ -> None
  in
  `Link ({href; rel; type_media; hreflang; title; length} : link)

let link_href_of_xml ~xmlbase a =
  `HREF (XML.resolve ~xmlbase (Uri.of_string a))

(* atomLink = element atom:link { atomCommonAttributes, attribute href {
   atomUri }, attribute rel { atomNCName | atomUri }?, attribute type {
   atomMediaType }?, attribute hreflang { atomLanguageTag }?, attribute title {
   text }?, attribute length { text }?, undefinedContent } *)
let link_attr_producer =
  [ ("rel", fun ~xmlbase:_ a -> `Rel a)
  ; ("type", fun ~xmlbase:_ a -> `Type a)
  ; ("hreflang", fun ~xmlbase:_ a -> `HREFLang a)
  ; ("title", fun ~xmlbase:_ a -> `Title a)
  ; ("length", fun ~xmlbase:_ a -> `Length a) ]

let link_of_xml =
  let attr_producer = ("href", link_href_of_xml) :: link_attr_producer in
  generate_catcher ~attr_producer make_link

let link_of_xml' =
  let attr_producer =
    ("href", fun ~xmlbase:_ a -> `HREF a) :: link_attr_producer
  in
  generate_catcher ~attr_producer (fun ~pos:_ x -> `Link x)



let  ~pos (l : Uri.t list) =
  (* (atomUri) *)
  let uri =
    match l with
    | u :: _ -> u
    | [] ->
        raise
          (Error.Error (pos, "The content of <logo> MUST be a non-empty string"))
  in
  `Logo uri

(* atomLogo = element atom:logo { atomCommonAttributes, (atomUri) } *)
let logo_of_xml =
  let leaf_producer ~xmlbase _pos data =
    XML.resolve ~xmlbase (Uri.of_string data)
  in
  generate_catcher ~leaf_producer make_logo

let logo_of_xml' =
  let leaf_producer ~xmlbase _pos data = `URI (xmlbase, data) in
  generate_catcher ~leaf_producer (fun ~pos:_ x -> `Logo x)

type published = Date.t
type published' = [`Date of string]

let make_published ~pos (l : [< published'] list) =
  (* atom:published { atomDateConstruct } *)
  let date =
    match find (fun (`Date _) -> true) l with
    | Some (`Date d) -> Date.of_rfc3339 d
    | _ ->
        raise
          (Error.Error
             (pos, "The content of <published> MUST be a non-empty string"))
  in
  `Published date

(* atomPublished = element atom:published { atomDateConstruct } *)
let published_of_xml, published_of_xml' =
  let leaf_producer ~xmlbase:_ _pos data = `Date data in
  ( generate_catcher ~leaf_producer make_published
  , generate_catcher ~leaf_producer (fun ~pos:_ x -> `Published x) )

type rights = text_construct

let rights_of_xml ~xmlbase a = `Rights (text_construct_of_xml ~xmlbase a)

(* atomRights = element atom:rights { atomTextConstruct } *)
let rights_of_xml' ~xmlbase:_
    ((_pos, (_tag, _attr), data) : XML.pos * XML.tag * t list) =
  `Rights data

type title = text_construct

let title_of_xml ~xmlbase a = `Title (text_construct_of_xml ~xmlbase a)

(* atomTitle = element atom:title { atomTextConstruct } *)
let title_of_xml' ~xmlbase:_
    ((_pos, (_tag, _attr), data) : XML.pos * XML.tag * t list) =
  `Title data

type subtitle = text_construct

let subtitle_of_xml ~xmlbase a = `Subtitle (text_construct_of_xml ~xmlbase a)

(* atomSubtitle = element atom:subtitle { atomTextConstruct } *)
let subtitle_of_xml' ~xmlbase:_
    ((_pos, (_tag, _attr), data) : XML.pos * XML.tag * t list) =
  `Subtitle data

type updated = Date.t
type updated' = [`Date of string]

let make_updated ~pos (l : [< updated'] list) =
  (* atom:updated { atomDateConstruct } *)
  let updated =
    match find (fun (`Date _) -> true) l with
    | Some (`Date d) -> Date.of_rfc3339 d
    | _ ->
        raise
          (Error.Error
             (pos, "The content of <updated> MUST be a non-empty string"))
  in
  `Updated updated

(* atomUpdated = element atom:updated { atomDateConstruct } *)
let updated_of_xml, updated_of_xml' =
  let leaf_producer ~xmlbase:_ _pos data = `Date data in
  ( generate_catcher ~leaf_producer make_updated
  , generate_catcher ~leaf_producer (fun ~pos:_ x -> `Updated x) )

type source =
  { authors: author list
  ; categories: category list
  ; contributors: author list
  ; generator: generator option
  ; icon: icon option
  ; id: id
  ; links: link list
  ; logo: logo option
  ; rights: rights option
  ; subtitle: subtitle option
  ; title: title
  ; updated: updated option }

let source ?(categories = []) ?(contributors = []) ?generator ?icon
    ?(links = []) ?logo ?rights ?subtitle ?updated ~authors ~id ~title =
  { authors
  ; categories
  ; contributors
  ; generator
  ; icon
  ; id
  ; links
  ; logo
  ; rights
  ; subtitle
  ; title
  ; updated }

type source' =
  [ `Author of author
  | `Category of category
  | `Contributor of author
  | `Generator of generator
  | `Icon of icon
  | `ID of id
  | `Link of link
  | `Logo of logo
  | `Subtitle of subtitle
  | `Title of title
  | `Rights of rights
  | `Updated of updated ]

let make_source ~pos (l : [< source'] list) =
  (* atomAuthor* *)
  let authors =
    List.fold_left
      (fun acc -> function `Author x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomCategory* *)
  let categories =
    List.fold_left
      (fun acc -> function `Category x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomContributor* *)
  let contributors =
    List.fold_left
      (fun acc -> function `Contributor x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomGenerator? *)
  let generator =
    match find (function `Generator _ -> true | _ -> false) l with
    | Some (`Generator g) -> Some g
    | _ -> None
  in
  (* atomIcon? *)
  let icon =
    match find (function `Icon _ -> true | _ -> false) l with
    | Some (`Icon u) -> Some u
    | _ -> None
  in
  (* atomId? *)
  let id =
    match find (function `ID _ -> true | _ -> false) l with
    | Some (`ID i) -> i
    | _ ->
        raise
          (Error.Error
             (pos, "<source> elements MUST contains exactly one <id> elements"))
  in
  (* atomLink* *)
  let links =
    List.fold_left (fun acc -> function `Link x -> x :: acc | _ -> acc) [] l
  in
  (* atomLogo? *)
  let logo =
    match find (function `Logo _ -> true | _ -> false) l with
    | Some (`Logo u) -> Some u
    | _ -> None
  in
  (* atomRights? *)
  let rights =
    match find (function `Rights _ -> true | _ -> false) l with
    | Some (`Rights r) -> Some r
    | _ -> None
  in
  (* atomSubtitle? *)
  let subtitle =
    match find (function `Subtitle _ -> true | _ -> false) l with
    | Some (`Subtitle s) -> Some s
    | _ -> None
  in
  (* atomTitle? *)
  let title =
    match find (function `Title _ -> true | _ -> false) l with
    | Some (`Title s) -> s
    | _ ->
        raise
          (Error.Error
             ( pos
             , "<source> elements MUST contains exactly one <title> elements"
             ))
  in
  (* atomUpdated? *)
  let updated =
    match find (function `Updated _ -> true | _ -> false) l with
    | Some (`Updated d) -> Some d
    | _ -> None
  in
  `Source
    ( { authors
      ; categories
      ; contributors
      ; generator
      ; icon
      ; id
      ; links
      ; logo
      ; rights
      ; subtitle
      ; title
      ; updated }
      : source )

(* atomSource = element atom:source { atomCommonAttributes, (atomAuthor* &
   atomCategory* & atomContributor* & atomGenerator? & atomIcon? & atomId? &
   atomLink* & atomLogo? & atomRights? & atomSubtitle? & atomTitle? &
   atomUpdated? & extensionElement * ) } *)
let source_of_xml =
  let data_producer =
    [ ("author", author_of_xml)
    ; ("category", category_of_xml)
    ; ("contributor", contributor_of_xml)
    ; ("generator", generator_of_xml)
    ; ("icon", icon_of_xml); ("id", id_of_xml); ("link", link_of_xml)
    ; ("logo", logo_of_xml); ("rights", rights_of_xml)
    ; ("subtitle", subtitle_of_xml)
    ; ("title", title_of_xml)
    ; ("updated", updated_of_xml) ]
  in
  generate_catcher ~namespaces ~data_producer make_source

let source_of_xml' =
  let data_producer =
    [ ("author", author_of_xml')
    ; ("category", category_of_xml')
    ; ("contributor", contributor_of_xml')
    ; ("generator", generator_of_xml')
    ; ("icon", icon_of_xml'); ("id", id_of_xml'); ("link", link_of_xml')
    ; ("logo", logo_of_xml'); ("rights", rights_of_xml')
    ; ("subtitle", subtitle_of_xml')
    ; ("title", title_of_xml')
    ; ("updated", updated_of_xml') ]
  in
  generate_catcher ~namespaces ~data_producer (fun ~pos:_ x -> `Source x)

type mime = string

type content =
  | Text of string
  | Html of Uri.t option * string
  | Xhtml of Uri.t option * Syndic_xml.t list
  | Mime of mime * string
  | Src of mime option * Uri.t

[@@@warning "-34"]

type content' = [`Type of string | `SRC of string | `Data of Syndic_xml.t list]

(* atomInlineTextContent = element atom:content { atomCommonAttributes,
   attribute type { "text" | "html" }?, (text)* }

   atomInlineXHTMLContent = element atom:content { atomCommonAttributes,
   attribute type { "xhtml" }, xhtmlDiv }

   atomInlineOtherContent = element atom:content { atomCommonAttributes,
   attribute type { atomMediaType }?, (text|anyElement)* }

   atomOutOfLineContent = element atom:content { atomCommonAttributes,
   attribute type { atomMediaType }?, attribute src { atomUri }, empty }

   atomContent = atomInlineTextContent | atomInlineXHTMLContent |
   atomInlineOtherContent | atomOutOfLineContent *)
let content_of_xml ~xmlbase
    ((_pos, (_tag, attr), data) : XML.pos * XML.tag * t list) =
  (* MIME ::= attribute type { "text" | "html" }? | attribute type { "xhtml" }
     | attribute type { atomMediaType }? *)
  (* attribute src { atomUri } | none If src s present, [data] MUST be empty. *)
  match find (fun a -> attr_is a "src") attr with
  | Some (_, src) ->
      let mime =
        match find (fun a -> attr_is a "type") attr with
        | Some (_, ty) -> Some ty
        | None -> None
      in
      `Content (Src (mime, XML.resolve ~xmlbase (Uri.of_string src)))
  | None ->
      (* (text)*
       *  | xhtmlDiv
       *  | (text|anyElement)*
       *  | none *)
      `Content
        ( match find (fun a -> attr_is a "type") attr with
        | Some (_, "text") | None -> Text (get_leaf data)
        | Some (_, "html") -> Html (xmlbase, get_html_content data)
        | Some (_, "xhtml") -> Xhtml (xmlbase, get_xml_content data data)
        | Some (_, mime) -> Mime (mime, get_leaf data) )

let content_of_xml' ~xmlbase:_
    ((_pos, (_tag, attr), data) : XML.pos * XML.tag * t list) =
  let l =
    match find (fun a -> attr_is a "src") attr with
    | Some (_, src) -> [`SRC src]
    | None -> []
  in
  let l =
    match find (fun a -> attr_is a "type") attr with
    | Some (_, ty) -> `Type ty :: l
    | None -> l
  in
  `Content (`Data data :: l)

type summary = text_construct

(* atomSummary = element atom:summary { atomTextConstruct } *)
let summary_of_xml ~xmlbase a = `Summary (text_construct_of_xml ~xmlbase a)

let summary_of_xml' ~xmlbase:_ ((_, (_, _), data) : XML.pos * XML.tag * t list)
    =
  `Summary data

type entry =
  { authors: author * author list
  ; categories: category list
  ; content: content option
  ; contributors: author list
  ; id: id
  ; links: link list
  ; published: published option
  ; rights: rights option
  ; source: source option
  ; summary: summary option
  ; title: title
  ; updated: updated }

let entry ?(categories = []) ?content ?(contributors = []) ?(links = [])
    ?published ?rights ?source ?summary ~id ~authors ~title ~updated () =
  { authors
  ; categories
  ; content
  ; contributors
  ; id
  ; links
  ; published
  ; rights
  ; source
  ; summary
  ; title
  ; updated }

type entry' =
  [ `Author of author
  | `Category of category
  | `Contributor of author
  | `ID of id
  | `Link of link
  | `Published of published
  | `Rights of rights
  | `Source of source
  | `Content of content
  | `Summary of summary
  | `Title of title
  | `Updated of updated ]

module LinkOrder : Set.OrderedType with type t = string * string = struct
  type t = string * string

  let compare (a : t) (b : t) =
    match compare (fst a) (fst b) with 0 -> compare (snd a) (snd b) | n -> n
end

module LinkSet = Set.Make (LinkOrder)

let uniq_link_alternate ~pos (l : link list) =
  let string_of_duplicate_link {href; type_media; hreflang; _}
      (type_media', hreflang') =
    let ty = (function Some a -> a | None -> "(none)") type_media in
    let hl = (function Some a -> a | None -> "(none)") hreflang in
    let ty' = (function "" -> "(none)" | s -> s) type_media' in
    let hl' = (function "" -> "(none)" | s -> s) hreflang' in
    Printf.sprintf
      "Duplicate link between <link href=\"%s\" hreflang=\"%s\" type=\"%s\" \
       ..> and <link hreflang=\"%s\" type=\"%s\" ..>"
      (Uri.to_string href) hl ty hl' ty'
  in
  let raise_error link link' =
    raise (Error.Error (pos, string_of_duplicate_link link link'))
  in
  let rec aux acc = function
    | [] -> l
    | ({rel; type_media= Some ty; hreflang= Some hl; _} as x) :: r
      when rel = Alternate ->
        if LinkSet.mem (ty, hl) acc then
          raise_error x (LinkSet.find (ty, hl) acc)
        else aux (LinkSet.add (ty, hl) acc) r
    | ({rel; type_media= None; hreflang= Some hl; _} as x) :: r
      when rel = Alternate ->
        if LinkSet.mem ("", hl) acc then
          raise_error x (LinkSet.find ("", hl) acc)
        else aux (LinkSet.add ("", hl) acc) r
    | ({rel; type_media= Some ty; hreflang= None; _} as x) :: r
      when rel = Alternate ->
        if LinkSet.mem (ty, "") acc then
          raise_error x (LinkSet.find (ty, "") acc)
        else aux (LinkSet.add (ty, "") acc) r
    | ({rel; type_media= None; hreflang= None; _} as x) :: r
      when rel = Alternate ->
        if LinkSet.mem ("", "") acc then
          raise_error x (LinkSet.find ("", "") acc)
        else aux (LinkSet.add ("", "") acc) r
    | _ :: r -> aux acc r
  in
  aux LinkSet.empty l

type feed' =
  [ `Author of author
  | `Category of category
  | `Contributor of author
  | `Generator of generator
  | `Icon of icon
  | `ID of id
  | `Link of link
  | `Logo of logo
  | `Rights of rights
  | `Subtitle of subtitle
  | `Title of title
  | `Updated of updated
  | `Entry of entry ]

let dummy_name = "\000"

let make_entry ~pos l =
  let authors =
    List.fold_left
      (fun acc -> function `Author x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomSource? *)
  let sources =
    List.fold_left
      (fun acc -> function `Source x -> x :: acc | _ -> acc)
      [] l
  in
  let source =
    match sources with
    | [] -> None
    | [s] -> Some s
    | _ ->
        (* RFC 4287 § 4.1.2 *)
        let msg =
          "<entry> elements MUST NOT contain more than one <source> element."
        in
        raise (Error.Error (pos, msg))
  in
  let authors =
    match (authors, source) with
    | a0 :: a, _ -> (a0, a)
    | [], Some (s : source) -> (
      (* If an atom:entry element does not contain atom:author elements, then
         the atom:author elements of the contained atom:source element are
         considered to apply. http://tools.ietf.org/html/rfc4287#section-4.2.1 *)
      match s.authors with
      | a0 :: a -> (a0, a)
      | [] ->
          let msg =
            "<entry> does not contain an <author> and its <source> neither does"
          in
          raise (Error.Error (pos, msg)) )
    | [], None -> ({name= dummy_name; uri= None; email= None}, [])
    (* unacceptable value, see fix_author below *)
    (* atomCategory* *)
  in
  let categories =
    List.fold_left
      (fun acc -> function `Category x -> x :: acc | _ -> acc)
      [] l
    (* atomContributor* *)
  in
  let contributors =
    List.fold_left
      (fun acc -> function `Contributor x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomId *)
  let id =
    match find (function `ID _ -> true | _ -> false) l with
    | Some (`ID i) -> i
    | _ ->
        raise
          (Error.Error
             (pos, "<entry> elements MUST contains exactly one <id> elements"))
    (* atomLink* *)
  in
  let links =
    List.fold_left (fun acc -> function `Link x -> x :: acc | _ -> acc) [] l
  in
  (* atomPublished? *)
  let published =
    match find (function `Published _ -> true | _ -> false) l with
    | Some (`Published s) -> Some s
    | _ -> None
  in
  (* atomRights? *)
  let rights =
    match find (function `Rights _ -> true | _ -> false) l with
    | Some (`Rights r) -> Some r
    | _ -> None
  in
  (* atomContent? *)
  let content =
    match find (function `Content _ -> true | _ -> false) l with
    | Some (`Content c) -> Some c
    | _ -> None
  in
  (* atomSummary? *)
  let summary =
    match find (function `Summary _ -> true | _ -> false) l with
    | Some (`Summary s) -> Some s
    | _ -> None
  in
  (* atomTitle *)
  let title =
    match find (function `Title _ -> true | _ -> false) l with
    | Some (`Title t) -> t
    | _ ->
        raise
          (Error.Error
             ( pos
             , "<entry> elements MUST contains exactly one <title> elements" ))
  in
  (* atomUpdated *)
  let updated =
    match find (function `Updated _ -> true | _ -> false) l with
    | Some (`Updated u) -> u
    | _ ->
        raise
          (Error.Error
             ( pos
             , "<entry> elements MUST contains exactly one <updated> elements"
             ))
  in
  `Entry
    ( pos
    , ( { authors
        ; categories
        ; content
        ; contributors
        ; id
        ; links= uniq_link_alternate ~pos links
        ; published
        ; rights
        ; source
        ; summary
        ; title
        ; updated }
        : entry ) )

(* atomEntry = element atom:entry { atomCommonAttributes, (atomAuthor* &
   atomCategory* & atomContent? & atomContributor* & atomId & atomLink* &
   atomPublished? & atomRights? & atomSource? & atomSummary? & atomTitle &
   atomUpdated & extensionElement * ) } *)
let entry_of_xml =
  let data_producer =
    [ ("author", author_of_xml)
    ; ("category", category_of_xml)
    ; ("contributor", contributor_of_xml)
    ; ("id", id_of_xml); ("link", link_of_xml)
    ; ("published", published_of_xml)
    ; ("rights", rights_of_xml); ("source", source_of_xml)
    ; ("content", content_of_xml)
    ; ("summary", summary_of_xml)
    ; ("title", title_of_xml)
    ; ("updated", updated_of_xml) ]
  in
  generate_catcher ~namespaces ~data_producer make_entry

let entry_of_xml' =
  let data_producer =
    [ ("author", author_of_xml')
    ; ("category", category_of_xml')
    ; ("contributor", contributor_of_xml')
    ; ("id", id_of_xml'); ("link", link_of_xml')
    ; ("published", published_of_xml')
    ; ("rights", rights_of_xml'); ("source", source_of_xml')
    ; ("content", content_of_xml')
    ; ("summary", summary_of_xml')
    ; ("title", title_of_xml')
    ; ("updated", updated_of_xml') ]
  in
  generate_catcher ~namespaces ~data_producer (fun ~pos:_ x -> `Entry x)

type feed =
  { authors: author list
  ; categories: category list
  ; contributors: author list
  ; generator: generator option
  ; icon: icon option
  ; id: id
  ; links: link list
  ; logo: logo option
  ; rights: rights option
  ; subtitle: subtitle option
  ; title: title
  ; updated: updated
  ; entries: entry list }

let feed ?(authors = []) ?(categories = []) ?(contributors = []) ?generator
    ?icon ?(links = []) ?logo ?rights ?subtitle ~id ~title ~updated entries =
  { authors
  ; categories
  ; contributors
  ; generator
  ; icon
  ; id
  ; links
  ; logo
  ; rights
  ; subtitle
  ; title
  ; updated
  ; entries }

let make_feed ~pos (l : _ list) =
  (* atomAuthor* *)
  let authors =
    List.fold_left
      (fun acc -> function `Author x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomCategory* *)
  let categories =
    List.fold_left
      (fun acc -> function `Category x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomContributor* *)
  let contributors =
    List.fold_left
      (fun acc -> function `Contributor x -> x :: acc | _ -> acc)
      [] l
  in
  (* atomLink* *)
  let links =
    List.fold_left (fun acc -> function `Link x -> x :: acc | _ -> acc) [] l
  in
  (* atomGenerator? *)
  let generator =
    match find (function `Generator _ -> true | _ -> false) l with
    | Some (`Generator g) -> Some g
    | _ -> None
  in
  (* atomIcon? *)
  let icon =
    match find (function `Icon _ -> true | _ -> false) l with
    | Some (`Icon i) -> Some i
    | _ -> None
  in
  (* atomId *)
  let id =
    match find (function `ID _ -> true | _ -> false) l with
    | Some (`ID i) -> i
    | _ ->
        raise
          (Error.Error
             (pos, "<feed> elements MUST contains exactly one <id> elements"))
  in
  (* atomLogo? *)
  let logo =
    match find (function `Logo _ -> true | _ -> false) l with
    | Some (`Logo l) -> Some l
    | _ -> None
  in
  (* atomRights? *)
  let rights =
    match find (function `Rights _ -> true | _ -> false) l with
    | Some (`Rights r) -> Some r
    | _ -> None
  in
  (* atomSubtitle? *)
  let subtitle =
    match find (function `Subtitle _ -> true | _ -> false) l with
    | Some (`Subtitle s) -> Some s
    | _ -> None
  in
  (* atomTitle *)
  let title =
    match find (function `Title _ -> true | _ -> false) l with
    | Some (`Title t) -> t
    | _ ->
        raise
          (Error.Error
             (pos, "<feed> elements MUST contains exactly one <title> elements"))
  in
  (* atomUpdated *)
  let updated =
    match find (function `Updated _ -> true | _ -> false) l with
    | Some (`Updated u) -> u
    | _ ->
        raise
          (Error.Error
             ( pos
             , "<feed> elements MUST contains exactly one <updated> elements"
             ))
  in
  (* atomEntry* *)
  let fix_author pos (e : entry) =
    match e.authors with
    | a, [] when a.name = dummy_name -> (
      (* In an Atom Feed Document, the atom:author elements of the containing
         atom:feed element are considered to apply to the entry if there are no
         atom:author elements in the locations described above.
         http://tools.ietf.org/html/rfc4287#section-4.2.1 *)
      match authors with
      | a0 :: a -> {e with authors= (a0, a)}
      | [] ->
          let msg =
            "<entry> elements MUST contains at least an <author> element or \
             <feed> element MUST contains one or more <author> elements"
          in
          raise (Error.Error (pos, msg)) )
    | _ -> e
  in
  let entries =
    List.fold_left
      (fun acc -> function `Entry (pos, e) -> fix_author pos e :: acc
        | _ -> acc )
      [] l
  in
  ( { authors
    ; categories
    ; contributors
    ; generator
    ; icon
    ; id
    ; links
    ; logo
    ; rights
    ; subtitle
    ; title
    ; updated
    ; entries }
    : feed )

(* atomFeed = element atom:feed { atomCommonAttributes, (atomAuthor* &
   atomCategory* & atomContributor* & atomGenerator? & atomIcon? & atomId &
   atomLink* & atomLogo? & atomRights? & atomSubtitle? & atomTitle &
   atomUpdated & extensionElement * ), atomEntry* } *)

let feed_of_xml =
  let data_producer =
    [ ("author", author_of_xml)
    ; ("category", category_of_xml)
    ; ("contributor", contributor_of_xml)
    ; ("generator", generator_of_xml)
    ; ("icon", icon_of_xml); ("id", id_of_xml); ("link", link_of_xml)
    ; ("logo", logo_of_xml); ("rights", rights_of_xml)
    ; ("subtitle", subtitle_of_xml)
    ; ("title", title_of_xml)
    ; ("updated", updated_of_xml)
    ; ("entry", entry_of_xml) ]
  in
  generate_catcher ~namespaces ~data_producer make_feed

let feed_of_xml' =
  let data_producer =
    [ ("author", author_of_xml')
    ; ("category", category_of_xml')
    ; ("contributor", contributor_of_xml')
    ; ("generator", generator_of_xml')
    ; ("icon", icon_of_xml'); ("id", id_of_xml'); ("link", link_of_xml')
    ; ("logo", logo_of_xml'); ("rights", rights_of_xml')
    ; ("subtitle", subtitle_of_xml')
    ; ("title", title_of_xml')
    ; ("updated", updated_of_xml')
    ; ("entry", entry_of_xml') ]
  in
  generate_catcher ~namespaces ~data_producer (fun ~pos:_ x -> x)

(* Remove all tags *)
let rec add_to_buffer buf = function
  | XML.Node (_, _, subs) -> List.iter (add_to_buffer buf) subs
  | XML.Data (_, d) -> Buffer.add_string buf d

let xhtml_to_string xhtml =
  let buf = Buffer.create 128 in
  List.iter (add_to_buffer buf) xhtml ;
  Buffer.contents buf

let string_of_text_construct = function
  (* FIXME: Once we use a proper HTML library, we probably would like to parse
     the HTML and remove the tags *)
  | (Text s : text_construct) | Html (_, s) -> s
  | Xhtml (_, x) -> xhtml_to_string x

let parse ?self ?xmlbase input =
  let feed =
    match XML.of_xmlm input |> snd with
    | XML.Node (pos, tag, datas) when tag_is tag "feed" ->
        feed_of_xml ~xmlbase (pos, tag, datas)
    | _ ->
        raise
          (Error.Error
             ((0, 0), "document MUST contains exactly one <feed> element"))
  in
  (* FIXME: the spec says that an entry can appear as the top-level element *)
  match self with
  | None -> feed
  | Some self ->
      if List.exists (fun l -> l.rel = Self) feed.links then feed
      else
        let links =
          { href= self
          ; rel= Self
          ; type_media= Some "application/atom+xml"
          ; hreflang= None
          ; title= string_of_text_construct feed.title
          ; length= None }
          :: feed.links
        in
        {feed with links}

let read ?self ?xmlbase fname =
  let fh = open_in fname in
  try
    let x = parse ?self ?xmlbase (XML.input_of_channel fh) in
    close_in fh ; x
  with e -> close_in fh ; raise e

let set_self_link feed ?hreflang ?length url =
  match List.partition (fun l -> l.rel = Self) feed.links with
  | l :: _, links ->
      let hreflang =
        match hreflang with None -> l.hreflang | Some _ -> hreflang
      in
      let length = match length with None -> l.length | Some _ -> length in
      let self = {l with href= url; hreflang; length} in
      {feed with links= self :: links}
  | [], links ->
      let links =
        { href= url
        ; rel= Self
        ; type_media= Some "application/atom+xml"
        ; hreflang
        ; title= string_of_text_construct feed.title
        ; length }
        :: links
      in
      {feed with links}

let get_self_link feed =
  try Some (List.find (fun l -> l.rel = Self) feed.links) with Not_found ->
    None

let unsafe ?xmlbase input =
  match XML.of_xmlm input |> snd with
  | XML.Node (pos, tag, datas) when tag_is tag "feed" ->
      `Feed (feed_of_xml' ~xmlbase (pos, tag, datas))
  | _ -> `Feed []

let remove_empty_authors a = List.filter not_empty_author a

(* [normalize_authors a authors] returns (a', authors') where [authors'] is
   [authors] where the empty authors and the author [a] have been removed and
   [a'] is [a] possibly completed with the information found for [a] in
   [authors]. *)
let rec normalize_authors (a : author) = function
  | [] -> (a, [])
  | a0 :: tl ->
      if not_empty_author a0 then
        if a0.name = a.name then
          (* Merge [a0] and [a]. *)
          let uri = match a.uri with None -> a0.uri | Some _ -> a.uri in
          let email =
            match a.email with None -> a0.email | Some _ -> a.email
          in
          normalize_authors {name= a.name; uri; email} tl
        else
          let a', authors' = normalize_authors a tl in
          (a', a0 :: authors')
      else normalize_authors a tl

(* drop the empty author *)

let set_main_author_entry author (e : entry) =
  (* If the entry has a source, then [author] should be ignored and the one
     from the [source] should be used instead. *)
  let author, author_ok, source =
    match e.source with
    | None -> (author, true, None)
    | Some s -> (
        let s_authors = remove_empty_authors s.authors in
        let s_contributors = remove_empty_authors s.contributors in
        let s =
          Some {s with authors= s_authors; contributors= s_contributors}
        in
        (* A source exists. If it contains no author, one should not change the
           entry authors with [author] because that may wrongly attribute the
           post. *)
        match s_authors with
        | [] -> (author, false, s)
        | s_author :: _ -> (s_author, true, s) )
  in
  let a0, a = e.authors in
  let authors =
    match remove_empty_authors (a0 :: a) with
    | a0 :: a -> (a0, a)
    | [] -> ((if author_ok then author else empty_author), [])
  in
  let contributors = remove_empty_authors e.contributors in
  {e with authors; contributors; source}

let set_main_author feed author =
  let author, authors = normalize_authors author feed.authors in
  let contributors = remove_empty_authors feed.contributors in
  let entries = List.map (set_main_author_entry author) feed.entries in
  {feed with authors= author :: authors; contributors; entries}

(* Conversion to XML *)

(* Tag with the Atom namespace *)
let atom name : XML.tag = ((atom_ns, name), [])

let add_attr_xmlbase ~xmlbase attrs =
  match xmlbase with
  | Some u -> ((Xmlm.ns_xml, "base"), Uri.to_string u) :: attrs
  | None -> attrs

let text_construct_to_xml tag_name (t : text_construct) =
  match t with
  | Text t ->
      XML.Node
        ( dummy_pos
        , ((atom_ns, tag_name), [(("", "type"), "text")])
        , [XML.Data (dummy_pos, t)] )
  | Html (xmlbase, t) ->
      let attr = add_attr_xmlbase ~xmlbase [(("", "type"), "html")] in
      XML.Node
        (dummy_pos, ((atom_ns, tag_name), attr), [XML.Data (dummy_pos, t)])
  | Xhtml (xmlbase, x) ->
      let div =
        XML.Node
          (dummy_pos, ((xhtml_ns, "div"), [(("", "xmlns"), xhtml_ns)]), x)
      in
      let attr = add_attr_xmlbase ~xmlbase [(("", "type"), "xhtml")] in
      XML.Node (dummy_pos, ((atom_ns, tag_name), attr), [div])

let person_to_xml name (a : author) =
  XML.Node
    ( dummy_pos
    , atom name
    , [node_data (atom "name") a.name]
      |> add_node_uri (atom "uri") a.uri
      |> add_node_data (atom "email") a.email )

let author_to_xml a = person_to_xml "author" a
let contributor_to_xml a = person_to_xml "contributor" a

let category_to_xml (c : category) =
  let attrs =
    [(("", "term"), c.term)]
    |> add_attr_uri ("", "scheme") c.scheme
    |> add_attr ("", "label") c.label
  in
  XML.Node (dummy_pos, ((atom_ns, "category"), attrs), [])

let generator_to_xml (g : generator) =
  let attr =
    [] |> add_attr ("", "version") g.version |> add_attr_uri ("", "uri") g.uri
  in
  XML.Node
    ( dummy_pos
    , ((atom_ns, "generator"), attr)
    , [XML.Data (dummy_pos, g.content)] )

let string_of_rel = function
  | Alternate -> "alternate"
  | Related -> "related"
  | Self -> "self"
  | Enclosure -> "enclosure"
  | Via -> "via"
  | Link l -> Uri.to_string l

let link_to_xml (l : link) =
  let attr =
    [(("", "href"), Uri.to_string l.href); (("", "rel"), string_of_rel l.rel)]
    |> add_attr ("", "type") l.type_media
    |> add_attr ("", "hreflang") l.hreflang
  in
  let attr = if l.title = "" then attr else (("", "title"), l.title) :: attr in
  let attr =
    match l.length with
    | Some len -> (("", "length"), string_of_int len) :: attr
    | None -> attr
  in
  XML.Node (dummy_pos, ((atom_ns, "link"), attr), [])

let add_node_date tag date nodes =
  match date with
  | None -> nodes
  | Some d -> node_data tag (Date.to_rfc3339 d) :: nodes

let source_to_xml (s : source) =
  let nodes =
    node_data (atom "id") (Uri.to_string s.id)
    :: text_construct_to_xml "title" s.title
    :: List.map author_to_xml s.authors
    |> add_nodes_rev_map category_to_xml s.categories
    |> add_nodes_rev_map contributor_to_xml s.contributors
    |> add_node_option generator_to_xml s.generator
    |> add_node_option (node_uri (atom "icon")) s.icon
    |> add_nodes_rev_map link_to_xml s.links
    |> add_node_option (node_uri (atom "logo")) s.logo
    |> add_node_option (text_construct_to_xml "rights") s.rights
    |> add_node_option (text_construct_to_xml "subtitle") s.subtitle
    |> add_node_date (atom "updated") s.updated
  in
  XML.Node (dummy_pos, atom "source", nodes)

let content_to_xml (c : content) =
  match c with
  | Text t ->
      XML.Node
        ( dummy_pos
        , ((atom_ns, "content"), [(("", "type"), "text")])
        , [XML.Data (dummy_pos, t)] )
  | Html (xmlbase, t) ->
      let attrs = add_attr_xmlbase ~xmlbase [(("", "type"), "html")] in
      XML.Node
        (dummy_pos, ((atom_ns, "content"), attrs), [XML.Data (dummy_pos, t)])
  | Xhtml (xmlbase, x) ->
      let div =
        XML.Node
          (dummy_pos, ((xhtml_ns, "div"), [(("", "xmlns"), xhtml_ns)]), x)
      in
      let attrs = add_attr_xmlbase ~xmlbase [(("", "type"), "xhtml")] in
      XML.Node (dummy_pos, ((atom_ns, "content"), attrs), [div])
  | Mime (mime, d) ->
      XML.Node
        ( dummy_pos
        , ((atom_ns, "content"), [(("", "type"), mime)])
        , [XML.Data (dummy_pos, d)] )
  | Src (mime, uri) ->
      let attr =
        [(("", "src"), Uri.to_string uri)] |> add_attr ("", "type") mime
      in
      XML.Node (dummy_pos, ((atom_ns, "content"), attr), [])

let entry_to_xml (e : entry) =
  let a0, a = e.authors in
  let nodes =
    node_data (atom "id") (Uri.to_string e.id)
    :: text_construct_to_xml "title" e.title
    :: node_data (atom "updated") (Date.to_rfc3339 e.updated)
    :: author_to_xml a0
    :: List.map author_to_xml a
    |> add_nodes_rev_map category_to_xml e.categories
    |> add_node_option content_to_xml e.content
    |> add_nodes_rev_map contributor_to_xml e.contributors
    |> add_nodes_rev_map link_to_xml e.links
    |> add_node_date (atom "published") e.published
    |> add_node_option (text_construct_to_xml "rights") e.rights
    |> add_node_option source_to_xml e.source
    |> add_node_option (text_construct_to_xml "summary") e.summary
  in
  XML.Node (dummy_pos, atom "entry", nodes)

let to_xml (f : feed) =
  let nodes =
    node_data (atom "id") (Uri.to_string f.id)
    :: text_construct_to_xml "title" f.title
    :: node_data (atom "updated") (Date.to_rfc3339 f.updated)
    :: List.map entry_to_xml f.entries
    |> add_nodes_rev_map author_to_xml (List.rev f.authors)
    |> add_nodes_rev_map category_to_xml f.categories
    |> add_nodes_rev_map contributor_to_xml f.contributors
    |> add_node_option generator_to_xml f.generator
    |> add_node_option (node_uri (atom "icon")) f.icon
    |> add_nodes_rev_map link_to_xml f.links
    |> add_node_option (node_uri (atom "logo")) f.logo
    |> add_node_option (text_construct_to_xml "rights") f.rights
    |> add_node_option (text_construct_to_xml "subtitle") f.subtitle
  in
  XML.Node (dummy_pos, ((atom_ns, "feed"), [(("", "xmlns"), atom_ns)]), nodes)

(* Atom and XHTML have been declared well in the above XML representation. One
   can remove them. *)
let output_ns_prefix s = if s = atom_ns || s = xhtml_ns then Some "" else None

let output feed dest =
  let o = XML.make_output dest ~ns_prefix:output_ns_prefix in
  XML.to_xmlm (to_xml feed) o

let write feed fname =
  let fh = open_out fname in
  try
    output feed (`Channel fh) ;
    close_out fh
  with e -> close_out fh ; raise e

(* Comparing entries *)

let entry_date e = match e.published with Some d -> d | None -> e.updated

let ascending (e1 : entry) (e2 : entry) =
  Date.compare (entry_date e1) (entry_date e2)

let descending (e1 : entry) (e2 : entry) =
  Date.compare (entry_date e2) (entry_date e1)

(* Feed aggregation *)

let syndic_generator =
  { version= Some Syndic_conf.version
  ; uri= Some Syndic_conf.homepage
  ; content= "OCaml Syndic.Atom feed aggregator" }

let ocaml_icon = Uri.of_string "http://ocaml.org/img/colour-icon-170x148.png"
let default_title : text_construct = Text "Syndic.Atom aggregated feed"

let[@warning "-32"] is_alternate_Atom (l : link) =
  match l.type_media with
  | None -> false
  | Some ty -> ty = "application/atom+xml" && l.rel = Alternate

let add_entries_of_feed entries feed : entry list =
  let source_of_feed =
    Some
      { authors= feed.authors
      ; categories= feed.categories
      ; contributors= feed.contributors
      ; generator= feed.generator
      ; icon= feed.icon
      ; id= feed.id
      ; links= feed.links
      ; logo= feed.logo
      ; rights= feed.rights
      ; subtitle= feed.subtitle
      ; title= feed.title
      ; updated= Some feed.updated }
  in
  let add_entry entries (e : entry) =
    match e.source with
    | Some _ -> e :: entries (* if a source is present, do not overwrite it. *)
    | None -> {e with source= source_of_feed} :: entries
  in
  List.fold_left add_entry entries feed.entries

let entries_of_feeds feeds = List.fold_left add_entries_of_feed [] feeds

let more_recent d1 (e : entry) =
  if Date.compare d1 e.updated >= 0 then d1 else e.updated

let aggregate ?self ?id ?updated ?subtitle ?(title = default_title)
    ?(sort = `Newest_first) ?n feeds : feed =
  let entries = entries_of_feeds feeds in
  let entries =
    match sort with
    | `Newest_first -> List.sort descending entries
    | `Oldest_first -> List.sort ascending entries
    | `None -> entries
  in
  let entries = match n with Some n -> take entries n | None -> entries in
  let id =
    match id with
    | Some id -> id
    | None ->
        (* Collect all ids of the entries and "digest" them. *)
        let b = Buffer.create 4096 in
        let add_id (e : entry) = Buffer.add_string b (Uri.to_string e.id) in
        List.iter add_id entries ;
        let d = Digest.to_hex (Digest.string (Buffer.contents b)) in
        (* FIXME: use urn:uuid *)
        Uri.of_string ("urn:md5:" ^ d)
  in
  let links =
    match self with
    | Some u ->
        [ link u
            ~title:(string_of_text_construct title)
            ~rel:Self ~type_media:"application/atom+xml" ]
    | None -> []
  in
  let updated =
    match updated with
    | Some d -> d
    | None -> (
      (* Use the more recent date of the entries. *)
      match entries with
      | [] -> Date.epoch
      | e0 :: el -> List.fold_left more_recent e0.updated el )
  in
  { authors= []
  ; categories= []
  ; contributors= []
  ; generator= Some syndic_generator
  ; icon= Some ocaml_icon
  ; id
  ; links
  ; logo= None
  ; rights= None
  ; subtitle
  ; title
  ; updated
  ; entries }
OCaml

Innovation. Community. Security.