mirror of
https://github.com/getpelican/pelican.git
synced 2025-10-15 20:28:56 +02:00
Pagination added for index and tag/category pages
This commit is contained in:
parent
4edb5bf497
commit
4111acd1c1
6 changed files with 164 additions and 8 deletions
|
|
@ -12,10 +12,12 @@ from jinja2.exceptions import TemplateNotFound
|
|||
from pelican.utils import copytree, get_relative_path, process_translations, open
|
||||
from pelican.contents import Article, Page, is_valid_content
|
||||
from pelican.readers import read_file
|
||||
from pelican.paginator import Paginator
|
||||
|
||||
_TEMPLATES = ('index', 'tag', 'tags', 'article', 'category', 'categories',
|
||||
'archives', 'page')
|
||||
_DIRECT_TEMPLATES = ('index', 'tags', 'categories', 'archives')
|
||||
_PAGINATED_DIRECT_TEMPLATES = ('index', 'tag', 'category')
|
||||
|
||||
|
||||
class Generator(object):
|
||||
|
|
@ -141,18 +143,58 @@ class ArticlesGenerator(Generator):
|
|||
category=article.category)
|
||||
|
||||
for template in _DIRECT_TEMPLATES:
|
||||
write('%s.html' % template, templates[template], self.context,
|
||||
blog=True)
|
||||
if self.settings.get('WITH_PAGINATION') and template in _PAGINATED_DIRECT_TEMPLATES:
|
||||
articles_paginator = Paginator(self.articles,
|
||||
self.settings.get('DEFAULT_PAGINATION'),
|
||||
self.settings.get('DEFAULT_ORPHANS'))
|
||||
dates_paginator = Paginator(self.dates,
|
||||
self.settings.get('DEFAULT_PAGINATION'),
|
||||
self.settings.get('DEFAULT_ORPHANS'))
|
||||
for page_num in range(articles_paginator.num_pages):
|
||||
# update context with paginator object and current page
|
||||
self.context.update({'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'})
|
||||
write('%s%s.html' % (template, '%s' % (page_num > 0 and page_num+1 or '')),
|
||||
templates[template], self.context, blog=True)
|
||||
else:
|
||||
write('%s.html' % template, templates[template], self.context,
|
||||
blog=True)
|
||||
|
||||
# and subfolders after that
|
||||
for tag, articles in self.tags.items():
|
||||
for article in articles:
|
||||
if self.settings.get('WITH_PAGINATION') and 'tag' in _PAGINATED_DIRECT_TEMPLATES:
|
||||
articles_paginator = Paginator(articles,
|
||||
self.settings.get('DEFAULT_PAGINATION'),
|
||||
self.settings.get('DEFAULT_ORPHANS'))
|
||||
for page_num in range(articles_paginator.num_pages):
|
||||
# update context with paginator object and current page
|
||||
self.context.update({'articles_paginator': articles_paginator,
|
||||
'articles_page': articles_paginator.page(page_num+1),
|
||||
'page_name': 'tag/%s' % tag})
|
||||
write('tag/%s%s.html' % (tag, '%s' % (page_num > 0 and page_num+1 or '')),
|
||||
templates['tag'], self.context, blog=True)
|
||||
else:
|
||||
write('tag/%s.html' % tag, templates['tag'], self.context,
|
||||
tag=tag, articles=articles)
|
||||
|
||||
for cat, articles in self.categories:
|
||||
write('category/%s.html' % cat, templates['category'], self.context,
|
||||
category=cat, articles=articles)
|
||||
if self.settings.get('WITH_PAGINATION') and 'tag' in _PAGINATED_DIRECT_TEMPLATES:
|
||||
articles_paginator = Paginator(articles,
|
||||
self.settings.get('DEFAULT_PAGINATION'),
|
||||
self.settings.get('DEFAULT_ORPHANS'))
|
||||
for page_num in range(articles_paginator.num_pages):
|
||||
# update context with paginator object and current page
|
||||
self.context.update({'articles_paginator': articles_paginator,
|
||||
'articles_page': articles_paginator.page(page_num+1),
|
||||
'page_name': 'category/%s' % cat})
|
||||
write('category/%s%s.html' % (cat, '%s' % (page_num > 0 and page_num+1 or '')),
|
||||
templates['category'], self.context, blog=True)
|
||||
else:
|
||||
write('category/%s.html' % cat, templates['category'], self.context,
|
||||
category=cat, articles=articles)
|
||||
|
||||
def generate_context(self):
|
||||
"""change the context"""
|
||||
|
|
|
|||
85
pelican/paginator.py
Normal file
85
pelican/paginator.py
Normal file
|
|
@ -0,0 +1,85 @@
|
|||
# From django.core.paginator
|
||||
from math import ceil
|
||||
|
||||
class Paginator(object):
|
||||
def __init__(self, object_list, per_page, orphans=0):
|
||||
self.object_list = object_list
|
||||
self.per_page = per_page
|
||||
self.orphans = orphans
|
||||
self._num_pages = self._count = None
|
||||
|
||||
def page(self, number):
|
||||
"Returns a Page object for the given 1-based page number."
|
||||
bottom = (number - 1) * self.per_page
|
||||
top = bottom + self.per_page
|
||||
if top + self.orphans >= self.count:
|
||||
top = self.count
|
||||
return Page(self.object_list[bottom:top], number, self)
|
||||
|
||||
def _get_count(self):
|
||||
"Returns the total number of objects, across all pages."
|
||||
if self._count is None:
|
||||
self._count = len(self.object_list)
|
||||
return self._count
|
||||
count = property(_get_count)
|
||||
|
||||
def _get_num_pages(self):
|
||||
"Returns the total number of pages."
|
||||
if self._num_pages is None:
|
||||
hits = max(1, self.count - self.orphans)
|
||||
self._num_pages = int(ceil(hits / float(self.per_page)))
|
||||
return self._num_pages
|
||||
num_pages = property(_get_num_pages)
|
||||
|
||||
def _get_page_range(self):
|
||||
"""
|
||||
Returns a 1-based range of pages for iterating through within
|
||||
a template for loop.
|
||||
"""
|
||||
return range(1, self.num_pages + 1)
|
||||
page_range = property(_get_page_range)
|
||||
|
||||
class Page(object):
|
||||
def __init__(self, object_list, number, paginator):
|
||||
self.object_list = object_list
|
||||
self.number = number
|
||||
self.paginator = paginator
|
||||
|
||||
def __repr__(self):
|
||||
return '<Page %s of %s>' % (self.number, self.paginator.num_pages)
|
||||
|
||||
def has_next(self):
|
||||
return self.number < self.paginator.num_pages
|
||||
|
||||
def has_previous(self):
|
||||
return self.number > 1
|
||||
|
||||
def has_other_pages(self):
|
||||
return self.has_previous() or self.has_next()
|
||||
|
||||
def next_page_number(self):
|
||||
return self.number + 1
|
||||
|
||||
def previous_page_number(self):
|
||||
return self.number - 1
|
||||
|
||||
def start_index(self):
|
||||
"""
|
||||
Returns the 1-based index of the first object on this page,
|
||||
relative to total objects in the paginator.
|
||||
"""
|
||||
# Special case, return zero if no items.
|
||||
if self.paginator.count == 0:
|
||||
return 0
|
||||
return (self.paginator.per_page * (self.number - 1)) + 1
|
||||
|
||||
def end_index(self):
|
||||
"""
|
||||
Returns the 1-based index of the last object on this page,
|
||||
relative to total objects found (hits).
|
||||
"""
|
||||
# Special case for the last page because there can be orphans.
|
||||
if self.number == self.paginator.num_pages:
|
||||
return self.paginator.count
|
||||
return self.number * self.paginator.per_page
|
||||
|
||||
|
|
@ -30,6 +30,9 @@ _DEFAULT_CONFIG = {'PATH': None,
|
|||
'DATE_FORMATS': {},
|
||||
'JINJA_EXTENSIONS': [],
|
||||
'LOCALE': '', # default to user locale
|
||||
'WITH_PAGINATION': True,
|
||||
'DEFAULT_PAGINATION': 5,
|
||||
'DEFAULT_ORPHANS': 0,
|
||||
}
|
||||
|
||||
def read_settings(filename):
|
||||
|
|
|
|||
|
|
@ -2,8 +2,8 @@
|
|||
{% block content_title %}{% endblock %}
|
||||
{% block content %}
|
||||
{% if articles %}
|
||||
{% for article in articles %}
|
||||
{% if loop.index == 1 %}
|
||||
{% for article in articles_page.object_list %}
|
||||
{% if loop.first and not articles_page.has_previous() %}
|
||||
<aside id="featured" class="body"><article>
|
||||
<h1 class="entry-title"><a href="{{ SITEURL }}/{{ article.url
|
||||
}}">{{ article.title }}</a></h1>
|
||||
|
|
@ -11,14 +11,21 @@
|
|||
{{ article.content }}
|
||||
{% include 'comments.html' %}
|
||||
</article>
|
||||
{% if loop.length == 1 %}
|
||||
{% include 'paginator.html' %}
|
||||
{% endif %}
|
||||
</aside><!-- /#featured -->
|
||||
{% if loop.length > 1 %}
|
||||
{% if loop.length > 1 %}
|
||||
<section id="content" class="body">
|
||||
<h1>Other articles</h1>
|
||||
<hr />
|
||||
<ol id="posts-list" class="hfeed">
|
||||
{% endif %}
|
||||
{% else %}
|
||||
{% if loop.first and articles_page.has_previous %}
|
||||
<section id="content" class="body">
|
||||
<ol id="posts-list" class="hfeed" start="{{ articles_paginator.per_page -1 }}">
|
||||
{% endif %}
|
||||
<li><article class="hentry">
|
||||
<header>
|
||||
<h1><a href="{{ SITEURL }}/{{ article.url }}" rel="bookmark" title="Permalink to {{ article.title}}">{{ article.title }}</a></h1>
|
||||
|
|
@ -32,9 +39,14 @@
|
|||
</div><!-- /.entry-content -->
|
||||
</article></li>
|
||||
{% endif %}
|
||||
{% if loop.last and (articles_page.has_previous() or not articles_page.has_previous() and loop.length > 1) %}
|
||||
{% include 'paginator.html' %}
|
||||
{% endif %}
|
||||
{% endfor %}
|
||||
</ol><!-- /#posts-list -->
|
||||
{% if loop.length > 1 %}
|
||||
</section><!-- /#content -->
|
||||
{% endif %}
|
||||
{% else %}
|
||||
<section id="content" class="body">
|
||||
<h2>Pages</h2>
|
||||
|
|
|
|||
13
pelican/themes/notmyidea/templates/paginator.html
Normal file
13
pelican/themes/notmyidea/templates/paginator.html
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<p class="paginator">
|
||||
{% if articles_page.has_previous() %}
|
||||
{% if articles_page.previous_page_number() == 1 %}
|
||||
<a href="{{ SITEURL }}/{{ page_name }}.html">«</a>
|
||||
{% else %}
|
||||
<a href="{{ SITEURL }}/{{ page_name }}{{ articles_page.previous_page_number() }}.html">«</a>
|
||||
{% endif %}
|
||||
{% endif %}
|
||||
Page {{ articles_page.number }} / {{ articles_paginator.num_pages }}
|
||||
{% if articles_page.has_next() %}
|
||||
<a href="{{ SITEURL }}/{{ page_name }}{{ articles_page.next_page_number() }}.html">»</a>
|
||||
{% endif %}
|
||||
</p>
|
||||
|
|
@ -8,6 +8,7 @@ DISQUS_SITENAME = "blog-notmyidea"
|
|||
PDF_GENERATOR = False
|
||||
REVERSE_CATEGORY_ORDER = True
|
||||
LOCALE = 'fr_FR.utf8'
|
||||
DEFAULT_PAGINATION = 2
|
||||
|
||||
FEED_RSS = 'feeds/all.rss.xml'
|
||||
CATEGORY_FEED_RSS = 'feeds/%s.rss.xml'
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue