import os import datetime from xml.dom.minidom import parse, parseString from dynastie.generators.generator import DynastieGenerator from django.db import models class Index(DynastieGenerator): cur_page = 0 nb_pages = 0 cur_article = 0 articles_per_page = 0 filename = 'index' def createNavigation(self, articles, dom, root, node): if self.nb_pages == 0 or self.nb_pages == 1: return '' nav = '' if self.cur_page != 0: nav = nav + '<< ' if self.cur_page == 1: nav = nav + '< ' else: nav = nav + '< ' start = (self.cur_page/5)*5 end = start + 10 if end > self.nb_pages+1: end = self.nb_pages+1 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) + ' ' else: if i == 0: nav = nav + '1 ' else: nav = nav + '' + str(i+1) + ' ' if self.cur_page != self.nb_pages: nav = nav + '> ' nav = nav + '>>' new_dom = parseString('') new_node = new_dom.getElementsByTagName('div')[0] root.replaceChild(new_node.cloneNode(True), node) def createArticle(self, article, dom, article_elem, root): values = {} values['title'] = article.title values['author'] = article.author.first_name values['date'] = article.creation_date.strftime("%d/%m/%Y") values['content'] = '' self.simpleTransform(values, dom, article_elem, root) def createArticles(self, articles, dom, root, node): articles_elem = self.createElement(dom, 'articles') for i in range(0, self.articles_per_page): article_elem = self.createElement(dom, 'article') if len(articles) > self.cur_article: self.createArticle(articles[self.cur_article], dom, article_elem, node) else: article_elem = self.createElement(dom, '', 'No articles yet') articles_elem.appendChild(article_elem) self.cur_article = self.cur_article + 1 if self.cur_article == len(articles): break root.replaceChild(articles_elem, node) def createRecents(self, articles, dom, root, node): if node.hasAttribute("limit"): nb_recents = int(node.getAttribute("limit")) else: nb_recents = 5 list_elem = dom.createElement('ul') for i in range(0, nb_recents): article_elem = dom.createElement('li') if self.cur_article+i < len(articles): link_elem = dom.createElement('a') link_elem.setAttribute('href', articles[self.cur_article+i].getPath()) text_elem = dom.createTextNode(articles[self.cur_article+i].title) link_elem.appendChild(text_elem) article_elem.appendChild(link_elem) else: break list_elem.appendChild(article_elem) root.replaceChild(list_elem, node) def generate(self, blog, src, output): from dynastie.models import Article, Blog hooks = {'articles' : self.createArticles, 'navigation' : self.createNavigation, 'recents' : self.createRecents} if not os.path.exists(src + '/_index.html'): self.addError('No _index.html found, exiting') return self.report try: dom = parse(src + '/_index.html') except xml.dom.DOMException as e: self.addError('Error parsing _index.html : ' + e) return self.report article_nodes = dom.getElementsByTagNameNS(self.URI, "articles") if not article_nodes is None: if article_nodes[0].hasAttribute("limit"): self.articles_per_page = int(article_nodes[0].getAttribute("limit")) else: self.articles_per_page = 5 else: self.addError('No tag dyn:articles found') now = datetime.datetime.now() cur_year = now.year articles = Article.objects.filter(creation_date__year=cur_year).order_by('-creation_date') if articles.count() < self.articles_per_page: articles = Article.objects.all()[:self.articles_per_page] if articles.count() > self.articles_per_page: self.nb_pages = articles.count() / self.articles_per_page filename = 'index.html' while self.cur_page <= self.nb_pages: #print 'Generate ' + filename nodes = dom.getElementsByTagName("*") self.parse(hooks, articles, dom, nodes[0]) self.writeIfNotTheSame(output + '/' + filename, nodes[0].toxml('utf8')) self.cur_page = self.cur_page + 1 filename = 'index' + str(self.cur_page) + '.html' dom = parse(src + '/_index.html') filename = output + '/' + filename while os.path.exists(filename): self.addReport('Removing unused ' + filename) os.unlink(filename) filename = filename + '.gz' if os.path.exists(filename): self.addReport('Removing unused ' + filename) os.unlink(filename) self.cur_page = self.cur_page + 1 filename = output + '/index' + str(self.cur_page) + '.html' if not self.somethingWrote: self.addReport('Nothing changed') return self.report