aboutsummaryrefslogtreecommitdiff
path: root/modules/atom.nim
blob: ed78e47bebc03aa46419f32fdf3b0afcb0bbaf1e (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
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
# Nim Atom Syndication Format module

# Written by John Conway
# Released under the MIT open source license.

import httpclient
import strutils
import sequtils
import xmlparser
import xmltree
import streams
import sugar

type
    Atom* = object
        author: AtomAuthor          #Sugar, not in Atom spec. Returns the first author.
        authors*: seq[AtomAuthor]   #Pleuralised because the Atom spec allows more than one
        category*: seq[string]
        generator*: string
        icon*: string
        id*: string
        link*: AtomLink
        logo*: string
        rights*: string
        subtitle*: string
        title*: string
        updated*: string
        entrys*: seq[AtomEntry]

    AtomAuthor* = object
        name*: string
        url*: string
        email*: string

    AtomImage* = object
        url*: string
        title*: string
        link*: string
        width*: string
        height*: string
        description*: string

    AtomLink* = object
        href*: string
        rel*: string
        linktype*: string
        hreflang*: string
        title*: string
        length*: string

    AtomEntry* = object
        author: AtomAuthor         #Sugar, not in Atom spec. Returns the first author.
        authors*: seq[AtomAuthor]  #Pleuralised because the Atom spec allows more than one
        category*: seq[string]
        content*: string
        contentSrc*: string
        contentType*: string
        id*: string
        link*: AtomLink
        published*: string
        rights*: string
        source*: string
        summary*: string
        title*: string
        updated*: string


proc parseEntry( node: XmlNode) : AtomEntry =
    var entry: AtomEntry = AtomEntry()

    if node.child("author") != nil:
        for con_node in node.findAll("author"):
            var author: AtomAuthor = AtomAuthor()
            if con_node.child("name") != nil: author.name = con_node.child("name").innerText
            if con_node.child("url") != nil: author.url = con_node.child("url").innerText
            if con_node.child("email") != nil: author.email = con_node.child("email").innerText
            entry.authors.add(author)

    if node.child("category") != nil:
        entry.category = map(node.findAll("category"), (x: XmlNode) -> string => x.innerText)

    if node.child("content") != nil:
        entry.content = node.child("content").innerText
        if node.attrs != nil:
            if node.attr("type") != "": entry.contentType = node.attr("type")
            if node.attr("src") != "": entry.contentSrc = node.attr("src")

    if node.child("contributer") != nil:
        for con_node in node.findAll("author"):
            var author: AtomAuthor = AtomAuthor()
            if con_node.child("name") != nil: author.name = con_node.child("name").innerText
            if con_node.child("url") != nil: author.url = con_node.child("url").innerText
            if con_node.child("email") != nil: author.email = con_node.child("email").innerText
            entry.authors.add(author)

    if node.child("id") != nil: entry.id = node.child("id").innerText

    if node.child("link") != nil:
        if node.attrs != nil:
            if node.attr("href") != "": entry.link.href = node.attr("href")
            if node.attr("rel") != "": entry.link.rel = node.attr("rel")
            if node.attr("type") != "": entry.link.linktype = node.attr("type")
            if node.attr("hreflang") != "": entry.link.rel = node.attr("hreflang")
            if node.attr("title") != "": entry.link.rel = node.attr("title")
            if node.attr("length") != "": entry.link.rel = node.attr("length")

    if node.child("published") != nil: entry.published = node.child("published").innerText

    if node.child("rights") != nil: entry.rights = node.child("rights").innerText

    if node.child("summary") != nil: entry.summary = node.child("summary").innerText

    if node.child("title") != nil: entry.title = node.child("title").innerText

    if node.child("updated") != nil: entry.updated = node.child("updated").innerText

    # SUGAR

    if entry.authors.len() > 0:
        entry.author = entry.authors[0]
    else:
        entry.author = AtomAuthor()

    return entry

proc parseAtom*(data: string): Atom =
    ## Parses the Atom from the given string.

    # Parse into XML.
    let node: XmlNode = parseXML(newStringStream(data))

    # Create the return object.
    var atom: Atom = Atom()

    if node.child("author") != nil:
        for con_node in node.findAll("author"):
            var author: AtomAuthor = AtomAuthor()
            if con_node.child("name") != nil: author.name = con_node.child("name").innerText
            if con_node.child("url") != nil: author.url = con_node.child("url").innerText
            if con_node.child("email") != nil: author.email = con_node.child("email").innerText
            atom.authors.add(author)

    if node.child("category") != nil:
        atom.category = map(node.findAll("category"), (x: XmlNode) -> string => x.innerText)

    if node.child("generator") != nil: atom.generator = node.child("generator").innerText

    if node.child("icon") != nil: atom.icon = node.child("icon").innerText

    if node.child("id") != nil: atom.id = node.child("id").innerText

    if node.child("link") != nil:
        if node.attrs != nil:
            if node.attr("href") != "": atom.link.href = node.attr("href")
            if node.attr("rel") != "": atom.link.rel = node.attr("rel")
            if node.attr("type") != "": atom.link.linktype = node.attr("type")
            if node.attr("hreflang") != "": atom.link.rel = node.attr("hreflang")
            if node.attr("title") != "": atom.link.rel = node.attr("title")
            if node.attr("length") != "": atom.link.rel = node.attr("length")

    if node.child("logo") != nil: atom.logo = node.child("logo").innerText

    if node.child("rights") != nil: atom.rights = node.child("rights").innerText

    if node.child("title") != nil: atom.title = node.child("title").innerText

    if node.child("subtitle") != nil: atom.subtitle = node.child("subtitle").innerText

    if node.child("updated") != nil: atom.updated = node.child("updated").innerText

    if atom.authors.len() > 0:
        atom.author = atom.authors[0]
    else:
        atom.author = AtomAuthor()

    # If there are no entrys:
    if node.child("entry") == nil:
        atom.entrys = @[]
        return atom

    # Otherwise, add the entrys.
    if node.child("entry") != nil:
        atom.entrys = map( node.findAll("entry"), parseEntry )

    # Return the Atom data.
    return atom