Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render JSON Feed entries’ content with render_content #149

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 6 additions & 32 deletions granary/jsonfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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 = '<p><img src="{}"/></p>'


def activities_to_jsonfeed(activities, actor=None, title=None, feed_url=None,
home_page_url=None):
Expand Down Expand Up @@ -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'),
Expand Down Expand Up @@ -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 <img> 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'),
Expand Down
6 changes: 4 additions & 2 deletions granary/test/test_jsonfeed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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<p>\n<img class="u-photo" src="http://pict/ure.jpg" alt="" />\n</p>',
'attachments': [{
'url': 'http://pict/ure.jpg',
'mime_type': 'image/jpeg',
Expand All @@ -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<p>\n<a class="link" href="http://some/one">\n</a>\n</p>'
}], activities_to_jsonfeed([{
'attachments': [{
'url': 'http://quoted/tweet',
'objectType': 'note',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"avatar": "http://example.com/martin/image"
},
"items": [{
"content_text": "",
"content_html": "\n<p>\n</p>",
"attachments": [{
"mime_type": "image/gif",
"url": "http://a/pic.gif"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
"avatar": "http://example.com/martin/image"
},
"items": [{
"content_text": "",
"content_html": "\n<p>\n</p>",
"attachments": [{
"mime_type": "video/quicktime",
"url": "http://a/vidjo.mov"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
},
"items": [{
"title": "i'm ready to speak",
"content_html": "something",
"content_html": "something\n<p><audio class=\"u-audio\" src=\"http://a/podcast.mp3\" controls=\"controls\">Your browser does not support the audio tag. <a href=\"http://a/podcast.mp3\">Click here to listen directly.</a></audio>\n</p>\n<p><video class=\"u-video\" src=\"http://a/vidjo.mov\" controls=\"controls\" poster=\"\">Your browser does not support the video tag. <a href=\"http://a/vidjo.mov\">Click here to view directly. </a></video>\n</p>",
"attachments": [{
"mime_type": "audio/mpeg",
"url": "http://a/podcast.mp3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<p><img src=\"http://example.com/blog-post-123/image\"/></p>",
"content_html": "A note. <a href=\"http://my/link\">link</a> too\n<p> <span class=\"p-location h-card\">\n <data class=\"p-uid\" value=\"31cb9e7ed29dbe52\"></data>\n <a class=\"p-name u-url\" href=\"https://maps.google.com/maps?q=32.4004416,-98.9852672\">Carcassonne, Aude</a>\n \n \n </span>\n</p>",
"date_published": "2012-02-22T20:26:41",
"date_modified": "2013-10-25T10:31:30+00:00",
"author": {
Expand Down