package yocaml_syndication

  1. Overview
  2. Docs

Source file opml.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
(* YOCaml a static blog generator.
   Copyright (C) 2024 The Funkyworkers and The YOCaml's developers

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <https://www.gnu.org/licenses/>. *)

module Head = struct
  type t = {
      title : string option
    ; date_created : Datetime.t option
    ; date_modified : Datetime.t option
    ; owner : Person.t option
    ; expansion_state : int list
    ; vert_scroll_state : int option
    ; window_top : int option
    ; window_left : int option
    ; window_bottom : int option
    ; window_right : int option
  }

  let make ?title ?date_created ?date_modified ?owner ?(expansion_state = [])
      ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right
      () =
    {
      title
    ; date_created
    ; date_modified
    ; owner
    ; expansion_state
    ; vert_scroll_state
    ; window_top
    ; window_left
    ; window_right
    ; window_bottom
    }

  let expand = function [] -> None | list -> Some list

  let to_xml
      {
        title
      ; date_created
      ; date_modified
      ; owner
      ; expansion_state
      ; vert_scroll_state
      ; window_top
      ; window_left
      ; window_right
      ; window_bottom
      } =
    let open Xml in
    [
      may_leaf ~name:"title" Fun.id title
    ; may_leaf ~name:"dateCreated" Datetime.to_string date_created
    ; may_leaf ~name:"dateModified" Datetime.to_string date_modified
    ; may Person.to_owner_name owner
    ; may Person.to_owner_email owner
    ; may_leaf ~name:"expansionState"
        (fun x -> x |> List.map string_of_int |> String.concat ",")
        (expand expansion_state)
    ; may_leaf ~name:"vertScrollState" string_of_int vert_scroll_state
    ; may_leaf ~name:"windowTop" string_of_int window_top
    ; may_leaf ~name:"windowLeft" string_of_int window_left
    ; may_leaf ~name:"windowRight" string_of_int window_right
    ; may_leaf ~name:"windowBottom" string_of_int window_bottom
    ]

  let to_opml1 head = Xml.node ~name:"head" (to_xml head)

  let to_opml2 head =
    Xml.node ~name:"head"
      (to_xml head
      @ [
          Xml.leaf ~name:"docs" (Some "http://opml.org/spec2.opml")
        ; Xml.may Person.to_owner_id head.owner
        ])
end

module Outline = struct
  type t = {
      text : string
    ; typ : string option
    ; title : string option
    ; is_comment : bool option
    ; is_breakpoint : bool option
    ; xml_url : string option
    ; html_url : string option
    ; categories : string list
    ; attr : Xml.Attr.t list
    ; outlines : t list
  }

  let make ?typ ?is_comment ?is_breakpoint ?xml_url ?html_url ?(attr = [])
      ?(categories = []) ?title ~text outlines =
    {
      text
    ; typ
    ; title
    ; is_breakpoint
    ; is_comment
    ; xml_url
    ; html_url
    ; categories
    ; attr
    ; outlines
    }

  let rec to_opml
      {
        text
      ; typ
      ; title
      ; is_breakpoint
      ; is_comment
      ; xml_url
      ; html_url
      ; categories
      ; attr
      ; outlines
      } =
    let text = Xml.Attr.string ~key:"text" text in
    let is_comment =
      is_comment
      |> Option.map (Xml.Attr.bool ~key:"isComment")
      |> Option.to_list
    in
    let is_breakpoint =
      is_breakpoint
      |> Option.map (Xml.Attr.bool ~key:"isBreakpoint")
      |> Option.to_list
    in
    let typ =
      typ |> Option.map (Xml.Attr.string ~key:"type") |> Option.to_list
    in
    let title =
      title |> Option.map (Xml.Attr.string ~key:"title") |> Option.to_list
    in
    let xml_url =
      xml_url |> Option.map (Xml.Attr.string ~key:"xmlUrl") |> Option.to_list
    in
    let html_url =
      html_url |> Option.map (Xml.Attr.string ~key:"htmlUrl") |> Option.to_list
    in
    let categories =
      match categories with
      | [] -> []
      | categories ->
          [ String.concat "," categories |> Xml.Attr.string ~key:"category" ]
    in
    let attr =
      typ
      @ xml_url
      @ html_url
      @ title
      @ attr
      @ categories
      @ is_breakpoint
      @ is_comment
      @ [ text ]
    in
    Xml.node ~name:"outline" ~attr (List.map to_opml outlines)
end

module Body = struct
  type t = Outline.t list

  let to_opml l = Xml.node ~name:"body" (List.map Outline.to_opml l)
end

module Feed = struct
  type t = Head.t * Body.t

  let make ~head body = (head, body)

  let to_opml1 ?encoding ?standalone (head, body) =
    Xml.document ~version:"1.0" ?encoding ?standalone
      (Xml.node ~name:"opml"
         ~attr:Xml.Attr.[ string ~key:"version" "1.0" ]
         [ Head.to_opml1 head; Body.to_opml body ])

  let to_opml2 ?encoding ?standalone (head, body) =
    Xml.document ~version:"1.0" ?encoding ?standalone
      (Xml.node ~name:"opml"
         ~attr:Xml.Attr.[ string ~key:"version" "2.0" ]
         [ Head.to_opml2 head; Body.to_opml body ])
end

type outline = Outline.t
type t = Feed.t

let outline = Outline.make

let inclusion ~url ~text =
  outline ~typ:"link" ~attr:Xml.Attr.[ string ~key:"url" url ] ~text []

let subscription ?version ?description ?html_url ?language ~title ~feed_url () =
  let attr =
    let description =
      description
      |> Option.map (Xml.Attr.string ~key:"description")
      |> Option.to_list
    in
    let language =
      language |> Option.map (Xml.Attr.string ~key:"language") |> Option.to_list
    in
    let version =
      version |> Option.map (Xml.Attr.string ~key:"version") |> Option.to_list
    in
    description @ language @ version @ []
  in
  outline ~typ:"rss" ~xml_url:feed_url ~attr ?html_url ~text:title ~title []

let feed ?title ?date_created ?date_modified ?owner ?expansion_state
    ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right
    outlines =
  let head =
    Head.make ?title ?date_created ?date_modified ?owner ?expansion_state
      ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right
      ()
  in
  Feed.make ~head outlines

let to_opml1 ?encoding ?standalone feed =
  Feed.to_opml1 ?encoding ?standalone feed

let to_opml2 ?encoding ?standalone feed =
  Feed.to_opml2 ?encoding ?standalone feed

let from ?title ?date_created ?date_modified ?owner ?expansion_state
    ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right ()
    =
  Yocaml.Task.lift (fun outlines ->
      feed ?title ?date_created ?date_modified ?owner ?expansion_state
        ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right
        outlines)

let opml2_from ?encoding ?standalone ?title ?date_created ?date_modified ?owner
    ?expansion_state ?vert_scroll_state ?window_top ?window_left ?window_bottom
    ?window_right () =
  let open Yocaml.Task in
  from ?title ?date_created ?date_modified ?owner ?expansion_state
    ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right ()
  >>> lift (fun feed ->
          feed |> Feed.to_opml2 ?encoding ?standalone |> Xml.to_string)

let opml1_from ?encoding ?standalone ?title ?date_created ?date_modified ?owner
    ?expansion_state ?vert_scroll_state ?window_top ?window_left ?window_bottom
    ?window_right () =
  let open Yocaml.Task in
  from ?title ?date_created ?date_modified ?owner ?expansion_state
    ?vert_scroll_state ?window_top ?window_left ?window_bottom ?window_right ()
  >>> lift (fun feed ->
          feed |> Feed.to_opml1 ?encoding ?standalone |> Xml.to_string)
OCaml

Innovation. Community. Security.