From af3c79245098178d63d273423a934a5ba0052ae8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9gory=20Soutad=C3=A9?= Date: Wed, 15 Aug 2012 11:52:13 +0200 Subject: [PATCH] Rewrite code for navigation computation --- generators/archive.py | 2 +- generators/category.py | 2 +- generators/generator.py | 5 +++++ generators/index.py | 18 ++++++++---------- 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/generators/archive.py b/generators/archive.py index eba5ce3..6f65cbe 100644 --- a/generators/archive.py +++ b/generators/archive.py @@ -26,7 +26,7 @@ class Archive(Index): self.cur_article = 0 if len(articles) > self.articles_per_page: - self.nb_pages = len(articles) / self.articles_per_page + self.nb_pages = self.computeNbPages(len(articles), self.articles_per_page) self.dirname = '/archive/' + str(self.cur_year) diff --git a/generators/category.py b/generators/category.py index aae2d88..a7e25c0 100644 --- a/generators/category.py +++ b/generators/category.py @@ -63,7 +63,7 @@ class Category(Index): self.dirname = '/category/' + category.name_slug if articles.count() > self.articles_per_page: - self.nb_pages = articles.count() / self.articles_per_page + self.nb_pages = self.computeNbPages(articles.count(), self.articles_per_page) if not os.path.exists(output + self.dirname): os.mkdir(output + self.dirname) diff --git a/generators/generator.py b/generators/generator.py index fabd228..65da4b7 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -1,6 +1,7 @@ import os import hashlib import gzip +import math from xml.dom import * from xml.dom.minidom import parse from xml.parsers.expat import * @@ -33,6 +34,10 @@ class DynastieGenerator: def generate(self, blog, src, output): return + def computeNbPages(self, nb_articles, nb_articles_per_page): + res = math.ceil((nb_articles*1.0)/(nb_articles_per_page*1.0)) + return int(res) + def removeCDATA(self, content): content = content.replace('
')
         content = content.replace(']]>
', '') diff --git a/generators/index.py b/generators/index.py index c5f9372..52a5058 100644 --- a/generators/index.py +++ b/generators/index.py @@ -35,17 +35,14 @@ class Index(DynastieGenerator): else: nav = nav + href + str(self.cur_page-1) + '.html">< Prev ' - start = int(self.cur_page/self.articles_per_page) + start = self.cur_page-5 + if start < 0: + start = 0 end = start + 10 - if end > self.nb_pages+1: + if end > self.nb_pages: end = self.nb_pages - if (end-start) < 10: - start = end - 10 - if start < 0: - start = 0 - for i in range(start, end): if i == self.cur_page: nav = nav + str(i+1) + ' ' @@ -55,9 +52,9 @@ class Index(DynastieGenerator): else: nav = nav + href + str(i) + '.html">' + str(i+1) + ' ' - if self.cur_page != self.nb_pages: + if self.cur_page < self.nb_pages-1: nav = nav + href + str(self.cur_page+1) + '.html">Next > ' - nav = nav + href + str(self.nb_pages) + '.html">Last >>' + nav = nav + href + str(self.nb_pages-1) + '.html">Last >>' new_dom = parseString('') new_node = new_dom.getElementsByTagName('div')[0] @@ -175,7 +172,8 @@ class Index(DynastieGenerator): articles = Article.objects.all()[:self.articles_per_page] if articles.count() > self.articles_per_page: - self.nb_pages = articles.count() / self.articles_per_page + self.nb_pages = self.computeNbPages(articles.count(), self.articles_per_page) + filename = 'index.html' while self.cur_page <= self.nb_pages: #print 'Generate ' + filename