aboutsummaryrefslogtreecommitdiff
path: root/lib/xml.ml
blob: d5f8df3e81faa78278d1ecb838bd114f1a7e07eb (plain)
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
type attribute = string * string

type xml =
  | Tag of string * attribute list * xml list
  | String of string
  | Closing_tag of string * attribute list

let tag name attributes body = Tag (name, attributes, body)
let entry value = String value
let closing_tag name attributes = Closing_tag (name, attributes)

let format_attributes attributes =
  let format_attribut (name, value) = Printf.sprintf " %s=\"%s\"" name value in
  String.concat "" (List.map format_attribut attributes)

let rec format = function
  | Tag (name, attributes, body) ->
      let body = List.map format body in
      String.concat ""
        [
          "<";
          name;
          " ";
          format_attributes attributes;
          ">";
          String.concat "" body;
          "</";
          name;
          ">";
        ]
  | String text -> text
  | Closing_tag (name, attributes) ->
      String.concat "" [ "<"; name; " "; format_attributes attributes; "/>" ]

let format_rss host_name last_updated entries =
  tag "feed"
    [ ("xml:base", host_name) ]
    [
      tag "title" [] [ entry "Sensemaking Galaxy" ];
      tag "subtitle" [] [ entry "A collection of sensemaking links" ];
      closing_tag "link" [ ("href", host_name ^ "/feed.xml"); ("rel", "self") ];
      closing_tag "link" [ ("href", host_name) ];
      tag "id" [] [ entry host_name ];
      tag "updated" [] [ entry last_updated ];
      tag "author" [] [ tag "name" [] [ entry "Sensemaking Galaxy" ] ];
      tag "entry" [] entries;
    ]
  |> format