From f79e3a64b337ffb8d301e3b328252d36884be59c Mon Sep 17 00:00:00 2001 From: Denis Feoktistov Date: Sat, 5 May 2018 15:39:12 +1200 Subject: [PATCH] Added support for RSS content module `http://purl.org/rss/1.0/modules/content/` --- lib/domain/rss_content.dart | 35 +++++++++++++++++++++++++++++++++++ lib/domain/rss_item.dart | 36 ++++++++++++++++++++++++++++++++---- test/rss_test.dart | 3 +++ test/xml/RSS.xml | 5 ++++- 4 files changed, 74 insertions(+), 5 deletions(-) create mode 100644 lib/domain/rss_content.dart diff --git a/lib/domain/rss_content.dart b/lib/domain/rss_content.dart new file mode 100644 index 0000000..378f045 --- /dev/null +++ b/lib/domain/rss_content.dart @@ -0,0 +1,35 @@ +import 'package:xml/xml/nodes/element.dart'; + +final _imagesRegExp = new RegExp( + "]+)(?:'|\")", + multiLine: true, + caseSensitive: false, +); + +/// For RSS Content Module: +/// +/// - `xmlns:content="http://purl.org/rss/1.0/modules/content/"` +/// +class RssContent { + String value; + Iterable images; + + RssContent(this.value, this.images); + + factory RssContent.parse(XmlElement node) { + final content = node.text; + final images = []; + _imagesRegExp.allMatches(content).forEach((match) { + images.add(match.group(1)); + }); + return new RssContent(content, images); + } + + @override + String toString() { + return ''' + content: $value + images: $images + '''; + } +} diff --git a/lib/domain/rss_item.dart b/lib/domain/rss_item.dart index 94abf5a..889f9b2 100644 --- a/lib/domain/rss_item.dart +++ b/lib/domain/rss_item.dart @@ -1,4 +1,5 @@ import 'package:webfeed/domain/rss_category.dart'; +import 'package:webfeed/domain/rss_content.dart'; import 'package:webfeed/domain/rss_source.dart'; import 'package:webfeed/util/helpers.dart'; import 'package:xml/xml.dart'; @@ -14,9 +15,20 @@ class RssItem { final String author; final String comments; final RssSource source; + final RssContent content; - RssItem(this.title, this.description, this.link, - {this.categories, this.guid, this.pubDate, this.author, this.comments, this.source}); + RssItem( + this.title, + this.description, + this.link, { + this.categories, + this.guid, + this.pubDate, + this.author, + this.comments, + this.source, + this.content, + }); factory RssItem.parse(XmlElement element) { var title = xmlGetString(element, "title"); @@ -37,8 +49,23 @@ class RssItem { source = new RssSource.parse(element.findElements("source").first); } on StateError {} - return new RssItem(title, description, link, - categories: categories, guid: guid, pubDate: pubDate, author: author, comments: comments, source: source); + RssContent content; + try { + content = new RssContent.parse(element.findElements("content:encoded").first); + } on StateError {} + + return new RssItem( + title, + description, + link, + categories: categories, + guid: guid, + pubDate: pubDate, + author: author, + comments: comments, + source: source, + content: content, + ); } @override @@ -52,6 +79,7 @@ class RssItem { author: $author comments: $comments source: $source + content: $content '''; } } diff --git a/test/rss_test.dart b/test/rss_test.dart index 3a41da5..0952287 100644 --- a/test/rss_test.dart +++ b/test/rss_test.dart @@ -72,5 +72,8 @@ void main() { expect(feed.items.first.source.url, "https://foo.bar.news/1?source"); expect(feed.items.first.source.value, "Foo Bar"); expect(feed.items.first.comments, "https://foo.bar.news/1/comments"); + + expect(feed.items.first.content.value, " Test content
"); + expect(feed.items.first.content.images.first, "https://test.com/image_link"); }); } diff --git a/test/xml/RSS.xml b/test/xml/RSS.xml index 4a999de..a0a1060 100644 --- a/test/xml/RSS.xml +++ b/test/xml/RSS.xml @@ -1,5 +1,7 @@ - + + News - Foo bar News https://foo.bar.news/ @@ -44,6 +46,7 @@ alice@foo.bar.news Foo Bar https://foo.bar.news/1/comments + Test content
]]>
Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC