2012-07-20 21:54:43 +02:00
|
|
|
import os
|
2012-07-18 11:30:54 +02:00
|
|
|
import datetime
|
2012-08-14 21:26:48 +02:00
|
|
|
import xml
|
2012-08-05 13:36:55 +02:00
|
|
|
from xml.parsers.expat import *
|
2012-07-20 21:54:43 +02:00
|
|
|
from xml.dom.minidom import parse, parseString
|
2012-07-15 18:21:26 +02:00
|
|
|
from dynastie.generators.generator import DynastieGenerator
|
|
|
|
from django.db import models
|
|
|
|
|
|
|
|
class Index(DynastieGenerator):
|
|
|
|
|
2012-07-18 11:30:54 +02:00
|
|
|
cur_page = 0
|
2012-07-20 21:54:43 +02:00
|
|
|
nb_pages = 0
|
2012-08-28 09:09:14 +02:00
|
|
|
cur_post = 0
|
|
|
|
posts_per_page = 0
|
2012-07-22 19:07:02 +02:00
|
|
|
filename = 'index'
|
2012-08-14 21:26:48 +02:00
|
|
|
dirname = ''
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createNavigation(self, posts, dom, root, node):
|
2012-07-20 21:54:43 +02:00
|
|
|
if self.nb_pages == 0 or self.nb_pages == 1:
|
2012-10-16 22:06:33 +02:00
|
|
|
return None
|
2012-07-20 21:54:43 +02:00
|
|
|
|
2012-08-14 21:26:48 +02:00
|
|
|
if self.dirname != '':
|
|
|
|
if self.dirname.startswith('/'):
|
|
|
|
href = '<a href="' + self.dirname + '/' + self.filename
|
|
|
|
else:
|
|
|
|
href = '<a href="/' + self.dirname + '/' + self.filename
|
|
|
|
else:
|
|
|
|
href = '<a href="/' + self.filename
|
|
|
|
|
2012-07-20 21:54:43 +02:00
|
|
|
nav = ''
|
|
|
|
if self.cur_page != 0:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + '.html"><< First</a> '
|
2012-07-20 21:54:43 +02:00
|
|
|
if self.cur_page == 1:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + '.html">< Prev</a> '
|
2012-07-20 21:54:43 +02:00
|
|
|
else:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + str(self.cur_page-1) + '.html">< Prev</a> '
|
2012-07-20 21:54:43 +02:00
|
|
|
|
2012-08-15 11:52:13 +02:00
|
|
|
start = self.cur_page-5
|
|
|
|
if start < 0:
|
|
|
|
start = 0
|
2012-07-20 21:54:43 +02:00
|
|
|
end = start + 10
|
|
|
|
|
2012-08-15 11:52:13 +02:00
|
|
|
if end > self.nb_pages:
|
2012-08-14 21:26:48 +02:00
|
|
|
end = self.nb_pages
|
2012-07-20 21:54:43 +02:00
|
|
|
|
|
|
|
for i in range(start, end):
|
|
|
|
if i == self.cur_page:
|
|
|
|
nav = nav + str(i+1) + ' '
|
|
|
|
else:
|
|
|
|
if i == 0:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + '.html">1</a> '
|
2012-07-20 21:54:43 +02:00
|
|
|
else:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + str(i) + '.html">' + str(i+1) + '</a> '
|
2012-07-20 21:54:43 +02:00
|
|
|
|
2012-08-15 11:52:13 +02:00
|
|
|
if self.cur_page < self.nb_pages-1:
|
2012-08-14 21:26:48 +02:00
|
|
|
nav = nav + href + str(self.cur_page+1) + '.html">Next ></a> '
|
2012-08-15 11:52:13 +02:00
|
|
|
nav = nav + href + str(self.nb_pages-1) + '.html">Last >></a>'
|
2012-07-20 21:54:43 +02:00
|
|
|
|
2012-07-22 19:07:02 +02:00
|
|
|
new_dom = parseString('<div class="navigation">' + nav + '</div>')
|
|
|
|
new_node = new_dom.getElementsByTagName('div')[0]
|
2012-10-16 22:06:33 +02:00
|
|
|
res = new_node.cloneNode(True)
|
|
|
|
root.replaceChild(res, node)
|
|
|
|
|
|
|
|
return res
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createPost(self, post, dom, post_elem, root):
|
2012-07-18 11:30:54 +02:00
|
|
|
values = {}
|
2012-08-28 09:09:14 +02:00
|
|
|
values['title'] = self.createLinkElem(dom, post.getPath(), post.title)
|
|
|
|
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
2012-10-11 22:26:47 +02:00
|
|
|
values['date'] = post.creation_date.strftime('%A, %d %B %Y %H:%m')
|
2012-08-28 09:09:14 +02:00
|
|
|
values['post_content'] = ''
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
blog = post.blog
|
2012-08-05 13:36:55 +02:00
|
|
|
blog.create_paths()
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
filename = blog.src_path + '/_post/' + str(post.id)
|
2012-08-05 13:36:55 +02:00
|
|
|
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
self.addError('File does not exists ' + filename)
|
|
|
|
return
|
|
|
|
|
|
|
|
f = open(filename, 'rb')
|
2012-08-28 09:09:14 +02:00
|
|
|
post_content = f.read()
|
2012-08-05 13:36:55 +02:00
|
|
|
f.close()
|
2012-08-14 21:26:48 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
self.simpleTransform(values, dom, post_elem, root)
|
2012-10-11 22:26:47 +02:00
|
|
|
|
|
|
|
content_nodes = post_elem.getElementsByTagName('div')
|
|
|
|
post_transform = ['post_content']
|
2012-08-05 13:36:55 +02:00
|
|
|
for content_node in content_nodes:
|
|
|
|
the_class = content_node.getAttribute('class')
|
|
|
|
if not the_class in post_transform:
|
|
|
|
continue
|
2012-10-11 22:26:47 +02:00
|
|
|
new_node = dom.createTextNode(post_content)
|
|
|
|
content_node.appendChild(new_node)
|
|
|
|
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createPosts(self, posts, dom, root, node):
|
|
|
|
posts_elem = self.createElement(dom, 'posts')
|
|
|
|
for i in range(0, self.posts_per_page):
|
|
|
|
post_elem = self.createElement(dom, 'post')
|
|
|
|
if len(posts) > self.cur_post:
|
|
|
|
self.createPost(posts[self.cur_post], dom, post_elem, node)
|
2012-07-22 19:07:02 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
|
|
|
|
posts_elem.appendChild(post_elem)
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
self.cur_post = self.cur_post + 1
|
|
|
|
if self.cur_post == len(posts):
|
2012-07-22 19:07:02 +02:00
|
|
|
break
|
2012-08-28 09:09:14 +02:00
|
|
|
root.replaceChild(posts_elem, node)
|
2012-10-16 22:06:33 +02:00
|
|
|
return posts_elem
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createRecents(self, posts, dom, root, node):
|
|
|
|
if self.cur_post == len(posts):
|
2012-08-15 18:11:55 +02:00
|
|
|
root.removeChild(node)
|
|
|
|
return
|
|
|
|
|
2012-08-01 22:04:41 +02:00
|
|
|
if node.hasAttribute("limit"):
|
|
|
|
nb_recents = int(node.getAttribute("limit"))
|
|
|
|
else:
|
|
|
|
nb_recents = 5
|
2012-08-05 13:36:55 +02:00
|
|
|
recents_elem = self.createElement(dom, 'recents')
|
2012-08-14 21:26:48 +02:00
|
|
|
for child in node.childNodes:
|
|
|
|
recents_elem.appendChild(child.cloneNode(True))
|
2012-08-01 22:04:41 +02:00
|
|
|
list_elem = dom.createElement('ul')
|
|
|
|
for i in range(0, nb_recents):
|
2012-08-28 09:09:14 +02:00
|
|
|
post_elem = dom.createElement('li')
|
|
|
|
if self.cur_post+i < len(posts):
|
|
|
|
post = posts[self.cur_post+i]
|
|
|
|
link_elem = self.createLinkElem(dom, post.getPath(), post.title)
|
|
|
|
post_elem.appendChild(link_elem)
|
2012-08-01 22:04:41 +02:00
|
|
|
else:
|
|
|
|
break
|
2012-08-28 09:09:14 +02:00
|
|
|
list_elem.appendChild(post_elem)
|
2012-08-01 22:04:41 +02:00
|
|
|
|
2012-08-05 13:36:55 +02:00
|
|
|
recents_elem.appendChild(list_elem)
|
|
|
|
root.replaceChild(recents_elem, node)
|
2012-10-16 22:06:33 +02:00
|
|
|
|
|
|
|
return recents_elem
|
2012-08-01 22:04:41 +02:00
|
|
|
|
2012-07-15 18:21:26 +02:00
|
|
|
def generate(self, blog, src, output):
|
2012-08-28 09:09:14 +02:00
|
|
|
from dynastie.models import Post, Blog
|
2012-07-15 18:21:26 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
hooks = {'posts' : self.createPosts,
|
2012-08-01 22:04:41 +02:00
|
|
|
'navigation' : self.createNavigation,
|
|
|
|
'recents' : self.createRecents}
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-07-18 11:30:54 +02:00
|
|
|
if not os.path.exists(src + '/_index.html'):
|
|
|
|
self.addError('No _index.html found, exiting')
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
try:
|
|
|
|
dom = parse(src + '/_index.html')
|
|
|
|
except xml.dom.DOMException as e:
|
|
|
|
self.addError('Error parsing _index.html : ' + e)
|
|
|
|
return self.report
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
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-18 11:30:54 +02:00
|
|
|
else:
|
2012-08-28 09:09:14 +02:00
|
|
|
self.addError('No tag dyn:posts found')
|
2012-07-18 11:30:54 +02:00
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
cur_year = now.year
|
2012-08-28 09:09:14 +02:00
|
|
|
posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date')
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
if posts.count() < self.posts_per_page:
|
|
|
|
posts = Post.objects.all()[:self.posts_per_page]
|
2012-07-18 11:30:54 +02:00
|
|
|
|
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-08-15 11:52:13 +02:00
|
|
|
|
2012-07-18 11:30:54 +02:00
|
|
|
filename = 'index.html'
|
2012-07-20 21:54:43 +02:00
|
|
|
while self.cur_page <= self.nb_pages:
|
|
|
|
#print 'Generate ' + filename
|
2012-07-18 11:30:54 +02:00
|
|
|
nodes = dom.getElementsByTagName("*")
|
2012-08-28 09:09:14 +02:00
|
|
|
nodes[0] = self.parse(src, hooks, posts, dom, nodes[0])
|
2012-08-15 18:11:55 +02:00
|
|
|
self.writeIfNotTheSame(output + '/' + filename, nodes[0])
|
2012-07-18 11:30:54 +02:00
|
|
|
self.cur_page = self.cur_page + 1
|
|
|
|
filename = 'index' + str(self.cur_page) + '.html'
|
2012-07-20 21:54:43 +02:00
|
|
|
dom = parse(src + '/_index.html')
|
|
|
|
|
|
|
|
filename = output + '/' + filename
|
2012-07-18 11:30:54 +02:00
|
|
|
|
|
|
|
while os.path.exists(filename):
|
|
|
|
self.addReport('Removing unused ' + filename)
|
|
|
|
os.unlink(filename)
|
2012-07-18 11:46:31 +02:00
|
|
|
filename = filename + '.gz'
|
|
|
|
if os.path.exists(filename):
|
|
|
|
self.addReport('Removing unused ' + filename)
|
|
|
|
os.unlink(filename)
|
2012-07-18 11:30:54 +02:00
|
|
|
self.cur_page = self.cur_page + 1
|
2012-07-20 21:54:43 +02:00
|
|
|
filename = output + '/index' + str(self.cur_page) + '.html'
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-07-22 10:47:24 +02:00
|
|
|
if not self.somethingWrote:
|
|
|
|
self.addReport('Nothing changed')
|
|
|
|
|
2012-07-18 11:30:54 +02:00
|
|
|
return self.report
|