Dynastie/generators/article.py

52 lines
1.8 KiB
Python

import os
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 Article(Index):
def _createArticle(self, article, dom, article_elem, root):
self.createArticle(article, dom, article_elem, root)
article_nodes = dom.getElementsByTagNameNS(self.URI, "article")
article_elem = article_nodes[0]
article_elem.parentNode.removeChild(article_elem)
def generate(self, blog, src, output):
from dynastie.models import Article, Blog
hooks = {'article' : self._createArticle}
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("*")
nodes[0] = self.parse(src, hooks, 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])
dom = parse(src + '/_article.html')
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report