Allow to use page URL in pagination pattern.

Currently it was only possible to use page "save as" name or part of it
for generating pagination links. That's not sufficient when page URLs
differ a lot from actual filenames. With this patch it's possible to use
the `{url}` placeholder in PAGINATION_PATTERNS setting. For example, the
paginated archives would be saved to:

    blog/index.html
    blog/2/index.html
    blog/3/index.html

while the actual URLs would be like this (with the help of Apache's
mod_rewrite):

    http://blog.my.site/
    http://blog.my.site/2/
    http://blog.my.site/3/

The configuration that corresponds to this is roughly the following:

    ARCHIVES_SAVE_AS = 'blog/index.html'
    ARCHIVES_URL = 'http://blog.my.site/'
    PAGINATION_PATTERNS = [
        (1, '/{url}', '{base_name}/index.html'),
        (2, '/{url}{number}/', '{base_name}/{number}/index.html')
    ]

Also added YEAR_ARCHIVE_URL, MONTH_ARCHIVE_URL and DAY_ARCHIVE_URL
settings, as they were missing and now they make sense.
This commit is contained in:
Vladimír Vondruš 2017-10-26 18:23:17 +02:00
commit 182fb11c80
6 changed files with 74 additions and 25 deletions

View file

@ -17,8 +17,9 @@ PaginationRule = namedtuple(
class Paginator(object):
def __init__(self, name, object_list, settings):
def __init__(self, name, url, object_list, settings):
self.name = name
self.url = url
self.object_list = object_list
self.settings = settings
@ -37,8 +38,8 @@ class Paginator(object):
top = bottom + self.per_page
if top + self.orphans >= self.count:
top = self.count
return Page(self.name, self.object_list[bottom:top], number, self,
self.settings)
return Page(self.name, self.url, self.object_list[bottom:top], number,
self, self.settings)
def _get_count(self):
"Returns the total number of objects, across all pages."
@ -65,8 +66,9 @@ class Paginator(object):
class Page(object):
def __init__(self, name, object_list, number, paginator, settings):
def __init__(self, name, url, object_list, number, paginator, settings):
self.name, self.extension = os.path.splitext(name)
self.base_url = url
self.object_list = object_list
self.number = number
self.paginator = paginator
@ -134,6 +136,7 @@ class Page(object):
# URL or SAVE_AS is a string, format it with a controlled context
context = {
'name': self.name.replace(os.sep, '/'),
'url': self.base_url,
'object_list': self.object_list,
'number': self.number,
'paginator': self.paginator,