From 4111acd1c136b77ec11e2b8962089502ee4c9bba Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Mon, 14 Feb 2011 16:24:54 +0100 Subject: [PATCH 01/13] Pagination added for index and tag/category pages --- pelican/generators.py | 52 ++++++++++-- pelican/paginator.py | 85 +++++++++++++++++++ pelican/settings.py | 3 + pelican/themes/notmyidea/templates/index.html | 18 +++- .../themes/notmyidea/templates/paginator.html | 13 +++ samples/pelican.conf.py | 1 + 6 files changed, 164 insertions(+), 8 deletions(-) create mode 100644 pelican/paginator.py create mode 100644 pelican/themes/notmyidea/templates/paginator.html diff --git a/pelican/generators.py b/pelican/generators.py index c50b55e1..088a7271 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -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""" diff --git a/pelican/paginator.py b/pelican/paginator.py new file mode 100644 index 00000000..a2a452b9 --- /dev/null +++ b/pelican/paginator.py @@ -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 '' % (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 + diff --git a/pelican/settings.py b/pelican/settings.py index d84178fe..dbd230d4 100644 --- a/pelican/settings.py +++ b/pelican/settings.py @@ -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): diff --git a/pelican/themes/notmyidea/templates/index.html b/pelican/themes/notmyidea/templates/index.html index 5969e6c8..57b7c1a7 100644 --- a/pelican/themes/notmyidea/templates/index.html +++ b/pelican/themes/notmyidea/templates/index.html @@ -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() %} - {% if loop.length > 1 %} + {% if loop.length > 1 %}

Other articles


    {% endif %} {% else %} + {% if loop.first and articles_page.has_previous %} +
    +
      + {% endif %}
    1. {% endif %} + {% if loop.last and (articles_page.has_previous() or not articles_page.has_previous() and loop.length > 1) %} + {% include 'paginator.html' %} + {% endif %} {% endfor %}
    +{% if loop.length > 1 %}
    +{% endif %} {% else %}

    Pages

    diff --git a/pelican/themes/notmyidea/templates/paginator.html b/pelican/themes/notmyidea/templates/paginator.html new file mode 100644 index 00000000..9cce0237 --- /dev/null +++ b/pelican/themes/notmyidea/templates/paginator.html @@ -0,0 +1,13 @@ +

    + {% if articles_page.has_previous() %} + {% if articles_page.previous_page_number() == 1 %} + « + {% else %} + « + {% endif %} + {% endif %} + Page {{ articles_page.number }} / {{ articles_paginator.num_pages }} + {% if articles_page.has_next() %} + » + {% endif %} +

    diff --git a/samples/pelican.conf.py b/samples/pelican.conf.py index 10d5497a..07c49d01 100755 --- a/samples/pelican.conf.py +++ b/samples/pelican.conf.py @@ -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' From dc6771d60457a996ea8cc01a32695ad5d53b171c Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Mon, 14 Feb 2011 16:46:18 +0100 Subject: [PATCH 02/13] Pagination - fix context --- pelican/generators.py | 27 ++++++++++----------------- 1 file changed, 10 insertions(+), 17 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index 088a7271..5029e397 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -151,14 +151,11 @@ class ArticlesGenerator(Generator): 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) + 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) @@ -170,12 +167,10 @@ class ArticlesGenerator(Generator): 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) + templates['tag'], self.context, tag=tag, articles=articles, + articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1), + page_name='tag/%s'%tag) else: write('tag/%s.html' % tag, templates['tag'], self.context, tag=tag, articles=articles) @@ -186,12 +181,10 @@ class ArticlesGenerator(Generator): 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) + templates['category'], self.context, category=cat, articles=articles, + articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1), + page_name='category/%s' % cat) else: write('category/%s.html' % cat, templates['category'], self.context, category=cat, articles=articles) From 60756678abdbfbcd12fafc2c9a06ac2aa39d8650 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:41:55 +0100 Subject: [PATCH 03/13] Pagination - simplification --- pelican/generators.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index 5029e397..a2da66a4 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -17,7 +17,7 @@ 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') +_PAGINATED_DIRECT_TEMPLATES = ('index', ) class Generator(object): @@ -162,7 +162,7 @@ class ArticlesGenerator(Generator): # and subfolders after that for tag, articles in self.tags.items(): - if self.settings.get('WITH_PAGINATION') and 'tag' in _PAGINATED_DIRECT_TEMPLATES: + if self.settings.get('WITH_PAGINATION'): articles_paginator = Paginator(articles, self.settings.get('DEFAULT_PAGINATION'), self.settings.get('DEFAULT_ORPHANS')) @@ -176,7 +176,7 @@ class ArticlesGenerator(Generator): tag=tag, articles=articles) for cat, articles in self.categories: - if self.settings.get('WITH_PAGINATION') and 'tag' in _PAGINATED_DIRECT_TEMPLATES: + if self.settings.get('WITH_PAGINATION'): articles_paginator = Paginator(articles, self.settings.get('DEFAULT_PAGINATION'), self.settings.get('DEFAULT_ORPHANS')) From be7274064b847bea6e9cde14f1ea73989d000330 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:44:36 +0100 Subject: [PATCH 04/13] Cat and tag page generation - update dates list with articles of current tag/category --- pelican/generators.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index a2da66a4..d116a28e 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -162,32 +162,34 @@ class ArticlesGenerator(Generator): # and subfolders after that for tag, articles in self.tags.items(): + dates = [article for article in self.dates if article in articles] if self.settings.get('WITH_PAGINATION'): articles_paginator = Paginator(articles, 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, + templates['tag'], self.context, tag=tag, articles=articles, dates=dates, articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1), page_name='tag/%s'%tag) else: write('tag/%s.html' % tag, templates['tag'], self.context, - tag=tag, articles=articles) + tag=tag, articles=articles, dates=dates) for cat, articles in self.categories: + dates = [article for article in self.dates if article in articles] if self.settings.get('WITH_PAGINATION'): articles_paginator = Paginator(articles, 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, + templates['category'], self.context, category=cat, articles=articles, dates=dates, articles_paginator=articles_paginator, articles_page=articles_paginator.page(page_num+1), page_name='category/%s' % cat) else: write('category/%s.html' % cat, templates['category'], self.context, - category=cat, articles=articles) + category=cat, articles=articles, dates=dates) def generate_context(self): """change the context""" From b042974673e0bab5e6d8c145c9a325b6c3d09613 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:46:31 +0100 Subject: [PATCH 05/13] Paginiation - add dates pagination to tag/category pages --- pelican/generators.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/pelican/generators.py b/pelican/generators.py index d116a28e..0279bda3 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -167,10 +167,14 @@ class ArticlesGenerator(Generator): articles_paginator = Paginator(articles, self.settings.get('DEFAULT_PAGINATION'), self.settings.get('DEFAULT_ORPHANS')) + 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, @@ -182,10 +186,14 @@ class ArticlesGenerator(Generator): articles_paginator = Paginator(articles, self.settings.get('DEFAULT_PAGINATION'), self.settings.get('DEFAULT_ORPHANS')) + 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, From eb0f33df165ffcfb7750fc418e14d23c871673f6 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:48:14 +0100 Subject: [PATCH 06/13] Pagination - rename template --- pelican/themes/notmyidea/templates/index.html | 4 ++-- .../notmyidea/templates/{paginator.html => pagination.html} | 0 2 files changed, 2 insertions(+), 2 deletions(-) rename pelican/themes/notmyidea/templates/{paginator.html => pagination.html} (100%) diff --git a/pelican/themes/notmyidea/templates/index.html b/pelican/themes/notmyidea/templates/index.html index 57b7c1a7..ded814ff 100644 --- a/pelican/themes/notmyidea/templates/index.html +++ b/pelican/themes/notmyidea/templates/index.html @@ -12,7 +12,7 @@ {% include 'comments.html' %} {% if loop.length == 1 %} - {% include 'paginator.html' %} + {% include 'pagination.html' %} {% endif %} {% if loop.length > 1 %} @@ -40,7 +40,7 @@ {% endif %} {% if loop.last and (articles_page.has_previous() or not articles_page.has_previous() and loop.length > 1) %} - {% include 'paginator.html' %} + {% include 'pagination.html' %} {% endif %} {% endfor %}
diff --git a/pelican/themes/notmyidea/templates/paginator.html b/pelican/themes/notmyidea/templates/pagination.html similarity index 100% rename from pelican/themes/notmyidea/templates/paginator.html rename to pelican/themes/notmyidea/templates/pagination.html From 17fe724ca490024ca742cb7fd568fdd191600dcc Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:53:20 +0100 Subject: [PATCH 07/13] Fix category url in base template - simple theme --- pelican/themes/simple/templates/base.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pelican/themes/simple/templates/base.html b/pelican/themes/simple/templates/base.html index 26103864..eaf4484f 100644 --- a/pelican/themes/simple/templates/base.html +++ b/pelican/themes/simple/templates/base.html @@ -11,7 +11,7 @@ {% if categories %} {% endif %} {% block content %} From 636e939d2324a29ecfbbafc9fbc4f2d765dd1de9 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 13:57:00 +0100 Subject: [PATCH 08/13] Pagination - update simple theme --- pelican/themes/simple/templates/index.html | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/pelican/themes/simple/templates/index.html b/pelican/themes/simple/templates/index.html index c09dbf5d..0e4ef141 100644 --- a/pelican/themes/simple/templates/index.html +++ b/pelican/themes/simple/templates/index.html @@ -6,7 +6,7 @@ {% endblock %}
    -{% for article in articles %} +{% for article in articles_page.object_list %}
  1. {% endfor %}
+

+ {% if articles_page.has_previous() %} + {% if articles_page.previous_page_number() == 1 %} + « + {% else %} + « + {% endif %} + {% endif %} + Page {{ articles_page.number }} / {{ articles_paginator.num_pages }} + {% if articles_page.has_next() %} + » + {% endif %} +

{% endblock content %} From d272896adfe5bbd996b726fcf5ee926578784479 Mon Sep 17 00:00:00 2001 From: Laureline Guerin Date: Tue, 15 Feb 2011 14:36:55 +0100 Subject: [PATCH 09/13] Pagination - refactoring --- pelican/__init__.py | 2 +- pelican/generators.py | 62 +++++++++---------------------------------- pelican/writers.py | 56 ++++++++++++++++++++++++++++++-------- 3 files changed, 59 insertions(+), 61 deletions(-) diff --git a/pelican/__init__.py b/pelican/__init__.py index f599cc46..8114cae8 100755 --- a/pelican/__init__.py +++ b/pelican/__init__.py @@ -82,7 +82,7 @@ class Pelican(object): return generators def get_writer(self): - return Writer(self.output_path) + return Writer(self.output_path, settings=self.settings) diff --git a/pelican/generators.py b/pelican/generators.py index 0279bda3..94f1a21c 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -12,7 +12,6 @@ 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') @@ -143,61 +142,26 @@ class ArticlesGenerator(Generator): category=article.category) for template in _DIRECT_TEMPLATES: - 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): - 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) + paginated = {} + if template in _PAGINATED_DIRECT_TEMPLATES: + paginated = {'articles': self.articles, 'dates': self.dates} + write('%s.html' % template, templates[template], self.context, + blog=True, paginated=paginated, page_name=template) # and subfolders after that for tag, articles in self.tags.items(): dates = [article for article in self.dates if article in articles] - if self.settings.get('WITH_PAGINATION'): - articles_paginator = Paginator(articles, - self.settings.get('DEFAULT_PAGINATION'), - self.settings.get('DEFAULT_ORPHANS')) - 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) + write('tag/%s.html' % tag, templates['tag'], self.context, + tag=tag, articles=articles, dates=dates, + paginated={'articles': articles, 'dates': dates}, + page_name='tag/%s'%tag) for cat, articles in self.categories: dates = [article for article in self.dates if article in articles] - if self.settings.get('WITH_PAGINATION'): - articles_paginator = Paginator(articles, - self.settings.get('DEFAULT_PAGINATION'), - self.settings.get('DEFAULT_ORPHANS')) - 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) + write('category/%s.html' % cat, templates['category'], self.context, + category=cat, articles=articles, dates=dates, + paginated={'articles': articles, 'dates': dates}, + page_name='category/%s' % cat) def generate_context(self): """change the context""" diff --git a/pelican/writers.py b/pelican/writers.py index ea53aead..9f3779d4 100644 --- a/pelican/writers.py +++ b/pelican/writers.py @@ -7,13 +7,15 @@ import locale from feedgenerator import Atom1Feed, Rss201rev2Feed from pelican.utils import get_relative_path +from pelican.paginator import Paginator class Writer(object): - def __init__(self, output_path): + def __init__(self, output_path, settings=None): self.output_path = output_path self.reminder = dict() + self.settings = settings or {} def _create_new_feed(self, feed_type, context): feed_class = Rss201rev2Feed if feed_type == 'rss' else Atom1Feed @@ -74,15 +76,29 @@ class Writer(object): locale.setlocale(locale.LC_ALL, old_locale) def write_file(self, name, template, context, relative_urls=True, - **kwargs): + paginated=None, **kwargs): """Render the template and write the file. :param name: name of the file to output :param template: template to use to generate the content :param context: dict to pass to the templates. :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 """ + + 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() if relative_urls: localcontext['SITEURL'] = get_relative_path(name) @@ -90,15 +106,33 @@ class Writer(object): localcontext.update(kwargs) self.update_context_contents(name, localcontext) - output = template.render(localcontext) - filename = os.sep.join((self.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 + # check paginated + paginated = paginated or {} + if self.settings.get('WITH_PAGINATION') and paginated: + # pagination needed, init paginators + paginators = {} + for key in paginated.iterkeys(): + object_list = paginated[key] + paginators[key] = Paginator(object_list, + 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): """Recursively run the context to find elements (articles, pages, etc) whose content getter needs to From e0e4155e89e00e928fb1617bb61737f31fd60125 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Tue, 15 Feb 2011 13:48:57 +0000 Subject: [PATCH 10/13] PEP 8 fixes --- pelican/generators.py | 9 ++++++--- pelican/writers.py | 30 +++++++++++++++++------------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/pelican/generators.py b/pelican/generators.py index 94f1a21c..c2312ddb 100755 --- a/pelican/generators.py +++ b/pelican/generators.py @@ -36,7 +36,8 @@ class Generator(object): templates ready to use with Jinja2. """ path = os.path.expanduser(os.path.join(self.theme, 'templates')) - env = Environment(loader=FileSystemLoader(path),extensions=self.settings.get('JINJA_EXTENSIONS', [])) + env = Environment(loader=FileSystemLoader(path), + extensions=self.settings.get('JINJA_EXTENSIONS', [])) templates = {} for template in _TEMPLATES: try: @@ -135,7 +136,8 @@ class ArticlesGenerator(Generator): writer.write_file, relative_urls = self.settings.get('RELATIVE_URLS') ) - # to minimize the number of relative path stuff modification in writer, articles pass first + # to minimize the number of relative path stuff modification + # in writer, articles pass first for article in chain(self.translations, self.articles): write('%s' % article.save_as, templates['article'], self.context, article=article, @@ -291,7 +293,8 @@ class PdfGenerator(Generator): pass def generate_output(self, writer=None): - # we don't use the writer passed as argument here, since we write our own files + # we don't use the writer passed as argument here + # since we write our own files print u' Generating PDF files...' pdf_path = os.path.join(self.output_path, 'pdf') try: diff --git a/pelican/writers.py b/pelican/writers.py index 9f3779d4..18a09f05 100644 --- a/pelican/writers.py +++ b/pelican/writers.py @@ -83,7 +83,8 @@ class Writer(object): :param template: template to use to generate the content :param context: dict to pass to the templates. :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 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 """ @@ -114,8 +115,8 @@ class Writer(object): for key in paginated.iterkeys(): object_list = paginated[key] paginators[key] = Paginator(object_list, - self.settings.get('DEFAULT_PAGINATION'), - self.settings.get('DEFAULT_ORPHANS')) + 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() @@ -127,19 +128,22 @@ class Writer(object): '%s_page' % key: page}) if page_num > 0: # FIXME file extension - paginated_name = paginated_name.replace('.html', '%s.html' % (page_num+1)) + paginated_name = paginated_name.replace('.html', + '%s.html' % (page_num+1)) - _write_file(template, paginated_localcontext, self.output_path, paginated_name) + _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): - """Recursively run the context to find elements (articles, pages, etc) whose content getter needs to - be modified in order to deal with relative paths. + """Recursively run the context to find elements (articles, pages, etc) + whose content getter needs to + be modified in order to deal with relative paths. - :param name: name of the file to output. - :param context: dict that will be passed to the templates. + :param name: name of the file to output. + :param context: dict that will be passed to the templates. """ if context is None: return None @@ -173,13 +177,13 @@ class Writer(object): return context def inject_update_method(self, name, item): - """Replace the content attribute getter of an element by a function that will deals with its - relatives paths. + """Replace the content attribute getter of an element by a function + that will deals with its relatives paths. """ def _update_object_content(name, input): - """Change all the relatives paths of the input content to relatives paths - suitable fot the ouput content + """Change all the relatives paths of the input content to relatives + paths suitable fot the ouput content :param name: path of the output. :param input: input resource that will be passed to the templates. From b1f6cfb2c9a5fc73c9e36cf48c6490f976db01e6 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Tue, 15 Feb 2011 14:27:27 +0000 Subject: [PATCH 11/13] pagination - fix extension --- pelican/writers.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pelican/writers.py b/pelican/writers.py index 18a09f05..b8dda376 100644 --- a/pelican/writers.py +++ b/pelican/writers.py @@ -127,9 +127,9 @@ class Writer(object): 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)) + ext = '.' + paginated_name.rsplit('.')[-1] + paginated_name = paginated_name.replace(ext, + '%s%s' % (page_num + 1, ext)) _write_file(template, paginated_localcontext, self.output_path, paginated_name) From 8171e1753c0cdda3e13c452717019f47fa056928 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Thu, 17 Feb 2011 19:03:23 +0000 Subject: [PATCH 12/13] Fix a template bug. --- pelican/themes/notmyidea/templates/index.html | 60 ++++++++++--------- 1 file changed, 31 insertions(+), 29 deletions(-) diff --git a/pelican/themes/notmyidea/templates/index.html b/pelican/themes/notmyidea/templates/index.html index ded814ff..217bacf2 100644 --- a/pelican/themes/notmyidea/templates/index.html +++ b/pelican/themes/notmyidea/templates/index.html @@ -2,31 +2,32 @@ {% block content_title %}{% endblock %} {% block content %} {% if articles %} -{% for article in articles_page.object_list %} - {% if loop.first and not articles_page.has_previous() %} - + {% if loop.length > 1 %} +
+

Other articles

+
+
    + {% endif %} + {# other items #} + {% else %} + {% if loop.first and articles_page.has_previous %} +
    +
      + {% endif %} +
    1. - {% endif %} - {% if loop.last and (articles_page.has_previous() or not articles_page.has_previous() and loop.length > 1) %} - {% include 'pagination.html' %} - {% endif %} + + {% endif %} + {% if loop.last and (articles_page.has_previous() + or not articles_page.has_previous() and loop.length > 1) %} + {% include 'pagination.html' %} + {% endif %} {% endfor %} -
    -{% if loop.length > 1 %} -
    + {% if loop.length > 1 or articles_page.has_previous() %} +
+
{% endif %} {% else %}
From 80d1fdaef3e994fb5e058a96348cc68e2cf76892 Mon Sep 17 00:00:00 2001 From: Alexis Metaireau Date: Thu, 17 Feb 2011 19:03:55 +0000 Subject: [PATCH 13/13] Add some few test content. --- samples/content/cat1/article1.rst | 6 ++++++ samples/content/cat1/article2.rst | 6 ++++++ samples/content/cat1/article3.rst | 6 ++++++ 3 files changed, 18 insertions(+) create mode 100644 samples/content/cat1/article1.rst create mode 100644 samples/content/cat1/article2.rst create mode 100644 samples/content/cat1/article3.rst diff --git a/samples/content/cat1/article1.rst b/samples/content/cat1/article1.rst new file mode 100644 index 00000000..4789543b --- /dev/null +++ b/samples/content/cat1/article1.rst @@ -0,0 +1,6 @@ +Article 1 +######### + +:date: 2011-02-17 + +Article 1 diff --git a/samples/content/cat1/article2.rst b/samples/content/cat1/article2.rst new file mode 100644 index 00000000..a4f87866 --- /dev/null +++ b/samples/content/cat1/article2.rst @@ -0,0 +1,6 @@ +Article 2 +######### + +:date: 2011-02-17 + +Article 2 diff --git a/samples/content/cat1/article3.rst b/samples/content/cat1/article3.rst new file mode 100644 index 00000000..53471177 --- /dev/null +++ b/samples/content/cat1/article3.rst @@ -0,0 +1,6 @@ +Article 3 +######### + +:date: 2011-02-17 + +Article 3