Parcourir la source

Added support for RSS content module `http://purl.org/rss/1.0/modules/content/`

master
Denis Feoktistov il y a 8 ans
Parent
révision
f79e3a64b3
4 fichiers modifiés avec 74 ajouts et 5 suppressions
  1. +35
    -0
      lib/domain/rss_content.dart
  2. +32
    -4
      lib/domain/rss_item.dart
  3. +3
    -0
      test/rss_test.dart
  4. +4
    -1
      test/xml/RSS.xml

+ 35
- 0
lib/domain/rss_content.dart Voir le fichier

@@ -0,0 +1,35 @@
import 'package:xml/xml/nodes/element.dart';

final _imagesRegExp = new RegExp(
"<img\\s.*?src=(?:'|\")([^'\">]+)(?:'|\")",
multiLine: true,
caseSensitive: false,
);

/// For RSS Content Module:
///
/// - `xmlns:content="http://purl.org/rss/1.0/modules/content/"`
///
class RssContent {
String value;
Iterable<String> images;

RssContent(this.value, this.images);

factory RssContent.parse(XmlElement node) {
final content = node.text;
final images = <String>[];
_imagesRegExp.allMatches(content).forEach((match) {
images.add(match.group(1));
});
return new RssContent(content, images);
}

@override
String toString() {
return '''
content: $value
images: $images
''';
}
}

+ 32
- 4
lib/domain/rss_item.dart Voir le fichier

@@ -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
''';
}
}

+ 3
- 0
test/rss_test.dart Voir le fichier

@@ -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, "<img width=\"1000\" height=\"690\" src=\"https://test.com/image_link\"/> Test content<br />");
expect(feed.items.first.content.images.first, "https://test.com/image_link");
});
}

+ 4
- 1
test/xml/RSS.xml Voir le fichier

@@ -1,5 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/">

<channel>
<title>News - Foo bar News</title>
<link>https://foo.bar.news/</link>
@@ -44,6 +46,7 @@
<author>alice@foo.bar.news</author>
<source url="https://foo.bar.news/1?source">Foo Bar</source>
<comments>https://foo.bar.news/1/comments</comments>
<content:encoded><![CDATA[<img width="1000" height="690" src="https://test.com/image_link"/> Test content<br />]]></content:encoded>
</item>
<item>
<title>Section 1.10.32 of "de Finibus Bonorum et Malorum", written by Cicero in 45 BC</title>


Chargement…
Annuler
Enregistrer