Skip to content

Commit f79e3a6

Browse files
committed
Added support for RSS content module http://purl.org/rss/1.0/modules/content/`
1 parent 546454b commit f79e3a6

File tree

4 files changed

+74
-5
lines changed

4 files changed

+74
-5
lines changed

lib/domain/rss_content.dart

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import 'package:xml/xml/nodes/element.dart';
2+
3+
final _imagesRegExp = new RegExp(
4+
"<img\\s.*?src=(?:'|\")([^'\">]+)(?:'|\")",
5+
multiLine: true,
6+
caseSensitive: false,
7+
);
8+
9+
/// For RSS Content Module:
10+
///
11+
/// - `xmlns:content="http://purl.org/rss/1.0/modules/content/"`
12+
///
13+
class RssContent {
14+
String value;
15+
Iterable<String> images;
16+
17+
RssContent(this.value, this.images);
18+
19+
factory RssContent.parse(XmlElement node) {
20+
final content = node.text;
21+
final images = <String>[];
22+
_imagesRegExp.allMatches(content).forEach((match) {
23+
images.add(match.group(1));
24+
});
25+
return new RssContent(content, images);
26+
}
27+
28+
@override
29+
String toString() {
30+
return '''
31+
content: $value
32+
images: $images
33+
''';
34+
}
35+
}

lib/domain/rss_item.dart

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:webfeed/domain/rss_category.dart';
2+
import 'package:webfeed/domain/rss_content.dart';
23
import 'package:webfeed/domain/rss_source.dart';
34
import 'package:webfeed/util/helpers.dart';
45
import 'package:xml/xml.dart';
@@ -14,9 +15,20 @@ class RssItem {
1415
final String author;
1516
final String comments;
1617
final RssSource source;
18+
final RssContent content;
1719

18-
RssItem(this.title, this.description, this.link,
19-
{this.categories, this.guid, this.pubDate, this.author, this.comments, this.source});
20+
RssItem(
21+
this.title,
22+
this.description,
23+
this.link, {
24+
this.categories,
25+
this.guid,
26+
this.pubDate,
27+
this.author,
28+
this.comments,
29+
this.source,
30+
this.content,
31+
});
2032

2133
factory RssItem.parse(XmlElement element) {
2234
var title = xmlGetString(element, "title");
@@ -37,8 +49,23 @@ class RssItem {
3749
source = new RssSource.parse(element.findElements("source").first);
3850
} on StateError {}
3951

40-
return new RssItem(title, description, link,
41-
categories: categories, guid: guid, pubDate: pubDate, author: author, comments: comments, source: source);
52+
RssContent content;
53+
try {
54+
content = new RssContent.parse(element.findElements("content:encoded").first);
55+
} on StateError {}
56+
57+
return new RssItem(
58+
title,
59+
description,
60+
link,
61+
categories: categories,
62+
guid: guid,
63+
pubDate: pubDate,
64+
author: author,
65+
comments: comments,
66+
source: source,
67+
content: content,
68+
);
4269
}
4370

4471
@override
@@ -52,6 +79,7 @@ class RssItem {
5279
author: $author
5380
comments: $comments
5481
source: $source
82+
content: $content
5583
''';
5684
}
5785
}

test/rss_test.dart

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,8 @@ void main() {
7272
expect(feed.items.first.source.url, "https://foo.bar.news/1?source");
7373
expect(feed.items.first.source.value, "Foo Bar");
7474
expect(feed.items.first.comments, "https://foo.bar.news/1/comments");
75+
76+
expect(feed.items.first.content.value, "<img width=\"1000\" height=\"690\" src=\"https://test.com/image_link\"/> Test content<br />");
77+
expect(feed.items.first.content.images.first, "https://test.com/image_link");
7578
});
7679
}

test/xml/RSS.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<rss version="2.0">
2+
<rss version="2.0"
3+
xmlns:content="http://purl.org/rss/1.0/modules/content/">
4+
35
<channel>
46
<title>News - Foo bar News</title>
57
<link>https://foo.bar.news/</link>
@@ -44,6 +46,7 @@
4446
<author>[email protected]</author>
4547
<source url="https://foo.bar.news/1?source">Foo Bar</source>
4648
<comments>https://foo.bar.news/1/comments</comments>
49+
<content:encoded><![CDATA[<img width="1000" height="690" src="https://test.com/image_link"/> Test content<br />]]></content:encoded>
4750
</item>
4851
<item>
4952
<title>Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</title>

0 commit comments

Comments
 (0)