Rewrite code for navigation computation
This commit is contained in:
parent
6fb23e62fb
commit
af3c792450
|
@ -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)
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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>')
|
||||
|
|
|
@ -35,17 +35,14 @@ class Index(DynastieGenerator):
|
|||
else:
|
||||
nav = nav + href + str(self.cur_page-1) + '.html">< 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 ></a> '
|
||||
nav = nav + href + str(self.nb_pages) + '.html">Last >></a>'
|
||||
nav = nav + href + str(self.nb_pages-1) + '.html">Last >></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
|
||||
|
|
Loading…
Reference in New Issue
Block a user