Rewrite code for navigation computation

This commit is contained in:
Grégory Soutadé 2012-08-15 11:52:13 +02:00
parent 6fb23e62fb
commit af3c792450
4 changed files with 15 additions and 12 deletions

View File

@ -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)

View File

@ -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)

View File

@ -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('<pre><![CDATA[', '<pre>')
content = content.replace(']]></pre>', '</pre>')

View File

@ -35,17 +35,14 @@ class Index(DynastieGenerator):
else:
nav = nav + href + str(self.cur_page-1) + '.html">&lt; Prev</a> '
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) + '</a> '
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 &gt;</a> '
nav = nav + href + str(self.nb_pages) + '.html">Last &gt;&gt;</a>'
nav = nav + href + str(self.nb_pages-1) + '.html">Last &gt;&gt;</a>'
new_dom = parseString('<div class="navigation">' + nav + '</div>')
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