2012-07-22 19:07:02 +02:00
|
|
|
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 Category(Index):
|
|
|
|
|
|
|
|
cur_page = 0
|
|
|
|
nb_pages = 0
|
2012-08-28 09:09:14 +02:00
|
|
|
cur_post = 0
|
|
|
|
posts_per_page = 0
|
2012-08-04 21:21:04 +02:00
|
|
|
filename = 'index'
|
2012-08-14 21:26:48 +02:00
|
|
|
dirname = ''
|
2012-07-22 19:07:02 +02:00
|
|
|
cur_category = None
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createCategory(self, posts, dom, root, node):
|
2012-07-22 19:07:02 +02:00
|
|
|
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)
|
2012-10-16 22:06:33 +02:00
|
|
|
return None
|
2012-07-22 19:07:02 +02:00
|
|
|
|
|
|
|
def generate(self, blog, src, output):
|
2012-08-28 09:09:14 +02:00
|
|
|
from dynastie.models import Post, Blog, Category
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-11-08 21:23:30 +01:00
|
|
|
self.hooks = {'posts' : self.createPosts,
|
2012-12-10 19:30:25 +01:00
|
|
|
'navigation' : self.createNavigation,
|
|
|
|
'category' : self.createCategory,
|
|
|
|
'tags' : self.createTags,
|
|
|
|
'replace' : self.createReplace}
|
|
|
|
|
|
|
|
self.blog = blog
|
2012-07-22 19:07:02 +02:00
|
|
|
|
|
|
|
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')
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
|
|
|
if not post_nodes is None:
|
|
|
|
if post_nodes[0].hasAttribute("limit"):
|
|
|
|
self.posts_per_page = int(post_nodes[0].getAttribute("limit"))
|
2012-08-01 22:04:41 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
self.posts_per_page = 5
|
2012-07-22 19:07:02 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
self.addError('No tag dyn:posts found')
|
2012-07-22 19:07:02 +02:00
|
|
|
|
|
|
|
categories = Category.objects.all()
|
|
|
|
|
|
|
|
for category in categories:
|
|
|
|
self.cur_category = category
|
|
|
|
filename = self.filename + '.html'
|
2012-08-28 09:09:14 +02:00
|
|
|
posts = Post.objects.filter(category__exact=category, published=True).order_by('-creation_date')
|
2012-07-22 19:07:02 +02:00
|
|
|
|
|
|
|
self.nb_pages = 0
|
|
|
|
self.cur_page = 0
|
2012-08-28 09:09:14 +02:00
|
|
|
self.cur_post = 0
|
2012-08-04 21:21:04 +02:00
|
|
|
|
2012-08-14 21:26:48 +02:00
|
|
|
self.dirname = '/category/' + category.name_slug
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
if posts.count() > self.posts_per_page:
|
|
|
|
self.nb_pages = self.computeNbPages(posts.count(), self.posts_per_page)
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-08-14 21:26:48 +02:00
|
|
|
if not os.path.exists(output + self.dirname):
|
|
|
|
os.mkdir(output + self.dirname)
|
|
|
|
|
2012-08-04 21:21:04 +02:00
|
|
|
|
2012-07-22 19:07:02 +02:00
|
|
|
while self.cur_page <= self.nb_pages:
|
|
|
|
#print 'Generate ' + filename
|
|
|
|
nodes = dom.getElementsByTagName("*")
|
2012-11-08 21:23:30 +01:00
|
|
|
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
2012-08-15 18:11:55 +02:00
|
|
|
self.writeIfNotTheSame(output + self.dirname + '/' + filename, nodes[0])
|
2012-07-22 19:07:02 +02:00
|
|
|
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
|
2012-08-14 21:26:48 +02:00
|
|
|
filename = output + self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
2012-07-22 19:07:02 +02:00
|
|
|
|
|
|
|
if not self.somethingWrote:
|
|
|
|
self.addReport('Nothing changed')
|
|
|
|
|
|
|
|
return self.report
|
|
|
|
|