diff --git a/forms.py b/forms.py index 63ce7ac..292c3f6 100644 --- a/forms.py +++ b/forms.py @@ -17,7 +17,7 @@ class ArticleForm(ModelForm): class CategoryForm(ModelForm): class Meta: model = Category - exclude = ('parent') + exclude = ('parent', 'name_slug') class UserForm(ModelForm): class Meta: diff --git a/generators/__init__.py b/generators/__init__.py index 8b4c9af..625fb79 100644 --- a/generators/__init__.py +++ b/generators/__init__.py @@ -1 +1 @@ -__all__ = ["generator", "index", "article"] +__all__ = ["generator", "index", "article", "category"] diff --git a/generators/article.py b/generators/article.py index caf71e9..0576e0c 100644 --- a/generators/article.py +++ b/generators/article.py @@ -43,8 +43,8 @@ class Article(DynastieGenerator): self.addError('Error parsing _article.html : ' + e) return self.report - if not os.path.exists(output + '/articles'): - os.mkdir(output + '/articles') + if not os.path.exists(output + '/article'): + os.mkdir(output + '/article') articles = Article.objects.all() @@ -52,7 +52,7 @@ class Article(DynastieGenerator): #print 'Generate ' + filename nodes = dom.getElementsByTagName("*") self.parse(article, dom, nodes[0]) - filename = output + '/articles/' + filename = output + '/article/' filename = filename + article.creation_date.strftime("%Y") + '/' + article.creation_date.strftime("%m") + '/' if not os.path.exists(filename): os.makedirs(filename) diff --git a/generators/category.py b/generators/category.py new file mode 100644 index 0000000..1614fa0 --- /dev/null +++ b/generators/category.py @@ -0,0 +1,85 @@ +import os +import datetime +from xml.dom.minidom import parse, parseString +from dynastie.generators.generator import DynastieGenerator +from dynastie.generators.index import Index +from django.db import models + +class Category(Index): + + cur_page = 0 + nb_pages = 0 + cur_article = 0 + articles_per_page = 0 + filename = 'category' + cur_category = None + + def createCategory(self, articles, dom, root, node): + if node.hasAttribute('name'): + self.replaceByText(dom, root, node, self.cur_category.name) + if node.hasAttribute('description'): + self.replaceByText(dom, root, node, self.cur_category.description) + + def generate(self, blog, src, output): + from dynastie.models import Article, Blog, Category + + hooks = {'articles' : self.createArticles, + 'navigation' : self.createNavigation, + 'category' : self.createCategory} + + if not os.path.exists(src + '/_category.html'): + self.addError('No _category.html found, exiting') + return self.report + + try: + dom = parse(src + '/_category.html') + except xml.dom.DOMException as e: + self.addError('Error parsing _category.html : ' + e) + return self.report + + if not os.path.exists(output + '/category'): + os.mkdir(output + '/category') + + article_nodes = dom.getElementsByTagNameNS(self.URI, "articles") + if not article_nodes is None: + self.articles_per_page = int(article_nodes[0].getAttribute("limit")) + else: + self.addError('No tag dyn:articles found') + + categories = Category.objects.all() + + for category in categories: + self.cur_category = category + self.filename = category.name_slug + filename = self.filename + '.html' + articles = Article.objects.filter(category__exact=category) + + self.nb_pages = 0 + self.cur_page = 0 + if articles.count() > self.articles_per_page: + self.nb_pages = articles.count() / self.articles_per_page + + while self.cur_page <= self.nb_pages: + #print 'Generate ' + filename + nodes = dom.getElementsByTagName("*") + self.parse(hooks, articles, dom, nodes[0]) + self.writeIfNotTheSame(output + '/category/' + filename, nodes[0].toxml('utf8')) + self.cur_page = self.cur_page + 1 + filename = self.filename + str(self.cur_page) + '.html' + dom = parse(src + '/_category.html') + + 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 + '/category/' + self.filename + str(self.cur_page) + '.html' + + if not self.somethingWrote: + self.addReport('Nothing changed') + + return self.report + diff --git a/generators/generator.py b/generators/generator.py index 8ba551d..4534492 100644 --- a/generators/generator.py +++ b/generators/generator.py @@ -82,6 +82,17 @@ class DynastieGenerator: new_elem = node.cloneNode(False) self.simpleTransform(values, dom, new_elem, node) elem.appendChild(new_elem) + + def replaceByText(self, dom, root, node, content): + new_node = dom.createTextNode(content) + root.replaceChild(new_node, node) - - + def parse(self, hooks, articles, dom, root): + for node in root.childNodes: + if node.prefix == 'dyn': + if node.localName in hooks: + hooks[node.localName](articles, dom, root, node) + + if node.hasChildNodes(): + self.parse(hooks, articles, dom, node) + return diff --git a/generators/index.py b/generators/index.py index 9d5de23..ae74fa7 100644 --- a/generators/index.py +++ b/generators/index.py @@ -4,26 +4,25 @@ from xml.dom.minidom import parse, parseString from dynastie.generators.generator import DynastieGenerator from django.db import models -# TODO : content - class Index(DynastieGenerator): cur_page = 0 nb_pages = 0 cur_article = 0 articles_per_page = 0 + filename = 'index' - def createNavigation(self): + 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 + '<< ' + nav = nav + '<< ' if self.cur_page == 1: - nav = nav + '< ' + nav = nav + '< ' else: - nav = nav + '< ' + nav = nav + '< ' start = (self.cur_page/5)*5 end = start + 10 @@ -36,15 +35,17 @@ class Index(DynastieGenerator): nav = nav + str(i+1) + ' ' else: if i == 0: - nav = nav + '1 ' + nav = nav + '1 ' else: - nav = nav + '' + str(i+1) + ' ' + nav = nav + '' + str(i+1) + ' ' if self.cur_page != self.nb_pages: - nav = nav + '> ' - nav = nav + '>>' + nav = nav + '> ' + nav = nav + '>>' - return 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 = {} @@ -55,37 +56,27 @@ class Index(DynastieGenerator): self.simpleTransform(values, dom, article_elem, root) - def parse(self, articles, dom, root): - for node in root.childNodes: - if node.prefix == 'dyn': - if node.localName == 'articles': - articles_elem = self.createElement(dom, 'articles') - for i in range(0, self.articles_per_page): - 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) + 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 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 + self.cur_article = self.cur_article + 1 + if self.cur_article == articles.count(): + break + root.replaceChild(articles_elem, node) def generate(self, blog, src, output): from dynastie.models import Article, Blog + hooks = {'articles' : self.createArticles, + 'navigation' : self.createNavigation} + if not os.path.exists(src + '/_index.html'): self.addError('No _index.html found, exiting') return self.report @@ -117,7 +108,7 @@ class Index(DynastieGenerator): while self.cur_page <= self.nb_pages: #print 'Generate ' + filename nodes = dom.getElementsByTagName("*") - self.parse(articles, dom, nodes[0]) + 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' diff --git a/init.sql b/init.sql deleted file mode 100644 index f602138..0000000 --- a/init.sql +++ /dev/null @@ -1,13 +0,0 @@ -BEGIN TRANSACTION; -CREATE TABLE user (id INTEGER PRIMARY KEY, name VARCHAR(255), login VARCHAR(255), password VARCHAR(255), email VARCHAR(255), administrator CHAR(1), editor REFERENCES editor(id)); -CREATE TABLE editor (name VARCHAR(255)); -CREATE TABLE article (id INTEGER PRIMARY KEY, title VARCHAR(255), category REFERENCES category(id) ON DELETE SET NULL, published CHAR(1), creation_date DATE, front_page CHAR(1), author REFERENCES user(id) ON DELETE SET NULL, description TEXT, keywords TEXT); -CREATE TABLE category (name VARCHAR(255) PRIMARY KEY, parent REFERENCES category(id) ON DELETE SET NULL); -CREATE TABLE tag (name VARCHAR(255)); -CREATE TABLE tags (article REFERENCES article(id), tag REFERENCES tag(id)); -CREATE TABLE comment(id INTEGER PRIMARY KEY, article REFERENCES article(id), parent REFERENCES comment(id), comment_date DATE, author VARCHAR(255), email VARCHAR(255), comment TEXT); -CREATE TABLE blog (id INTEGER PRIMARY KEY, name VARCHAR(255), title VARCHAR(255), description TEXT, keywords TEXT); -CREATE TABLE writers (blog REFERENCES blog(id), user REFERENCES user(id)); --- Administrator with empty password -INSERT INTO user ("name", "login", "password", "administrator") VALUES ("Administrator", "admin", "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "1"); -COMMIT TRANSACTION; \ No newline at end of file diff --git a/models.py b/models.py index 3123b66..a6ac0f1 100644 --- a/models.py +++ b/models.py @@ -121,9 +121,22 @@ class Editor(models.Model): class Category(models.Model): name = models.CharField(max_length=255, unique=True) + name_slug = models.CharField(max_length=255) parent = models.ForeignKey('self', blank=True, null=True) description = models.TextField(max_length=255, blank=True) + def slugify(self): + name = normalize('NFKD', self.name).encode('ascii', 'ignore').replace(' ', '-').lower() + #remove `other` characters + name = sub('[^a-zA-Z0-9_-]', '', name) + #nomalize dashes + name = sub('-+', '-', name) + self.name_slug = name + + def save(self): + self.slugify() + super(Category, self).save() + class Tag(models.Model): name = models.CharField(max_length=255, unique=True) @@ -140,7 +153,6 @@ class Article(models.Model): tags = models.ManyToManyField(Tag, blank=True, null=True) blog = models.ForeignKey(Blog) - def slugify(self): name = normalize('NFKD', self.title).encode('ascii', 'ignore').replace(' ', '-').lower() #remove `other` characters @@ -177,7 +189,7 @@ class Article(models.Model): os.unlink(filename) output = b.output_path - filename = output + '/articles/' + filename = output + '/article/' filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/' filename = filename + self.title_slug + '.html' if os.path.exists(filename): @@ -186,12 +198,12 @@ class Article(models.Model): if os.path.exists(filename): os.unlink(filename) - filename = output + '/articles/' + filename = output + '/article/' filename = filename + self.creation_date.strftime("%Y") + '/' + self.creation_date.strftime("%m") + '/' if len(os.listdir(filename)) == 0: os.rmdir(filename) - filename = output + '/articles/' + filename = output + '/article/' filename = filename + self.creation_date.strftime("%Y") + '/' if len(os.listdir(filename)) == 0: os.rmdir(filename)