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 '