From 2008088a07dc08f5d58f2287ae70610da0f6c1ad Mon Sep 17 00:00:00 2001 From: Valentin Lorentz Date: Tue, 17 Oct 2023 19:57:29 +0200 Subject: [PATCH] RSS: Copy $summary to $description on Atom feeds Otherwise $description would remain feedparser's default, which is unescaped ; but $description is the only usable one on RSS feeds. --- plugins/RSS/plugin.py | 13 ++++++++ plugins/RSS/test.py | 72 +++++++++++++++++++++++++++++++------------ 2 files changed, 65 insertions(+), 20 deletions(-) diff --git a/plugins/RSS/plugin.py b/plugins/RSS/plugin.py index 9685241ee..aed17a2a0 100644 --- a/plugins/RSS/plugin.py +++ b/plugins/RSS/plugin.py @@ -526,6 +526,19 @@ class RSS(callbacks.Plugin): if isinstance(item, dict) and 'value' in item: value = item['value'] kwargs[key] = value + + for key in ('summary', 'title'): + detail = kwargs.get('%s_detail' % key) + if isinstance(detail, dict) and detail.get('type') in \ + ('text/html', 'application/xhtml+xml'): + kwargs[key] = utils.web.htmlToText(detail['value']) + + if 'description' not in kwargs and kwargs[key]: + kwargs['description'] = kwargs[key] + + if 'description' not in kwargs and kwargs.get('content'): + kwargs['description'] = kwargs['content'] + s = string.Template(template).safe_substitute(entry, **kwargs, date=date) return self._normalize_entry(s) diff --git a/plugins/RSS/test.py b/plugins/RSS/test.py index d5cdc9f6b..71c9d7dc5 100644 --- a/plugins/RSS/test.py +++ b/plugins/RSS/test.py @@ -359,83 +359,91 @@ class RSSTestCase(ChannelPluginTestCase): 'On the other hand, the refractor\'s') @mock_urllib - def testContentHtmlOnly(self, mock): + def testAtomContentHtmlOnly(self, mock): timeFastForward(1.1) - with conf.supybot.plugins.RSS.format.context('$content'): - mock._data = """ + mock._data = """ Recent Commits to anope:2.0 2023-10-04T16:14:39Z - title with <pre>HTML<pre> + title with <pre>HTML</pre> 2023-10-04T16:14:39Z - content with <pre>HTML<pre> + content with <pre>HTML</pre> """ + with conf.supybot.plugins.RSS.format.context('$content'): + self.assertRegexp('rss https://example.org', + 'content with HTML') + with conf.supybot.plugins.RSS.format.context('$description'): self.assertRegexp('rss https://example.org', 'content with HTML') @mock_urllib - def testContentXhtmlOnly(self, mock): + def testAtomContentXhtmlOnly(self, mock): timeFastForward(1.1) - with conf.supybot.plugins.RSS.format.context('$content'): - mock._data = """ + mock._data = """ Recent Commits to anope:2.0 2023-10-04T16:14:39Z - title with <pre>HTML<pre> + title with <pre>HTML</pre> 2023-10-04T16:14:39Z
- content with
XHTML
+        content with 
XHTML
""" + with conf.supybot.plugins.RSS.format.context('$content'): + self.assertRegexp('rss https://example.org', + 'content with XHTML') + with conf.supybot.plugins.RSS.format.context('$description'): self.assertRegexp('rss https://example.org', 'content with XHTML') @mock_urllib - def testContentHtmlAndPlaintext(self, mock): + def testAtomContentHtmlAndPlaintext(self, mock): timeFastForward(1.1) - with conf.supybot.plugins.RSS.format.context('$content'): - mock._data = """ + mock._data = """ Recent Commits to anope:2.0 2023-10-04T16:14:39Z - title with <pre>HTML<pre> + title with <pre>HTML</pre> 2023-10-04T16:14:39Z - content with <pre>HTML<pre> + content with <pre>HTML</pre> content with plaintext """ + with conf.supybot.plugins.RSS.format.context('$content'): + self.assertRegexp('rss https://example.org', + 'content with plaintext') + with conf.supybot.plugins.RSS.format.context('$description'): self.assertRegexp('rss https://example.org', 'content with plaintext') @mock_urllib - def testContentPlaintextAndHtml(self, mock): + def testAtomContentPlaintextAndHtml(self, mock): timeFastForward(1.1) - with conf.supybot.plugins.RSS.format.context('$content'): - mock._data = """ + mock._data = """ Recent Commits to anope:2.0 2023-10-04T16:14:39Z - title with <pre>HTML<pre> + title with <pre>HTML</pre> 2023-10-04T16:14:39Z @@ -443,12 +451,36 @@ class RSSTestCase(ChannelPluginTestCase): content with plaintext
- content with <pre>HTML<pre> + content with <pre>HTML</pre> """ + with conf.supybot.plugins.RSS.format.context('$content'): self.assertRegexp('rss https://example.org', 'content with plaintext') + with conf.supybot.plugins.RSS.format.context('$description'): + self.assertRegexp('rss https://example.org', + 'content with plaintext') + + @mock_urllib + def testRssDescriptionHtml(self, mock): + timeFastForward(1.1) + mock._data = """ + + + + feed title + + en + + title with <pre>HTML</pre> + description with <pre>HTML</pre> + + +""" + with conf.supybot.plugins.RSS.format.context('$description'): + self.assertRegexp('rss https://example.org', + 'description with HTML') @mock_urllib def testFeedAttribute(self, mock):