diff --git a/granary/jsonfeed.py b/granary/jsonfeed.py index 758637a7..edc8a702 100644 --- a/granary/jsonfeed.py +++ b/granary/jsonfeed.py @@ -2,7 +2,7 @@ JSON Feed spec: https://jsonfeed.org/version/1 """ -from __future__ import unicode_literals +from __future__ import absolute_import, unicode_literals from builtins import str from past.builtins import basestring @@ -11,12 +11,11 @@ import mf2util from oauth_dropins.webutil import util +from . import microformats2 + # allowed ActivityStreams objectTypes for attachments ATTACHMENT_TYPES = {'image', 'audio', 'video'} -# format string for inserting a JSON Feed entry's image into its content_html -HTML_IMAGE_TEMPLATE = '

' - def activities_to_jsonfeed(activities, actor=None, title=None, feed_url=None, home_page_url=None): @@ -55,18 +54,8 @@ def actor_name(obj): if obj.get('objectType') == 'person': continue author = obj.get('author', {}) - - content = obj.get('content') - # The JSON Feed spec (https://jsonfeed.org/version/1#items) says that the - # URL from the "image" property may also appear in "content_html", in which - # case it should be interpreted as the "main, featured image" of the - # post. It does not specify the behavior or semantics in the case that the - # image does *not* appear in "content_html", but currently at least one - # feed reader (Feedbin) will not display the image as part of the post - # content unless it is explicitly included in "content_html". - if content and image_url(obj): - content += HTML_IMAGE_TEMPLATE.format(image_url(obj)) - + content = microformats2.render_content( + obj, include_location=True, render_attachments=True) obj_title = obj.get('title') or obj.get('displayName') item = { 'id': obj.get('id') or obj.get('url'), @@ -152,26 +141,11 @@ def attachment(jf): as1['url'] = url return as1 - def content(item): - if item.get('content_html'): - content = item.get('content_html') - - # If the content_html ends with an tag that could have been generated - # by Granary (and which is redundant with the entry's "image" property), - # remove that tag. - if item.get('image'): - image_html = HTML_IMAGE_TEMPLATE.format(item.get('image')) - if content.endswith(image_html): - content = content[:-len(image_html)] - else: - content = item.get('content_text') - return content - activities = [{'object': { 'objectType': 'article' if item.get('title') else 'note', 'title': item.get('title'), 'summary': item.get('summary'), - 'content': content(item), + 'content': item.get('content_html') or item.get('content_text'), 'id': str(item.get('id') or ''), 'published': item.get('date_published'), 'updated': item.get('date_modified'), diff --git a/granary/test/test_jsonfeed.py b/granary/test/test_jsonfeed.py index 2763146b..13f423cb 100644 --- a/granary/test/test_jsonfeed.py +++ b/granary/test/test_jsonfeed.py @@ -57,7 +57,7 @@ def test_activities_to_jsonfeed_name_is_not_title(self): def test_activities_to_jsonfeed_image_attachment(self): self.assert_equals([{ - 'content_text': '', + 'content_html': '\n

\n\n

', 'attachments': [{ 'url': 'http://pict/ure.jpg', 'mime_type': 'image/jpeg', @@ -67,7 +67,9 @@ def test_activities_to_jsonfeed_image_attachment(self): }], {})['items']) def test_activities_to_jsonfeed_ignore_other_attachment_types(self): - self.assert_equals([{'content_text': ''}], activities_to_jsonfeed([{ + self.assert_equals([{ + 'content_html': '\n

\n\n\n

' + }], activities_to_jsonfeed([{ 'attachments': [{ 'url': 'http://quoted/tweet', 'objectType': 'note', diff --git a/granary/test/testdata/article_with_attachment_image_list.feed-from-as.json b/granary/test/testdata/article_with_attachment_image_list.feed-from-as.json index 83b35808..425fe196 100644 --- a/granary/test/testdata/article_with_attachment_image_list.feed-from-as.json +++ b/granary/test/testdata/article_with_attachment_image_list.feed-from-as.json @@ -8,7 +8,7 @@ "avatar": "http://example.com/martin/image" }, "items": [{ - "content_text": "", + "content_html": "\n

\n

", "attachments": [{ "mime_type": "image/gif", "url": "http://a/pic.gif" diff --git a/granary/test/testdata/article_with_attachment_stream_list.feed-from-as.json b/granary/test/testdata/article_with_attachment_stream_list.feed-from-as.json index 883b4625..a8eaf483 100644 --- a/granary/test/testdata/article_with_attachment_stream_list.feed-from-as.json +++ b/granary/test/testdata/article_with_attachment_stream_list.feed-from-as.json @@ -8,7 +8,7 @@ "avatar": "http://example.com/martin/image" }, "items": [{ - "content_text": "", + "content_html": "\n

\n

", "attachments": [{ "mime_type": "video/quicktime", "url": "http://a/vidjo.mov" diff --git a/granary/test/testdata/article_with_audio_video.feed.json b/granary/test/testdata/article_with_audio_video.feed-from-as.json similarity index 52% rename from granary/test/testdata/article_with_audio_video.feed.json rename to granary/test/testdata/article_with_audio_video.feed-from-as.json index 74104bef..5719d3c1 100644 --- a/granary/test/testdata/article_with_audio_video.feed.json +++ b/granary/test/testdata/article_with_audio_video.feed-from-as.json @@ -9,7 +9,7 @@ }, "items": [{ "title": "i'm ready to speak", - "content_html": "something", + "content_html": "something\n

\n

\n

\n

", "attachments": [{ "mime_type": "audio/mpeg", "url": "http://a/podcast.mp3" diff --git a/granary/test/testdata/note.feed.json b/granary/test/testdata/note.feed-from-as.json similarity index 67% rename from granary/test/testdata/note.feed.json rename to granary/test/testdata/note.feed-from-as.json index b0874168..5ef65d5a 100644 --- a/granary/test/testdata/note.feed.json +++ b/granary/test/testdata/note.feed-from-as.json @@ -12,7 +12,7 @@ "url": "http://example.com/blog-post-123", "image": "http://example.com/blog-post-123/image", "summary": "too cool to summarize", - "content_html": "A note. link too

", + "content_html": "A note. link too\n

\n \n Carcassonne, Aude\n \n \n \n

", "date_published": "2012-02-22T20:26:41", "date_modified": "2013-10-25T10:31:30+00:00", "author": {