Dynastie/generators/article.py
Grégory Soutadé 47d68daec0 Add category generator
Add name_slug to category
Do a more generic parse function
2012-07-22 19:07:02 +02:00

68 lines
2.3 KiB
Python

import os
import datetime
from xml.dom.minidom import parse, parseString
from dynastie.generators.generator import DynastieGenerator
from django.db import models
# TODO : content
class Article(DynastieGenerator):
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 parse(self, article, dom, root):
for node in root.childNodes:
if node.prefix == 'dyn':
if node.localName == 'content':
article_elem = self.createElement(dom, 'article')
self.createArticle(article, dom, article_elem, node)
root.replaceChild(article_elem, node)
continue
if node.hasChildNodes():
self.parse(article, dom, node)
return
def generate(self, blog, src, output):
from dynastie.models import Article, Blog
if not os.path.exists(src + '/_article.html'):
self.addError('No _article.html found, exiting')
return self.report
try:
dom = parse(src + '/_article.html')
except xml.dom.DOMException as e:
self.addError('Error parsing _article.html : ' + e)
return self.report
if not os.path.exists(output + '/article'):
os.mkdir(output + '/article')
articles = Article.objects.all()
for article in articles:
#print 'Generate ' + filename
nodes = dom.getElementsByTagName("*")
self.parse(article, dom, nodes[0])
filename = output + '/article/'
filename = filename + article.creation_date.strftime("%Y") + '/' + article.creation_date.strftime("%m") + '/'
if not os.path.exists(filename):
os.makedirs(filename)
filename = filename + article.title_slug + '.html'
self.writeIfNotTheSame(filename, nodes[0].toxml('utf8'))
dom = parse(src + '/_article.html')
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report