mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Pagination - refactoring
This commit is contained in:
parent
636e939d23
commit
d272896adf
3 changed files with 59 additions and 61 deletions
|
|
@ -82,7 +82,7 @@ class Pelican(object):
|
||||||
return generators
|
return generators
|
||||||
|
|
||||||
def get_writer(self):
|
def get_writer(self):
|
||||||
return Writer(self.output_path)
|
return Writer(self.output_path, settings=self.settings)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,6 @@ from jinja2.exceptions import TemplateNotFound
|
||||||
from pelican.utils import copytree, get_relative_path, process_translations, open
|
from pelican.utils import copytree, get_relative_path, process_translations, open
|
||||||
from pelican.contents import Article, Page, is_valid_content
|
from pelican.contents import Article, Page, is_valid_content
|
||||||
from pelican.readers import read_file
|
from pelican.readers import read_file
|
||||||
from pelican.paginator import Paginator
|
|
||||||
|
|
||||||
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
|
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
|
||||||
'archives', 'page')
|
'archives', 'page')
|
||||||
|
|
@ -143,61 +142,26 @@ class ArticlesGenerator(Generator):
|
||||||
category=article.category)
|
category=article.category)
|
||||||
|
|
||||||
for template in _DIRECT_TEMPLATES:
|
for template in _DIRECT_TEMPLATES:
|
||||||
if self.settings.get('WITH_PAGINATION') and template in _PAGINATED_DIRECT_TEMPLATES:
|
paginated = {}
|
||||||
articles_paginator = Paginator(self.articles,
|
if template in _PAGINATED_DIRECT_TEMPLATES:
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
paginated = {'articles': self.articles, 'dates': self.dates}
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
write('%s.html' % template, templates[template], self.context,
|
||||||
dates_paginator = Paginator(self.dates,
|
blog=True, paginated=paginated, page_name=template)
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
|
||||||
for page_num in range(articles_paginator.num_pages):
|
|
||||||
write('%s%s.html' % (template, '%s' % (page_num > 0 and page_num+1 or '')),
|
|
||||||
templates[template], self.context, blog=True,
|
|
||||||
articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1),
|
|
||||||
dates_paginator=dates_paginator, dates_page=dates_paginator.page(page_num+1),
|
|
||||||
page_name='index')
|
|
||||||
else:
|
|
||||||
write('%s.html' % template, templates[template], self.context,
|
|
||||||
blog=True)
|
|
||||||
|
|
||||||
# and subfolders after that
|
# and subfolders after that
|
||||||
for tag, articles in self.tags.items():
|
for tag, articles in self.tags.items():
|
||||||
dates = [article for article in self.dates if article in articles]
|
dates = [article for article in self.dates if article in articles]
|
||||||
if self.settings.get('WITH_PAGINATION'):
|
write('tag/%s.html' % tag, templates['tag'], self.context,
|
||||||
articles_paginator = Paginator(articles,
|
tag=tag, articles=articles, dates=dates,
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
paginated={'articles': articles, 'dates': dates},
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
page_name='tag/%s'%tag)
|
||||||
dates_paginator = Paginator(dates,
|
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
|
||||||
for page_num in range(articles_paginator.num_pages):
|
|
||||||
write('tag/%s%s.html' % (tag, '%s' % (page_num > 0 and page_num+1 or '')),
|
|
||||||
templates['tag'], self.context, tag=tag, articles=articles, dates=dates,
|
|
||||||
articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1),
|
|
||||||
dates_paginator=dates_paginator, dates_page=dates_paginator.page(page_num+1),
|
|
||||||
page_name='tag/%s'%tag)
|
|
||||||
else:
|
|
||||||
write('tag/%s.html' % tag, templates['tag'], self.context,
|
|
||||||
tag=tag, articles=articles, dates=dates)
|
|
||||||
|
|
||||||
for cat, articles in self.categories:
|
for cat, articles in self.categories:
|
||||||
dates = [article for article in self.dates if article in articles]
|
dates = [article for article in self.dates if article in articles]
|
||||||
if self.settings.get('WITH_PAGINATION'):
|
write('category/%s.html' % cat, templates['category'], self.context,
|
||||||
articles_paginator = Paginator(articles,
|
category=cat, articles=articles, dates=dates,
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
paginated={'articles': articles, 'dates': dates},
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
page_name='category/%s' % cat)
|
||||||
dates_paginator = Paginator(dates,
|
|
||||||
self.settings.get('DEFAULT_PAGINATION'),
|
|
||||||
self.settings.get('DEFAULT_ORPHANS'))
|
|
||||||
for page_num in range(articles_paginator.num_pages):
|
|
||||||
write('category/%s%s.html' % (cat, '%s' % (page_num > 0 and page_num+1 or '')),
|
|
||||||
templates['category'], self.context, category=cat, articles=articles, dates=dates,
|
|
||||||
articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1),
|
|
||||||
dates_paginator=dates_paginator, dates_page=dates_paginator.page(page_num+1),
|
|
||||||
page_name='category/%s' % cat)
|
|
||||||
else:
|
|
||||||
write('category/%s.html' % cat, templates['category'], self.context,
|
|
||||||
category=cat, articles=articles, dates=dates)
|
|
||||||
|
|
||||||
def generate_context(self):
|
def generate_context(self):
|
||||||
"""change the context"""
|
"""change the context"""
|
||||||
|
|
|
||||||
|
|
@ -7,13 +7,15 @@ import locale
|
||||||
|
|
||||||
from feedgenerator import Atom1Feed, Rss201rev2Feed
|
from feedgenerator import Atom1Feed, Rss201rev2Feed
|
||||||
from pelican.utils import get_relative_path
|
from pelican.utils import get_relative_path
|
||||||
|
from pelican.paginator import Paginator
|
||||||
|
|
||||||
|
|
||||||
class Writer(object):
|
class Writer(object):
|
||||||
|
|
||||||
def __init__(self, output_path):
|
def __init__(self, output_path, settings=None):
|
||||||
self.output_path = output_path
|
self.output_path = output_path
|
||||||
self.reminder = dict()
|
self.reminder = dict()
|
||||||
|
self.settings = settings or {}
|
||||||
|
|
||||||
def _create_new_feed(self, feed_type, context):
|
def _create_new_feed(self, feed_type, context):
|
||||||
feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed
|
feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed
|
||||||
|
|
@ -74,15 +76,29 @@ class Writer(object):
|
||||||
locale.setlocale(locale.LC_ALL, old_locale)
|
locale.setlocale(locale.LC_ALL, old_locale)
|
||||||
|
|
||||||
def write_file(self, name, template, context, relative_urls=True,
|
def write_file(self, name, template, context, relative_urls=True,
|
||||||
**kwargs):
|
paginated=None, **kwargs):
|
||||||
"""Render the template and write the file.
|
"""Render the template and write the file.
|
||||||
|
|
||||||
:param name: name of the file to output
|
:param name: name of the file to output
|
||||||
:param template: template to use to generate the content
|
:param template: template to use to generate the content
|
||||||
:param context: dict to pass to the templates.
|
:param context: dict to pass to the templates.
|
||||||
:param relative_urls: use relative urls or absolutes ones
|
:param relative_urls: use relative urls or absolutes ones
|
||||||
|
:param paginated: dict of article list to paginate - must have the same length (same list in different orders)
|
||||||
:param **kwargs: additional variables to pass to the templates
|
:param **kwargs: additional variables to pass to the templates
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
def _write_file(template, localcontext, output_path, name):
|
||||||
|
"""Render the template write the file."""
|
||||||
|
output = template.render(localcontext)
|
||||||
|
filename = os.sep.join((output_path, name))
|
||||||
|
try:
|
||||||
|
os.makedirs(os.path.dirname(filename))
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
with open(filename, 'w', encoding='utf-8') as f:
|
||||||
|
f.write(output)
|
||||||
|
print u' [ok] writing %s' % filename
|
||||||
|
|
||||||
localcontext = context.copy()
|
localcontext = context.copy()
|
||||||
if relative_urls:
|
if relative_urls:
|
||||||
localcontext['SITEURL'] = get_relative_path(name)
|
localcontext['SITEURL'] = get_relative_path(name)
|
||||||
|
|
@ -90,15 +106,33 @@ class Writer(object):
|
||||||
localcontext.update(kwargs)
|
localcontext.update(kwargs)
|
||||||
self.update_context_contents(name, localcontext)
|
self.update_context_contents(name, localcontext)
|
||||||
|
|
||||||
output = template.render(localcontext)
|
# check paginated
|
||||||
filename = os.sep.join((self.output_path, name))
|
paginated = paginated or {}
|
||||||
try:
|
if self.settings.get('WITH_PAGINATION') and paginated:
|
||||||
os.makedirs(os.path.dirname(filename))
|
# pagination needed, init paginators
|
||||||
except Exception:
|
paginators = {}
|
||||||
pass
|
for key in paginated.iterkeys():
|
||||||
with open(filename, 'w', encoding='utf-8') as f:
|
object_list = paginated[key]
|
||||||
f.write(output)
|
paginators[key] = Paginator(object_list,
|
||||||
print u' [ok] writing %s' % filename
|
self.settings.get('DEFAULT_PAGINATION'),
|
||||||
|
self.settings.get('DEFAULT_ORPHANS'))
|
||||||
|
# generated pages, and write
|
||||||
|
for page_num in range(paginators.values()[0].num_pages):
|
||||||
|
paginated_localcontext = localcontext.copy()
|
||||||
|
paginated_name = name
|
||||||
|
for key in paginators.iterkeys():
|
||||||
|
paginator = paginators[key]
|
||||||
|
page = paginator.page(page_num+1)
|
||||||
|
paginated_localcontext.update({'%s_paginator' % key: paginator,
|
||||||
|
'%s_page' % key: page})
|
||||||
|
if page_num > 0:
|
||||||
|
# FIXME file extension
|
||||||
|
paginated_name = paginated_name.replace('.html', '%s.html' % (page_num+1))
|
||||||
|
|
||||||
|
_write_file(template, paginated_localcontext, self.output_path, paginated_name)
|
||||||
|
else:
|
||||||
|
# no pagination
|
||||||
|
_write_file(template, localcontext, self.output_path, name)
|
||||||
|
|
||||||
def update_context_contents(self, name, context):
|
def update_context_contents(self, name, context):
|
||||||
"""Recursively run the context to find elements (articles, pages, etc) whose content getter needs to
|
"""Recursively run the context to find elements (articles, pages, etc) whose content getter needs to
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue