Merge pull request #2375 from oulenz/page_save_as

Tweak paginator to accommodate {slug}.html patterns
This commit is contained in:
Justin Mayer 2018-11-01 09:53:19 +01:00 committed by GitHub
commit c97c128d16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 30 deletions

View file

@ -8,6 +8,8 @@ Next release
* Replace Fabric by Invoke and ``fabfile.py`` template by ``tasks.py``. * Replace Fabric by Invoke and ``fabfile.py`` template by ``tasks.py``.
* Replace ``SLUG_SUBSTITUTIONS`` (and friends) by ``SLUG_REGEX_SUBSTITUTIONS`` * Replace ``SLUG_SUBSTITUTIONS`` (and friends) by ``SLUG_REGEX_SUBSTITUTIONS``
for more finegrained control for more finegrained control
* ``'{base_name}'`` value in ``PAGINATION_PATTERNS`` setting no longer strips
``'bar'`` from ``'foo/bar.html'`` (unless ``'bar' == 'index'``).
3.7.1 (2017-01-10) 3.7.1 (2017-01-10)
================== ==================

View file

@ -994,7 +994,10 @@ You can use the following settings to configure the pagination.
The templates to use pagination with, and the number of articles to include The templates to use pagination with, and the number of articles to include
on a page. If this value is ``None``, it defaults to ``DEFAULT_PAGINATION``. on a page. If this value is ``None``, it defaults to ``DEFAULT_PAGINATION``.
.. data:: PAGINATION_PATTERNS .. data:: PAGINATION_PATTERNS = (
(1, '{name}{extension}', '{name}{extension}'),
(2, '{name}{number}{extension}', '{name}{number}{extension}'),
)
A set of patterns that are used to determine advanced pagination output. A set of patterns that are used to determine advanced pagination output.
@ -1002,25 +1005,28 @@ You can use the following settings to configure the pagination.
Using Pagination Patterns Using Pagination Patterns
------------------------- -------------------------
The ``PAGINATION_PATTERNS`` setting can be used to configure where By default, pages subsequent to ``.../foo.html`` are created as
subsequent pages are created. The setting is a sequence of three ``.../foo2.html``, etc. The ``PAGINATION_PATTERNS`` setting can be used to
element tuples, where each tuple consists of:: change this. It takes a sequence of triples, where each triple consists of::
(minimum page, URL setting, SAVE_AS setting,) (minimum_page, page_url, page_save_as,)
For example, if you wanted the first page to just be ``/``, and the For ``page_url`` and ``page_save_as``, you may use a number of variables.
second (and subsequent) pages to be ``/page/2/``, you would set ``{url}`` and ``{save_as}`` correspond respectively to the ``*_URL`` and
``PAGINATION_PATTERNS`` as follows:: ``*_SAVE_AS`` values of the corresponding page type (e.g. ``ARTICLE_SAVE_AS``).
If ``{save_as} == foo/bar.html``, then ``{name} == foo/bar`` and
``{extension} == .html``. ``{base_name}`` equals ``{name}`` except that it
strips trailing ``/index`` if present. ``{number}`` equals the page number.
For example, if you want to leave the first page unchanged, but place
subsequent pages at ``.../page/2/`` etc, you could set ``PAGINATION_PATTERNS``
as follows::
PAGINATION_PATTERNS = ( PAGINATION_PATTERNS = (
(1, '{base_name}/', '{base_name}/index.html'), (1, '{url}', '{save_as}`,
(2, '{base_name}/page/{number}/', '{base_name}/page/{number}/index.html'), (2, '{base_name}/page/{number}/', '{base_name}/page/{number}/index.html'),
) )
This would cause the first page to be written to
``{base_name}/index.html``, and subsequent ones would be written into
``page/{number}`` directories.
Translations Translations
============ ============

View file

@ -66,7 +66,10 @@ class Paginator(object):
class Page(object): class Page(object):
def __init__(self, name, url, object_list, number, paginator, settings): def __init__(self, name, url, object_list, number, paginator, settings):
self.full_name = name
self.name, self.extension = os.path.splitext(name) self.name, self.extension = os.path.splitext(name)
dn, fn = os.path.split(name)
self.base_name = dn if fn in ('index.htm', 'index.html') else self.name
self.base_url = url self.base_url = url
self.object_list = object_list self.object_list = object_list
self.number = number self.number = number
@ -134,25 +137,16 @@ class Page(object):
# URL or SAVE_AS is a string, format it with a controlled context # URL or SAVE_AS is a string, format it with a controlled context
context = { context = {
'name': self.name.replace(os.sep, '/'), 'save_as': self.full_name,
'url': self.base_url, 'url': self.base_url,
'object_list': self.object_list, 'name': self.name,
'number': self.number, 'base_name': self.base_name,
'paginator': self.paginator,
'settings': self.settings,
'base_name': os.path.dirname(self.name),
'number_sep': '/',
'extension': self.extension, 'extension': self.extension,
'number': self.number,
} }
if self.number == 1:
# no page numbers on the first page
context['number'] = ''
context['number_sep'] = ''
ret = prop_value.format(**context) ret = prop_value.format(**context)
if ret[0] == '/': ret = ret.lstrip('/')
ret = ret[1:]
return ret return ret
url = property(functools.partial(_from_settings, key='URL')) url = property(functools.partial(_from_settings, key='URL'))

View file

@ -97,7 +97,8 @@ DEFAULT_CONFIG = {
'AUTHOR_URL': 'author/{slug}.html', 'AUTHOR_URL': 'author/{slug}.html',
'AUTHOR_SAVE_AS': posix_join('author', '{slug}.html'), 'AUTHOR_SAVE_AS': posix_join('author', '{slug}.html'),
'PAGINATION_PATTERNS': [ 'PAGINATION_PATTERNS': [
(0, '{name}{number}{extension}', '{name}{number}{extension}'), (1, '{name}{extension}', '{name}{extension}'),
(2, '{name}{number}{extension}', '{name}{number}{extension}'),
], ],
'YEAR_ARCHIVE_URL': '', 'YEAR_ARCHIVE_URL': '',
'YEAR_ARCHIVE_SAVE_AS': '', 'YEAR_ARCHIVE_SAVE_AS': '',

View file

@ -74,7 +74,7 @@ class TestPage(unittest.TestCase):
object_list, settings, 1) object_list, settings, 1)
page1 = paginator.page(1) page1 = paginator.page(1)
self.assertEqual(page1.save_as, 'blog/index.html') self.assertEqual(page1.save_as, 'blog/index.html')
self.assertEqual(page1.url, '//blog.my.site/') self.assertEqual(page1.url, 'blog.my.site/')
page2 = paginator.page(2) page2 = paginator.page(2)
self.assertEqual(page2.save_as, 'blog/2/index.html') self.assertEqual(page2.save_as, 'blog/2/index.html')
self.assertEqual(page2.url, '//blog.my.site/2/') self.assertEqual(page2.url, 'blog.my.site/2/')