From cf98bb0f7bc678e1fe66edc665b99bd6e47b1c84 Mon Sep 17 00:00:00 2001 From: Ross Patterson Date: Tue, 23 Mar 2021 15:17:43 -0700 Subject: [PATCH] fix(feeds): Feed validation, templates regression (#97) --- ablog/post.py | 15 ++++++++------- tests/roots/test-build/foo-empty-post.rst | 5 +++++ tests/roots/test-build/post.rst | 4 +++- tests/test_build.py | 16 +++++++++++++--- 4 files changed, 29 insertions(+), 11 deletions(-) create mode 100644 tests/roots/test-build/foo-empty-post.rst diff --git a/ablog/post.py b/ablog/post.py index b6be6c88..e08daccc 100644 --- a/ablog/post.py +++ b/ablog/post.py @@ -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/") @@ -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): diff --git a/tests/roots/test-build/foo-empty-post.rst b/tests/roots/test-build/foo-empty-post.rst new file mode 100644 index 00000000..e2221dfe --- /dev/null +++ b/tests/roots/test-build/foo-empty-post.rst @@ -0,0 +1,5 @@ +.. post:: 2021-03-23 + +############## +Foo Empty Post +############## diff --git a/tests/roots/test-build/post.rst b/tests/roots/test-build/post.rst index d8f1d1e1..bef264d8 100644 --- a/tests/roots/test-build/post.rst +++ b/tests/roots/test-build/post.rst @@ -4,6 +4,8 @@ Foo Post Title ============== - Foo post description. + Foo post description `with link`_. Foo post content. + +.. _`with link`: https://example.com diff --git a/tests/test_build.py b/tests/test_build.py index ff4211b2..10c077b9 100644 --- a/tests/test_build.py +++ b/tests/test_build.py @@ -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" @@ -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" @@ -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"