mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Merge pull request #1246 from fenekku/period-to-archive-context
Add period to period_archives context. Refs #1044.
This commit is contained in:
commit
9b36437d97
5 changed files with 133 additions and 13 deletions
|
|
@ -253,6 +253,29 @@ page_name TAG_URL where everything after `{slug}` is removed
|
||||||
-- useful for pagination links
|
-- 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
|
Feeds
|
||||||
=====
|
=====
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,7 @@ import random
|
||||||
import logging
|
import logging
|
||||||
import shutil
|
import shutil
|
||||||
import fnmatch
|
import fnmatch
|
||||||
|
import calendar
|
||||||
|
|
||||||
from codecs import open
|
from codecs import open
|
||||||
from collections import defaultdict
|
from collections import defaultdict
|
||||||
|
|
@ -279,6 +280,18 @@ class ArticlesGenerator(Generator):
|
||||||
except Exception:
|
except Exception:
|
||||||
template = self.get_template('archives')
|
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):
|
def _generate_period_archives(dates, key, save_as_fmt):
|
||||||
"""Generate period archives from `dates`, grouped by
|
"""Generate period archives from `dates`, grouped by
|
||||||
`key` and written to `save_as`.
|
`key` and written to `save_as`.
|
||||||
|
|
@ -291,21 +304,21 @@ class ArticlesGenerator(Generator):
|
||||||
# period archive dates
|
# period archive dates
|
||||||
date = archive[0].date
|
date = archive[0].date
|
||||||
save_as = save_as_fmt.format(date=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)
|
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':
|
for period in 'year', 'month', 'day':
|
||||||
save_as = period_save_as[period]
|
save_as = period_save_as[period]
|
||||||
if save_as:
|
if save_as:
|
||||||
|
|
|
||||||
|
|
@ -197,6 +197,66 @@ class TestArticlesGenerator(unittest.TestCase):
|
||||||
self.assertIn(custom_template, self.articles)
|
self.assertIn(custom_template, self.articles)
|
||||||
self.assertIn(standard_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):
|
class TestPageGenerator(unittest.TestCase):
|
||||||
# Note: Every time you want to test for a new field; Make sure the test
|
# Note: Every time you want to test for a new field; Make sure the test
|
||||||
|
|
|
||||||
13
pelican/themes/notmyidea/templates/period_archives.html
Normal file
13
pelican/themes/notmyidea/templates/period_archives.html
Normal file
|
|
@ -0,0 +1,13 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
{% block content %}
|
||||||
|
<section id="content" class="body">
|
||||||
|
<h1>Archives for {{ period | reverse | join(' ') }}</h1>
|
||||||
|
|
||||||
|
<dl>
|
||||||
|
{% for article in dates %}
|
||||||
|
<dt>{{ article.locale_date }}</dt>
|
||||||
|
<dd><a href="{{ SITEURL }}/{{ article.url }}">{{ article.title }}</a></dd>
|
||||||
|
{% endfor %}
|
||||||
|
</dl>
|
||||||
|
</section>
|
||||||
|
{% endblock %}
|
||||||
11
pelican/themes/simple/templates/period_archives.html
Normal file
11
pelican/themes/simple/templates/period_archives.html
Normal file
|
|
@ -0,0 +1,11 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
{% block content %}
|
||||||
|
<h1>Archives for {{ period | reverse | join(' ') }}</h1>
|
||||||
|
|
||||||
|
<dl>
|
||||||
|
{% for article in dates %}
|
||||||
|
<dt>{{ article.locale_date }}</dt>
|
||||||
|
<dd><a href="{{ SITEURL }}/{{ article.url }}">{{ article.title }}</a></dd>
|
||||||
|
{% endfor %}
|
||||||
|
</dl>
|
||||||
|
{% endblock %}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue