package yocaml_syndication

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file yocaml_syndication.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
module Atom = struct
  type t = Syndic.Atom.feed

  let default_generator =
    { Syndic.Atom.version = Some "dev"
    ; uri = Some (Uri.of_string "https://github.com/xhtmlboi/yocaml")
    ; content = "YOcaml"
    }
  ;;

  let make
    ?authors
    ?categories
    ?contributors
    ?(generator = default_generator)
    ?icon
    ?links
    ?logo
    ?rights
    ?subtitle
    ~id
    ~title
    ~updated
    =
    Syndic.Atom.feed
      ?authors
      ?categories
      ?contributors
      ?icon
      ~generator
      ?links
      ?logo
      ?rights
      ?subtitle
      ~id
      ~title
      ~updated
  ;;

  let pp ppf feed =
    Format.fprintf ppf "%s" (Syndic.Atom.to_xml feed |> Syndic.XML.to_string)
  ;;

  let pp_atom ?(xml_version = "1.0") ?(encoding = "UTF-8") ppf feed =
    Format.fprintf
      ppf
      "<?xml version=%S encoding=%S?>%s"
      xml_version
      encoding
      (Syndic.Atom.to_xml feed
      |> Syndic.XML.to_string ~ns_prefix:(function
           | "http://www.w3.org/2005/Atom" -> Some ""
           | _ -> Some "http://www.w3.org/2005/Atom"))
  ;;

  let to_atom ?xml_version ?encoding feed =
    Format.asprintf "%a" (pp_atom ?xml_version ?encoding) feed
  ;;

  let entry_of_article url authors article =
    let module Article = Yocaml.Metadata.Article in
    let (year, month, day), time =
      Article.date article |> Yocaml.Date.to_pair
    in
    let time = Option.value time ~default:(0, 0, 0) in
    match
      Ptime.of_date_time
        ((year, Yocaml.Date.month_to_int month, day), (time, 0))
    with
    | None -> failwith "article date is not representable!"
    | Some updated ->
      Syndic.Atom.entry
        ~id:url
        ~title:(Text (Article.article_title article))
        ~authors
        ~updated
        ~summary:(Text (Article.article_description article))
        ()
  ;;
end