Dynastie/generators/post.py

60 lines
2.0 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 Post(Index):
def _createPost(self, post, dom, post_elem, root):
self.createPost(post, dom, post_elem, root)
post_nodes = dom.getElementsByTagNameNS(self.URI, "post")
post_elem = post_nodes[0]
post_elem.parentNode.removeChild(post_elem)
title_nodes = dom.getElementsByTagName("title")
# Set title to be title's post
for node in title_nodes:
if node.hasChildNodes():
node.removeChild(node.childNodes[0])
node.appendChild(dom.createTextNode(post.title))
def generate(self, blog, src, output):
from dynastie.models import Post, Blog
hooks = {'post' : self._createPost}
if not os.path.exists(src + '/_post.html'):
self.addError('No _post.html found, exiting')
return self.report
try:
dom = parse(src + '/_post.html')
except xml.dom.DOMException as e:
self.addError('Error parsing _post.html : ' + e)
return self.report
if not os.path.exists(output + '/post'):
os.mkdir(output + '/post')
posts = Post.objects.all()
for post in posts:
#print 'Generate ' + filename
nodes = dom.getElementsByTagName("*")
nodes[0] = self.parse(src, hooks, post, dom, nodes[0])
filename = output + '/post/'
filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/'
if not os.path.exists(filename):
os.makedirs(filename)
filename = filename + post.title_slug + '.html'
self.writeIfNotTheSame(filename, nodes[0])
dom = parse(src + '/_post.html')
if not self.somethingWrote:
self.addReport('Nothing changed')
return self.report