blob: b771bc8116f03b53ca534db3dab5036c892a283f (
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
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
|
# 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.
id*: string # Required Atom field
title*: string # Required Atom field
updated*: string # Required Atom field
authors*: seq[AtomAuthor] # Pleuralised because the Atom spec allows more than one
categories*: seq[AtomCategory]
contributors*: seq[AtomAuthor]
generator*: string
icon*: string
link*: AtomLink
logo*: string
rights*: string
subtitle*: string
entries*: seq[AtomEntry]
AtomAuthor* = object
name*: string # Required Atom field
uri*: string
email*: string
AtomCategory* = object
term*: string
label*: string
scheme*: string
AtomLink* = object
href*: string
rel*: string
linktype*: string
hreflang*: string
title*: string
length*: string
AtomEntry* = object
id*: string # Required Atom field
title*: string # Required Atom field
updated*: string # Required Atom field
author: AtomAuthor # Sugar, not in Atom spec. Returns the first author.
authors*: seq[AtomAuthor] # Pleuralised because the Atom spec allows more than one
categories*: seq[AtomCategory]
content*: string
contentSrc*: string
contentType*: string
contributors*: seq[AtomAuthor]
link*: AtomLink
published*: string
rights*: string
source*: AtomSource
summary*: string
AtomSource* = object
author: AtomAuthor # Sugar, not in Atom spec. Returns the first author.
authors: seq[AtomAuthor]
categories*: seq[AtomCategory]
contributors*: seq[AtomAuthor]
generator*: string
icon*: string
id*: string
link*: AtomLink
logo*: string
rights*: string
subtitle*: string
title*: string
updated*: string
proc parseAuthors ( node: XmlNode, mode="author") : seq[AtomAuthor] =
var authors:seq[AtomAuthor]
if node.child(mode) != nil:
for athr_node in node.findAll(mode):
var author: AtomAuthor = AtomAuthor()
author.name = athr_node.child("name").innerText
if athr_node.child("uri") != nil: author.uri = athr_node.child("uri").innerText
if athr_node.child("email") != nil: author.email = athr_node.child("email").innerText
authors.add(author)
if authors.len == 0: return @[]
return authors
proc parseCategories ( node: XmlNode ) : seq[AtomCategory] =
var categories:seq[AtomCategory]
if node.child("category") != nil:
for cat_node in node.findAll("category"):
var category: AtomCategory = AtomCategory()
if cat_node.attr("term") != "": category.term = cat_node.attr("term")
if cat_node.attr("label") != "": category.label = cat_node.attr("label")
if cat_node.attr("scheme") != "": category.scheme = cat_node.attr("scheme")
categories.add(category)
if categories.len == 0: return @[]
return categories
proc parseLink ( node: XmlNode ): AtomLink =
var link: AtomLink = AtomLink()
if node.attrs != nil:
if node.attr("href") != "": link.href = node.attr("rel")
if node.attr("rel") != "": link.rel = node.attr("rel")
if node.attr("type") != "": link.linktype = node.attr("type")
if node.attr("hreflang") != "": link.hreflang = node.attr("hreflang")
if node.attr("title") != "": link.title = node.attr("title")
if node.attr("length") != "": link.length = node.attr("length")
return link
proc parseEntry( node: XmlNode) : AtomEntry =
var entry: AtomEntry = AtomEntry()
# Fill the required fields
entry.id = node.child("id").innerText
entry.title = node.child("title").innerText
entry.updated = node.child("updated").innerText
# Fill the optinal fields
entry.authors = node.parseAuthors()
if node.child("category") != nil: entry.categories = node.parseCategories()
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("contributor") != nil:
entry.contributors = node.parseAuthors(mode="contributor")
if node.child("link") != nil: entry.link = node.child("link").parseLink()
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("source") != nil:
let source = node.child("source")
if source.child("author") != nil: entry.source.authors = source.parseAuthors()
if source.child("category") != nil: entry.source.categories = source.parseCategories()
if source.child("contributor") != nil: entry.source.contributors = source.parseAuthors(mode="contributor")
if source.child("generator") != nil: entry.source.generator = source.child("generator").innerText
if source.child("icon") != nil: entry.source.icon = source.child("icon").innerText
if source.child("id") != nil: entry.source.id = source.child("id").innerText
if source.child("link") != nil: entry.source.link = source.child("link").parseLink()
if source.child("logo") != nil: entry.source.logo = source.child("logo").innerText
if source.child("rights") != nil: entry.source.rights = source.child("rights").innerText
if source.child("subtitle") != nil: entry.source.subtitle = source.child("subtitle").innerText
if source.child("title") != nil: entry.source.title = source.child("title").innerText
if source.child("updated") != nil: entry.source.updated = source.child("updated").innerText
entry.source.author = entry.source.authors[0]
if node.child("summary") != nil: entry.summary = node.child("summary").innerText
# SUGAR an easy way to access an author
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()
# Fill in the required fields
atom.id = node.child("id").innerText
atom.title = node.child("title").innerText
atom.updated = node.child("updated").innerText
# Fill in the optional fields
if node.child("author") != nil: atom.authors = node.parseAuthors()
if node.child("category") != nil: atom.categories = node.parseCategories()
if node.child("contributor") != nil: atom.contributors = node.parseAuthors(mode="contributor")
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("link") != nil: atom.link = node.child("link").parseLink()
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("subtitle") != nil: atom.subtitle = node.child("subtitle").innerText
if atom.authors.len() > 0:
atom.author = atom.authors[0]
else:
atom.author = AtomAuthor()
# If there are no entries:
if node.child("entry") == nil:
atom.entries = @[]
return atom
# Otherwise, add the entries.
if node.child("entry") != nil:
atom.entries = map( node.findAll("entry"), parseEntry )
# Return the Atom data.
return atom
|