From 7d69c37032349ae8f914d52139a021174c640b3d Mon Sep 17 00:00:00 2001 From: Bastian Venthur Date: Wed, 17 Mar 2021 10:39:26 +0100 Subject: [PATCH] Use description tag for Atom feed if provided --- blag/blag.py | 25 +++++++++++++++- tests/test_blag.py | 71 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/blag/blag.py b/blag/blag.py index 86825f8..a8a7bad 100644 --- a/blag/blag.py +++ b/blag/blag.py @@ -243,6 +243,25 @@ def generate_feed( blog_description, blog_author, ): + """Generate Atom feed. + + Parameters + ---------- + articles : list[list[str, dict]] + list of relative output path and article dictionary + output_dir : str + where the feed is stored + base_url : str + base url + blog_title : str + blog title + blog_description : str + blog description + blog_author : str + blog author + + """ + logger.info('Generating Atom feed.') feed = feedgenerator.Atom1Feed( link=base_url, title=blog_title, @@ -251,11 +270,15 @@ def generate_feed( ) for dst, context in articles: + # if article has a description, use that. otherwise fall back to + # the title + description = context.get('description', context['title']) + feed.add_item( title=context['title'], author_name=blog_author, link=base_url + dst, - description=context['title'], + description=description, content=context['content'], pubdate=context['date'], ) diff --git a/tests/test_blag.py b/tests/test_blag.py index fc2755f..d2ef954 100644 --- a/tests/test_blag.py +++ b/tests/test_blag.py @@ -1,5 +1,6 @@ from tempfile import TemporaryDirectory import os +from datetime import datetime import pytest @@ -18,6 +19,76 @@ def test_generate_feed(outdir): assert os.path.exists(f'{outdir}/atom.xml') +def test_feed(outdir): + articles = [ + [ + 'dest1.html', + { + 'title': 'title1', + 'date': datetime(2019, 6, 6), + 'content': 'content1', + } + ], + [ + 'dest2.html', + { + 'title': 'title2', + 'date': datetime(1980, 5, 9), + 'content': 'content2', + } + ], + + ] + + blag.generate_feed(articles, outdir, 'https://example.com/', 'blog title', + 'blog description', 'blog author') + with open(f'{outdir}/atom.xml') as fh: + feed = fh.read() + + assert 'blog title' in feed + # enable when https://github.com/getpelican/feedgenerator/issues/22 + # is fixed + # assert 'blog description' in feed + assert 'blog author' in feed + + # article 1 + assert 'title1' in feed + assert 'title1' in feed + assert '2019-06-06' in feed + assert 'content1' in feed + assert 'title2' in feed + assert 'title2' in feed + assert '1980-05-09' in feed + assert 'content2' in feed + assert 'title' in feed + assert 'description' in feed + assert '2019-06-06' in feed + assert 'content' in feed + + def test_parse_args_build(): # test default args args = blag.parse_args(['build'])