Add AllPosts generator
This commit is contained in:
parent
fa4dbf48fe
commit
7fba150877
|
@ -18,4 +18,4 @@
|
||||||
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
__all__ = ["generator", "index", "post", "category", "tag", "archive", "rss", "atom"]
|
__all__ = ["generator", "index", "post", "category", "tag", "archive", "rss", "atom", "all_posts"]
|
||||||
|
|
180
generators/all_posts.py
Normal file
180
generators/all_posts.py
Normal file
|
@ -0,0 +1,180 @@
|
||||||
|
# -*- coding: utf-8 -*-
|
||||||
|
"""
|
||||||
|
Copyright 2012-2013 Grégory Soutadé
|
||||||
|
|
||||||
|
This file is part of Dynastie.
|
||||||
|
|
||||||
|
Dynastie is free software: you can redistribute it and/or modify
|
||||||
|
it under the terms of the GNU General Public License as published by
|
||||||
|
the Free Software Foundation, either version 3 of the License, or
|
||||||
|
(at your option) any later version.
|
||||||
|
|
||||||
|
Dynastie is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
GNU General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU General Public License
|
||||||
|
along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
"""
|
||||||
|
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 AllPosts(Index):
|
||||||
|
|
||||||
|
filename = 'all_posts'
|
||||||
|
cur_post = 0
|
||||||
|
|
||||||
|
def createPosts(self, posts, dom, root, node):
|
||||||
|
posts_elem = self.createElement(dom, 'posts')
|
||||||
|
|
||||||
|
self.cur_post = 0
|
||||||
|
for p in posts:
|
||||||
|
self.cur_post_obj = p
|
||||||
|
|
||||||
|
post_elem = self.createElement(dom, 'post', '', subtree=node)
|
||||||
|
|
||||||
|
self._parse(self.hooks, posts, dom, post_elem)
|
||||||
|
posts_elem.appendChild(post_elem)
|
||||||
|
self.cur_post = self.cur_post + 1
|
||||||
|
|
||||||
|
root.replaceChild(posts_elem, node)
|
||||||
|
|
||||||
|
|
||||||
|
def createMonth(self, posts, dom, root, node):
|
||||||
|
date_format = node.getAttribute('format')
|
||||||
|
if date_format == '':
|
||||||
|
date_format = '%B'
|
||||||
|
|
||||||
|
self.cur_month = posts[0].creation_date.month
|
||||||
|
|
||||||
|
root.removeChild(node)
|
||||||
|
cur_posts = []
|
||||||
|
month_elem = None
|
||||||
|
prev_month = None
|
||||||
|
for p in posts:
|
||||||
|
if p.creation_date.month == self.cur_month:
|
||||||
|
cur_posts.append(p)
|
||||||
|
continue
|
||||||
|
|
||||||
|
month_elem = self.createElement(dom, 'month')
|
||||||
|
month_def = dom.createElement('month')
|
||||||
|
month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
|
||||||
|
month_elem.appendChild(month_def)
|
||||||
|
self.cloneSubtree(month_elem, node)
|
||||||
|
|
||||||
|
# Parse inner HTML
|
||||||
|
self._parse(self.hooks, cur_posts, dom, month_elem)
|
||||||
|
|
||||||
|
if not root.hasChildNodes():
|
||||||
|
root.appendChild(month_elem)
|
||||||
|
else:
|
||||||
|
root.insertBefore(month_elem, prev_month)
|
||||||
|
prev_month = month_elem
|
||||||
|
cur_posts = []
|
||||||
|
self.cur_month = p.creation_date.month
|
||||||
|
cur_posts.append(p)
|
||||||
|
|
||||||
|
# Last month
|
||||||
|
if not month_elem is None and len(cur_posts) != 0:
|
||||||
|
month_elem = self.createElement(dom, 'month')
|
||||||
|
month_def = dom.createElement('month')
|
||||||
|
month_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
|
||||||
|
month_elem.appendChild(month_def)
|
||||||
|
self.cloneSubtree(month_elem, node)
|
||||||
|
|
||||||
|
# Parse inner HTML
|
||||||
|
self._parse(self.hooks, cur_posts, dom, month_elem)
|
||||||
|
if not root.hasChildNodes():
|
||||||
|
root.appendChild(month_elem)
|
||||||
|
else:
|
||||||
|
root.insertBefore(month_elem, prev_month)
|
||||||
|
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def createYear(self, posts, dom, root, node):
|
||||||
|
date_format = node.getAttribute('format')
|
||||||
|
if date_format == '':
|
||||||
|
date_format = '%Y'
|
||||||
|
|
||||||
|
self.cur_year = posts[0].creation_date.year
|
||||||
|
|
||||||
|
root.removeChild(node)
|
||||||
|
cur_posts = []
|
||||||
|
year_elem = None
|
||||||
|
for p in posts:
|
||||||
|
if p.creation_date.year == self.cur_year:
|
||||||
|
cur_posts.append(p)
|
||||||
|
continue
|
||||||
|
|
||||||
|
year_elem = self.createElement(dom, 'year')
|
||||||
|
year_def = dom.createElement('year')
|
||||||
|
year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
|
||||||
|
year_elem.appendChild(year_def)
|
||||||
|
self.cloneSubtree(year_elem, node)
|
||||||
|
|
||||||
|
# Parse inner HTML
|
||||||
|
self._parse(self.hooks, cur_posts, dom, year_elem)
|
||||||
|
|
||||||
|
root.appendChild(year_elem)
|
||||||
|
cur_posts = []
|
||||||
|
self.cur_year = p.creation_date.year
|
||||||
|
cur_posts.append(p)
|
||||||
|
|
||||||
|
# Last year
|
||||||
|
if not year_elem is None and len(cur_posts) != 0:
|
||||||
|
year_elem = self.createElement(dom, 'year')
|
||||||
|
year_def = dom.createElement('year')
|
||||||
|
year_def.appendChild(dom.createTextNode(cur_posts[0].creation_date.strftime(date_format)))
|
||||||
|
year_elem.appendChild(year_def)
|
||||||
|
self.cloneSubtree(year_elem, node)
|
||||||
|
|
||||||
|
# Parse inner HTML
|
||||||
|
self._parse(self.hooks, cur_posts, dom, year_elem)
|
||||||
|
root.appendChild(year_elem)
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
def createAllPosts(self, posts, dom, root, node):
|
||||||
|
posts_elem = self.createElement(dom, 'all_posts')
|
||||||
|
|
||||||
|
if len(posts) == 0:
|
||||||
|
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
|
||||||
|
posts_elem.appendChild(post_elem)
|
||||||
|
else:
|
||||||
|
for cnode in node.childNodes:
|
||||||
|
new_node = cnode.cloneNode(True)
|
||||||
|
posts_elem.appendChild(new_node)
|
||||||
|
|
||||||
|
self._parse(self.hooks, posts, dom, posts_elem)
|
||||||
|
|
||||||
|
root.replaceChild(posts_elem, node)
|
||||||
|
return posts_elem
|
||||||
|
|
||||||
|
def generate(self, blog, src, output):
|
||||||
|
from dynastie.models import Post, Blog, Category
|
||||||
|
|
||||||
|
self.hooks['year'] = self.createYear
|
||||||
|
self.hooks['month'] = self.createMonth
|
||||||
|
self.hooks['posts'] = self.createPosts
|
||||||
|
self.hooks['all_posts'] = self.createAllPosts
|
||||||
|
|
||||||
|
dom = self.parseTemplate(blog, src, output, 'all_posts')
|
||||||
|
if dom is None: return self.report
|
||||||
|
|
||||||
|
posts = Post.objects.filter(published=True).order_by('-creation_date')
|
||||||
|
nodes = dom.getElementsByTagName("*")
|
||||||
|
|
||||||
|
nodes[0] = self.parse(src, self.hooks, posts, dom, nodes[0])
|
||||||
|
|
||||||
|
self.writeIfNotTheSame(output + "/" + self.filename + '.html', nodes[0])
|
||||||
|
|
||||||
|
if not self.somethingWrote:
|
||||||
|
self.addReport('Nothing changed')
|
||||||
|
|
||||||
|
return self.report
|
||||||
|
|
|
@ -132,12 +132,18 @@ class DynastieGenerator:
|
||||||
|
|
||||||
return link_elem
|
return link_elem
|
||||||
|
|
||||||
def createElement(self, dom, name='', content=''):
|
def cloneSubtree(self, div, subtree):
|
||||||
|
for node in subtree.childNodes:
|
||||||
|
div.appendChild(node.cloneNode(True))
|
||||||
|
|
||||||
|
def createElement(self, dom, name='', content='', subtree=None):
|
||||||
div = dom.createElement('div')
|
div = dom.createElement('div')
|
||||||
if name != '':
|
if name != '':
|
||||||
div.setAttribute('class', name)
|
div.setAttribute('class', name)
|
||||||
if content != '':
|
if content != '':
|
||||||
div.appendChild(dom.createTextNode(content))
|
div.appendChild(dom.createTextNode(content))
|
||||||
|
if not subtree is None:
|
||||||
|
self.cloneSubtree(div, subtree)
|
||||||
|
|
||||||
return div
|
return div
|
||||||
|
|
||||||
|
|
|
@ -94,6 +94,7 @@ class Blog(models.Model):
|
||||||
self.engines.append(globals()['archive'])
|
self.engines.append(globals()['archive'])
|
||||||
self.engines.append(globals()['atom'])
|
self.engines.append(globals()['atom'])
|
||||||
self.engines.append(globals()['rss'])
|
self.engines.append(globals()['rss'])
|
||||||
|
self.engines.append(globals()['all_posts'])
|
||||||
|
|
||||||
def copytree(self, src, dst):
|
def copytree(self, src, dst):
|
||||||
names = os.listdir(src)
|
names = os.listdir(src)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user