Skip to content

Commit

Permalink
fix(feeds): Feed validation, templates regression (#97)
Browse files Browse the repository at this point in the history
  • Loading branch information
rpatterson authored Mar 23, 2021
1 parent 5e894ab commit cf98bb0
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
15 changes: 8 additions & 7 deletions ablog/post.py
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ def generate_atom_feeds(app):
feed.title(feed_title)
feed.link(href=url)
feed.subtitle(blog.blog_feed_subtitle)
feed.link(href=feed_url)
feed.link(href=feed_url, rel="self")
feed.language(app.config.language)
feed.generator("ABlog", ablog.__version__, "https://ablog.readthedocs.org/")

Expand Down Expand Up @@ -741,17 +741,18 @@ def generate_atom_feeds(app):

# Entry values that support templates
title = post.title
if post.excerpt:
summary = " ".join(paragraph.astext() for paragraph in post.excerpt[0])
else:
summary = ""
summary = "".join(paragraph.astext() for paragraph in post.excerpt)
template_values = {}
for element in ("title", "summary", "content"):
if element in feed_templates:
template_values[element] = jinja2.Template(feed_templates[element]).render(**locals())
feed_entry.title(template_values.get("title", title))
feed_entry.summary(template_values.get("summary", summary))
feed_entry.content(content=template_values.get("content", content), type="html")
summary = template_values.get("summary", summary)
if summary:
feed_entry.summary(summary)
content = template_values.get("content", content)
if content:
feed_entry.content(content=content, type="html")

parent_dir = os.path.dirname(feed_path)
if not os.path.isdir(parent_dir):
Expand Down
5 changes: 5 additions & 0 deletions tests/roots/test-build/foo-empty-post.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. post:: 2021-03-23

##############
Foo Empty Post
##############
4 changes: 3 additions & 1 deletion tests/roots/test-build/post.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
Foo Post Title
==============

Foo post description.
Foo post description `with link`_.

Foo post content.

.. _`with link`: https://example.com
16 changes: 13 additions & 3 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ def test_feed(app, status, warning):
with feed_path.open() as feed_opened:
feed_tree = lxml.etree.parse(feed_opened)
entries = feed_tree.findall("{http://www.w3.org/2005/Atom}entry")
assert len(entries) == 1, "Wrong number of Atom feed entries"
assert len(entries) == 2, "Wrong number of Atom feed entries"

entry = entries[0]
title = entry.find("{http://www.w3.org/2005/Atom}title")
assert title.text == "Foo Post Title", "Wrong Atom feed entry title"
summary = entry.find("{http://www.w3.org/2005/Atom}summary")
assert summary.text == "Foo post description.", "Wrong Atom feed entry summary"
assert summary.text == "Foo post description with link.", "Wrong Atom feed entry summary"
categories = entry.findall("{http://www.w3.org/2005/Atom}category")
assert len(categories) == 2, "Wrong number of Atom feed categories"
assert categories[0].attrib["label"] == "Foo Tag", "Wrong Atom feed first category"
Expand All @@ -42,6 +42,16 @@ def test_feed(app, status, warning):
content = entry.find("{http://www.w3.org/2005/Atom}content")
assert "Foo post content." in content.text, "Wrong Atom feed entry content"

empty_entry = entries[1]
title = empty_entry.find("{http://www.w3.org/2005/Atom}title")
assert title.text == "Foo Empty Post", "Wrong Atom feed empty entry title"
summary = empty_entry.find("{http://www.w3.org/2005/Atom}summary")
assert summary is None, "Atom feed empty entry contains optional summary element"
categories = empty_entry.findall("{http://www.w3.org/2005/Atom}category")
assert len(categories) == 0, "Atom categories rendered for empty post"
content = empty_entry.find("{http://www.w3.org/2005/Atom}content")
assert 'id="foo-empty-post"' in content.text, "Atom feed empty entry missing post ID"

social_path = app.outdir / "blog/social.xml"
assert (social_path).exists(), "Social media feed was not built"

Expand All @@ -54,7 +64,7 @@ def test_feed(app, status, warning):
title = social_entry.find("{http://www.w3.org/2005/Atom}title")
assert title.text == "Foo Post Title", "Wrong Social media feed entry title"
summary = social_entry.find("{http://www.w3.org/2005/Atom}summary")
assert summary.text == "Foo post description.", "Wrong Social media feed entry summary"
assert summary.text == "Foo post description with link.", "Wrong Social media feed entry summary"
categories = social_entry.findall("{http://www.w3.org/2005/Atom}category")
assert len(categories) == 2, "Wrong number of Social media feed categories"
assert categories[0].attrib["label"] == "Foo Tag", "Wrong Social media feed first category"
Expand Down

0 comments on commit cf98bb0

Please sign in to comment.