Skip to content

Commit

Permalink
atom: shorten and ellipsize feed title when necessary
Browse files Browse the repository at this point in the history
fixes #144
  • Loading branch information
snarfed committed Apr 25, 2018
1 parent fd102d8 commit af259e5
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 3 deletions.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -313,6 +313,8 @@ Changelog
* GitHub:
* Escape HTML characters (`<`, `>`, and `&`) in content in `create()` and `preview_create()` ([snarfed/bridgy#810](https://github.com/snarfed/bridgy/issues/810)).
* `get_activities()` and `get_comment()` now return `ValueError` instead of `AssertionError` on malformed `activity_id` and `comment_id` args, respectively.
* Atom:
* Shorten and ellipsize feed title when necessary ([#144](https://github.com/snarfed/granary/issues/144)).

### 1.12 - 2018-03-24
* Add Python 3 support! Granary now requires either Python 2.7+ or Python 3.3+.
Expand Down
2 changes: 1 addition & 1 deletion app.py
Expand Up @@ -157,7 +157,7 @@ def fetch_mf2_func(url):
return mf2py.parse(doc=doc, url=url)

actor = microformats2.find_author(mf2, fetch_mf2_func=fetch_mf2_func)
title = mf2util.interpret_feed(mf2, url).get('name')
title = microformats2.get_title(mf2)

if input in ('as1', 'activitystreams'):
activities = body_items
Expand Down
2 changes: 1 addition & 1 deletion granary/atom.py
Expand Up @@ -332,7 +332,7 @@ def html_to_atom(html, url=None, fetch_author=False, reader=True):
return activities_to_atom(
microformats2.html_to_activities(html, url, actor),
actor,
title=mf2util.interpret_feed(parsed, url).get('name'),
title=microformats2.get_title(parsed),
xml_base=util.base_url(url),
host_url=url,
reader=reader)
Expand Down
15 changes: 15 additions & 0 deletions granary/microformats2.py
Expand Up @@ -913,6 +913,21 @@ def find_author(parsed, **kwargs):
}


def get_title(mf2):
"""Returns the author of a page as a ActivityStreams actor dict.
Args:
mf2: dict, parsed mf2 object (ie return value from mf2py.parse())
Returns: string title, possibly ellipsized
"""
lines = mf2util.interpret_feed(mf2, '').get('name', '').splitlines()
if lines:
return util.ellipsize(lines[0])

return ''


def first_props(props):
"""Converts a multiply-valued dict to singly valued.
Expand Down
15 changes: 15 additions & 0 deletions granary/test/test_atom.py
Expand Up @@ -632,6 +632,21 @@ def test_html_to_atom_fetch_author(self):
</author>
""", got, ignore_blanks=True)

def test_html_to_atom_title_without_hfeed_name(self):
self.assert_multiline_in("""\
<generator uri="https://github.com/snarfed/granary">granary</generator>
<id>https://my.site/feed</id>
<title>unmarked feed title</title>
""", atom.html_to_atom("""\
<div class="h-feed">
<span>unmarked feed title</span>
<article class="h-entry">
<p class="e-content">entry content</p>
</article>
</div>
""", 'https://my.site/feed'),
ignore_blanks=True)

def test_media_tags_and_enclosures(self):
got = atom.activities_to_atom([{
'object': {
Expand Down
2 changes: 1 addition & 1 deletion test_app.py
Expand Up @@ -304,7 +304,7 @@ def test_url_html_to_atom(self):
self.expect_requests_get('http://my/posts.html', HTML % {
'body_class': ' class="h-feed"',
'extra': """
<span class="p-name">my title</span>
<span>my title</span>
<div class="p-author h-card">
<a href="http://my/site">My Name</a>
<img src="http://my/picture" />
Expand Down

0 comments on commit af259e5

Please sign in to comment.