diff --git a/generators/generator.py b/generators/generator.py index 8e049a4..57ef22d 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -61,10 +61,12 @@ class DynastieGenerator: f.write(content) f.close() - def createElement(self, dom, name, content): + def createElement(self, dom, name='', content=''): div = dom.createElement('div') - div.setAttribute('class', name) - div.appendChild(dom.createTextNode(content)) + if name != '': + div.setAttribute('class', name) + if content != '': + div.appendChild(dom.createTextNode(content)) return div @@ -75,6 +77,9 @@ class DynastieGenerator: new_elem = self.createElement(dom, node.localName, values[node.localName]) elem.appendChild(new_elem) else: - elem.appendChild(node.cloneNode(True)) + new_elem = node.cloneNode(False) + self.simpleTransform(values, dom, new_elem, node) + elem.appendChild(new_elem) + diff --git a/generators/index.py b/generators/index.py index 06fdf69..b726040 100644 --- a/generators/index.py +++ b/generators/index.py @@ -1,17 +1,51 @@ +import os import datetime -from xml.dom.minidom import parse +from xml.dom.minidom import parse, parseString from dynastie.generators.generator import DynastieGenerator from django.db import models -# TODO : links between indexes -# TODO : gzip +# TODO : content class Index(DynastieGenerator): cur_page = 0 + nb_pages = 0 cur_article = 0 articles_per_page = 0 + def createNavigation(self): + 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 + + 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 + '>>' + + return nav + def createArticle(self, article, dom, article_elem, root): values = {} values['title'] = article.title @@ -25,23 +59,32 @@ class Index(DynastieGenerator): for node in root.childNodes: if node.prefix == 'dyn': if node.localName == 'articles': - articles_elem = dom.createElement("articles") + articles_elem = self.createElement(dom, 'articles') for i in range(0, self.articles_per_page): - article_elem = dom.createElementNS(self.URI, "article") - self.createArticle(articles[self.cur_article], dom, article_elem, node) + article_elem = self.createElement(dom, 'article') + if articles.count() > 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 == articles.count(): break root.replaceChild(articles_elem, node) + + if node.localName == 'navigation': + new_dom = parseString('
') + new_node = new_dom.getElementsByTagName('div')[0] + root.replaceChild(new_node.cloneNode(True), node) + continue if node.hasChildNodes(): self.parse(articles, dom, node) return def generate(self, blog, src, output): - from dynastie.models import * + from dynastie.models import Article, Blog if not os.path.exists(src + '/_index.html'): self.addError('No _index.html found, exiting') @@ -67,13 +110,20 @@ class Index(DynastieGenerator): 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_article < articles.count(): + while self.cur_page <= self.nb_pages: + #print 'Generate ' + filename nodes = dom.getElementsByTagName("*") self.parse(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) @@ -83,7 +133,7 @@ class Index(DynastieGenerator): self.addReport('Removing unused ' + filename) os.unlink(filename) self.cur_page = self.cur_page + 1 - filename = 'index' + str(self.cur_page) + '.html' + filename = output + '/index' + str(self.cur_page) + '.html' return self.report diff --git a/models.py b/models.py index d146997..a516fcc 100644 --- a/models.py +++ b/models.py @@ -2,6 +2,7 @@ import os import shutil import hashlib import inspect +from datetime import datetime from django.db import models from django.contrib.auth.models import User from django.db.models.signals import post_init, post_delete, post_save @@ -19,6 +20,7 @@ class Blog(models.Model): src_path = '' output_path = '' + report = '' def create_paths(self): self.src_path = 'sites/' + self.name @@ -80,8 +82,10 @@ class Blog(models.Model): f.close() if src_md5.digest() != dst_md5.digest(): + self.report = self.report + 'Update ' + dstname + '