From 95890a2a611100a833ee727a4a39d5334b017ddf Mon Sep 17 00:00:00 2001 From: Nathan Yergler Date: Mon, 29 Jul 2013 08:10:28 -0400 Subject: [PATCH] Allow definition of pagination rules by page index. --- pelican/paginator.py | 20 ++++++++++++++++++-- pelican/settings.py | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/pelican/paginator.py b/pelican/paginator.py index e29a32fe..b5db2d7d 100644 --- a/pelican/paginator.py +++ b/pelican/paginator.py @@ -3,6 +3,7 @@ from __future__ import unicode_literals, print_function import six # From django.core.paginator +from collections import namedtuple import functools import logging import os @@ -11,6 +12,13 @@ from math import ceil logger = logging.getLogger(__name__) + +PaginationRule = namedtuple( + 'PaginationRule', + 'min_page URL SAVE_AS', +) + + class Paginator(object): def __init__(self, name, object_list, settings): self.name = name @@ -109,8 +117,16 @@ class Page(object): """Returns URL information as defined in settings. Similar to URLWrapper._from_settings, but specialized to deal with pagination logic.""" - setting = "%s_%s" % ('PAGINATION', key) - value = self.settings[setting] + + rule = None + + # find the last matching pagination rule + for p in self.settings['PAGINATION_PATTERNS']: + if p.min_page <= self.number: + rule = p + + value = getattr(rule, key) + if not isinstance(value, six.string_types): logger.warning('%s is set to %s' % (setting, value)) return value diff --git a/pelican/settings.py b/pelican/settings.py index b65a6df7..ff5f4ce4 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -73,8 +73,9 @@ DEFAULT_CONFIG = { 'TAG_SAVE_AS': os.path.join('tag', '{slug}.html'), 'AUTHOR_URL': 'author/{slug}.html', 'AUTHOR_SAVE_AS': os.path.join('author', '{slug}.html'), - 'PAGINATION_URL': '{name}{number}.html', - 'PAGINATION_SAVE_AS': '{name}{number}.html', + 'PAGINATION_PATTERNS': [ + (0, '{name}{number}.html', '{name}{number}.html'), + ], 'YEAR_ARCHIVE_SAVE_AS': False, 'MONTH_ARCHIVE_SAVE_AS': False, 'DAY_ARCHIVE_SAVE_AS': False, @@ -238,6 +239,16 @@ def configure_settings(settings): 'http://docs.getpelican.com/en/latest/settings.html#timezone ' 'for more information') + # fix up pagination rules + from pelican.paginator import PaginationRule + pagination_rules = [ + PaginationRule(*r) for r in settings['PAGINATION_PATTERNS'] + ] + settings['PAGINATION_PATTERNS'] = sorted( + pagination_rules, + key=lambda r: r[0], + ) + # Save people from accidentally setting a string rather than a list path_keys = ( 'ARTICLE_EXCLUDES',