diff options
| author | John Conway <john.a.conway@gmail.com> | 2019-05-09 18:19:33 +0100 |
|---|---|---|
| committer | John Conway <john.a.conway@gmail.com> | 2019-05-09 18:19:33 +0100 |
| commit | 8bf5047d24f904f90d8608b24b6e50a0e61df714 (patch) | |
| tree | 88a00fc5ada0d85700c1c5ecfbf2482415f970f1 | |
| parent | bbe3ff429077022a49e9a2c1aff9e2b7c1f0ed44 (diff) | |
| download | feed-nim-8bf5047d24f904f90d8608b24b6e50a0e61df714.tar.gz feed-nim-8bf5047d24f904f90d8608b24b6e50a0e61df714.zip | |
[Atom] Added string converter for text objects
| -rw-r--r-- | src/feednim/atom.nim | 62 | ||||
| -rwxr-xr-x | tests/test_atom | bin | 0 -> 492488 bytes | |||
| -rw-r--r-- | tests/test_atom.nim | 9 |
3 files changed, 46 insertions, 25 deletions
diff --git a/src/feednim/atom.nim b/src/feednim/atom.nim index 5c8b4a6..ebc171e 100644 --- a/src/feednim/atom.nim +++ b/src/feednim/atom.nim @@ -11,12 +11,15 @@ import xmltree import streams import sugar - type - Atom* = object - author*: AtomAuthor # Sugar, not in Atom spec. Returns the first author. + AtomCommon = ref object of RootObj + xmlbase*: string + xmllang*: string + + Atom* = ref object of AtomCommon + author*: AtomAuthor # Sugar, not in Atom spec. Refers to the first author. id*: string # Required Atom field - title*: string # Required Atom field + title*: AtomText # Required Atom field updated*: string # Required Atom field authors*: seq[AtomAuthor] # Pleuralised because the Atom spec allows more than one categories*: seq[AtomCategory] @@ -26,9 +29,13 @@ type link*: AtomLink logo*: string rights*: string - subtitle*: string + subtitle*: AtomText entries*: seq[AtomEntry] + AtomText* = ref object of AtomCommon + textType*: string + text*: string + AtomAuthor* = object name*: string # Required Atom field uri*: string @@ -39,6 +46,9 @@ type label*: string scheme*: string + AtomContent* = ref object of AtomText + src*: string + AtomLink* = object href*: string rel*: string @@ -47,16 +57,14 @@ type title*: string length*: string - AtomEntry* = object + AtomEntry* = ref object of AtomCommon id*: string # Required Atom field - title*: string # Required Atom field + title*: AtomText # 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 + content*: AtomContent contributors*: seq[AtomAuthor] link*: AtomLink published*: string @@ -64,7 +72,7 @@ type source*: AtomSource summary*: string - AtomSource* = object + AtomSource* = ref object of AtomCommon author*: AtomAuthor # Sugar, not in Atom spec. Returns the first author. authors*: seq[AtomAuthor] categories*: seq[AtomCategory] @@ -79,7 +87,10 @@ type title*: string updated*: string -proc parseAuthors ( node: XmlNode, mode="author") : seq[AtomAuthor] = +converter toString*(obj: AtomText): string = + return obj.text + +proc parseAuthors ( node: XmlNode, mode="author" ) : seq[AtomAuthor] = var authors:seq[AtomAuthor] if node.child(mode) != nil: for athr_node in node.findAll(mode): @@ -116,12 +127,13 @@ proc parseLink ( node: XmlNode ): AtomLink = if node.attr("length") != "": link.length = node.attr("length") return link -proc parseEntry( node: XmlNode) : AtomEntry = +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.title = AtomText() + entry.title.text = node.child("title").innerText entry.updated = node.child("updated").innerText # Fill the optinal fields @@ -131,19 +143,20 @@ proc parseEntry( node: XmlNode) : AtomEntry = if node.child("content") != nil: let content_node = node.child("content") - entry.content = content_node.innerText + entry.content = AtomContent() + entry.content.text = content_node.innerText if content_node.attrs != nil: if content_node.attr("type") == "xhtml" or content_node.attr("type") == "html": var content = "" - entry.contentType = node.attr("type") + entry.content.texttype = content_node.attr("type") for item in content_node.items: content = content & $item - entry.content = content + entry.content.text = content else: - entry.content = content_node.innerText + entry.content.text = content_node.innerText - entry.contentSrc = content_node.attr("src") + entry.content.src = content_node.attr("src") if node.child("contributor") != nil: entry.contributors = node.parseAuthors(mode="contributor") @@ -156,6 +169,7 @@ proc parseEntry( node: XmlNode) : AtomEntry = if node.child("source") != nil: let source = node.child("source") + entry.source = AtomSource() 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") @@ -181,7 +195,7 @@ proc parseEntry( node: XmlNode) : AtomEntry = return entry -proc parseAtom*(data: string): Atom = +proc parseAtom* ( data: string ): Atom = ## Parses the Atom from the given string. # Parse into XML. @@ -192,7 +206,9 @@ proc parseAtom*(data: string): Atom = # Fill in the required fields atom.id = node.child("id").innerText - atom.title = node.child("title").innerText + + atom.title = AtomText() + atom.title.text = node.child("title").innerText atom.updated = node.child("updated").innerText # Fill in the optional fields @@ -212,7 +228,9 @@ proc parseAtom*(data: string): Atom = if node.child("rights") != nil: atom.rights = node.child("rights").innerText - if node.child("subtitle") != nil: atom.subtitle = node.child("subtitle").innerText + if node.child("subtitle") != nil: + atom.subtitle = AtomText() + atom.subtitle.text = node.child("subtitle").innerText if atom.authors.len() > 0: atom.author = atom.authors[0] diff --git a/tests/test_atom b/tests/test_atom Binary files differnew file mode 100755 index 0000000..a24afac --- /dev/null +++ b/tests/test_atom diff --git a/tests/test_atom.nim b/tests/test_atom.nim index d2eac21..15140d6 100644 --- a/tests/test_atom.nim +++ b/tests/test_atom.nim @@ -10,6 +10,7 @@ import unittest import marshal import feednim +import ../src/feednim/atom test "Read Valid Atom Feed": let feed = "./tests/test_atom.xml".loadAtom() @@ -60,8 +61,10 @@ test "Read Valid Atom Feed": check feed.entries[0].categories[0].label == "Words" check feed.entries[0].categories[0].scheme == "http://awesomecategories.org" - #check feed.entries[0].content.contenttype == "xhtml" - check feed.entries[0].content == """<div xmlns="http://www.w3.org/1999/xhtml"><p><i>Aero</i>- not air-, fools!</p></div>""" + var feed_0_content_textType = feed.entries[0].content.textType + check feed_0_content_textType == "xhtml" + var feed_0_content_text: string = feed.entries[0].content + check feed_0_content_text == """<div xmlns="http://www.w3.org/1999/xhtml"><p><i>Aero</i>- not air-, fools!</p></div>""" check feed.entries[0].published == "2003-12-13T18:30:02Z" check feed.entries[0].rights == "Copyright Joe Bloggs" @@ -86,7 +89,7 @@ test "Read Valid Atom Feed": check feed.entries[1].categories[0].term == "trains" check feed.entries[1].categories[0].label == "Trains" - check feed.entries[1].contentSrc == "http://trains.com" + check feed.entries[1].content.src == "http://trains.com" check feed.entries[1].link.href == "http://joe.bloggs/trains-full" check feed.entries[1].link.rel == "alternate" check feed.entries[1].link.linktype == "text/html" |
