diff --git a/docs/themes.rst b/docs/themes.rst index 8362ec70..b029e816 100644 --- a/docs/themes.rst +++ b/docs/themes.rst @@ -253,6 +253,29 @@ page_name TAG_URL where everything after `{slug}` is removed -- useful for pagination links =================== =================================================== +period_archives.html +-------------------- + +This template will be processed for each year of your posts if a path +for YEAR_ARCHIVE_SAVE_AS is defined, each month if MONTH_ARCHIVE_SAVE_AS +is defined and each day if DAY_ARCHIVE_SAVE_AS is defined. + +=================== =================================================== +Variable Description +=================== =================================================== +period A tuple of the form (`year`, `month`, `day`) that + indicates the current time period. `year` and `day` + are numbers while `month` is a string. This tuple + only contains `year` if the time period is a + given year. It contains both `year` and `month` + if the time period is over years and months and + so on. + +=================== =================================================== + +You can see an example of how to use `period` in the ``simple`` theme's +period_archives.html + Feeds ===== diff --git a/pelican/generators.py b/pelican/generators.py index 758747e0..8c5f446b 100644 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -7,6 +7,7 @@ import random import logging import shutil import fnmatch +import calendar from codecs import open from collections import defaultdict @@ -279,6 +280,18 @@ class ArticlesGenerator(Generator): except Exception: template = self.get_template('archives') + period_save_as = { + 'year': self.settings['YEAR_ARCHIVE_SAVE_AS'], + 'month': self.settings['MONTH_ARCHIVE_SAVE_AS'], + 'day': self.settings['DAY_ARCHIVE_SAVE_AS'], + } + + period_date_key = { + 'year': attrgetter('date.year'), + 'month': attrgetter('date.year', 'date.month'), + 'day': attrgetter('date.year', 'date.month', 'date.day') + } + def _generate_period_archives(dates, key, save_as_fmt): """Generate period archives from `dates`, grouped by `key` and written to `save_as`. @@ -291,21 +304,21 @@ class ArticlesGenerator(Generator): # period archive dates date = archive[0].date save_as = save_as_fmt.format(date=date) - write(save_as, template, self.context, + context = self.context.copy() + + if key == period_date_key['year']: + context["period"] = (_period,) + elif key == period_date_key['month']: + context["period"] = (_period[0], + calendar.month_name[_period[1]]) + else: + context["period"] = (_period[0], + calendar.month_name[_period[1]], + _period[2]) + + write(save_as, template, context, dates=archive, blog=True) - period_save_as = { - 'year': self.settings['YEAR_ARCHIVE_SAVE_AS'], - 'month': self.settings['MONTH_ARCHIVE_SAVE_AS'], - 'day': self.settings['DAY_ARCHIVE_SAVE_AS'], - } - - period_date_key = { - 'year': attrgetter('date.year'), - 'month': attrgetter('date.year', 'date.month'), - 'day': attrgetter('date.year', 'date.month', 'date.day') - } - for period in 'year', 'month', 'day': save_as = period_save_as[period] if save_as: diff --git a/pelican/tests/test_generators.py b/pelican/tests/test_generators.py index 150d30f5..eddaa504 100644 --- a/pelican/tests/test_generators.py +++ b/pelican/tests/test_generators.py @@ -197,6 +197,66 @@ class TestArticlesGenerator(unittest.TestCase): self.assertIn(custom_template, self.articles) self.assertIn(standard_template, self.articles) + def test_period_in_timeperiod_archive(self): + """ + Test that the context of a generated period_archive is passed + 'period' : a tuple of year, month, day according to the time period + """ + settings = get_settings(filenames={}) + + settings['YEAR_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/index.html' + generator = ArticlesGenerator( + context=settings, settings=settings, + path=CONTENT_DIR, theme=settings['THEME'], output_path=None) + generator.generate_context() + write = MagicMock() + generator.generate_period_archives(write) + dates = [d for d in generator.dates if d.date.year == 1970] + self.assertEqual(len(dates), 1) + #among other things it must have at least been called with this + settings["period"] = (1970,) + write.assert_called_with("posts/1970/index.html", + generator.get_template("period_archives"), + settings, + blog=True, dates=dates) + + del settings["period"] + settings['MONTH_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/index.html' + generator = ArticlesGenerator( + context=settings, settings=settings, + path=CONTENT_DIR, theme=settings['THEME'], output_path=None) + generator.generate_context() + write = MagicMock() + generator.generate_period_archives(write) + dates = [d for d in generator.dates if d.date.year == 1970 + and d.date.month == 1] + self.assertEqual(len(dates), 1) + settings["period"] = (1970, "January") + #among other things it must have at least been called with this + write.assert_called_with("posts/1970/Jan/index.html", + generator.get_template("period_archives"), + settings, + blog=True, dates=dates) + + del settings["period"] + settings['DAY_ARCHIVE_SAVE_AS'] = 'posts/{date:%Y}/{date:%b}/{date:%d}/index.html' + generator = ArticlesGenerator( + context=settings, settings=settings, + path=CONTENT_DIR, theme=settings['THEME'], output_path=None) + generator.generate_context() + write = MagicMock() + generator.generate_period_archives(write) + dates = [d for d in generator.dates if d.date.year == 1970 + and d.date.month == 1 + and d.date.day == 1] + self.assertEqual(len(dates), 1) + settings["period"] = (1970, "January", 1) + #among other things it must have at least been called with this + write.assert_called_with("posts/1970/Jan/01/index.html", + generator.get_template("period_archives"), + settings, + blog=True, dates=dates) + class TestPageGenerator(unittest.TestCase): # Note: Every time you want to test for a new field; Make sure the test diff --git a/pelican/themes/notmyidea/templates/period_archives.html b/pelican/themes/notmyidea/templates/period_archives.html new file mode 100644 index 00000000..252e002f --- /dev/null +++ b/pelican/themes/notmyidea/templates/period_archives.html @@ -0,0 +1,13 @@ +{% extends "base.html" %} +{% block content %} +
+

Archives for {{ period | reverse | join(' ') }}

+ +
+{% for article in dates %} +
{{ article.locale_date }}
+
{{ article.title }}
+{% endfor %} +
+
+{% endblock %} diff --git a/pelican/themes/simple/templates/period_archives.html b/pelican/themes/simple/templates/period_archives.html new file mode 100644 index 00000000..d930dbbb --- /dev/null +++ b/pelican/themes/simple/templates/period_archives.html @@ -0,0 +1,11 @@ +{% extends "base.html" %} +{% block content %} +

Archives for {{ period | reverse | join(' ') }}

+ +
+{% for article in dates %} +
{{ article.locale_date }}
+
{{ article.title }}
+{% endfor %} +
+{% endblock %}