2013-02-09 08:55:06 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-02-07 18:50:54 +01:00
|
|
|
"""
|
2014-01-04 13:55:30 +01:00
|
|
|
Copyright 2012-2014 Grégory Soutadé
|
2013-02-07 18:50:54 +01:00
|
|
|
|
|
|
|
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/>.
|
|
|
|
"""
|
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 *
|
2014-03-27 18:24:58 +01:00
|
|
|
import xml.parsers.expat
|
2012-07-20 21:54:43 +02:00
|
|
|
from xml.dom.minidom import parse, parseString
|
2012-11-13 18:33:38 +01:00
|
|
|
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
2012-07-15 18:21:26 +02:00
|
|
|
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
|
2013-01-06 12:10:29 +01:00
|
|
|
cur_post_obj = None
|
2012-08-28 09:09:14 +02:00
|
|
|
posts_per_page = 0
|
2012-07-22 19:07:02 +02:00
|
|
|
filename = 'index'
|
2012-08-14 21:26:48 +02:00
|
|
|
dirname = ''
|
2012-12-10 19:30:25 +01:00
|
|
|
blog = None
|
|
|
|
|
2013-01-06 18:28:03 +01:00
|
|
|
def __init__(self, hash_posts=None, hash_posts_content=None):
|
|
|
|
DynastieGenerator.__init__(self, hash_posts, hash_posts_content)
|
2013-01-06 12:10:29 +01:00
|
|
|
|
|
|
|
self.hooks = {'posts' : self.createPosts,
|
|
|
|
'title' : self.createTitle,
|
2013-01-13 18:55:20 +01:00
|
|
|
'date' : self.createDate,
|
2013-01-06 12:10:29 +01:00
|
|
|
'navigation' : self.createNavigation,
|
2013-01-13 18:55:20 +01:00
|
|
|
'recents' : self.createRecents,
|
2013-01-06 12:10:29 +01:00
|
|
|
'tags' : self.createTags,
|
2014-06-05 19:22:13 +02:00
|
|
|
'replace' : self.createReplace,
|
2014-06-01 18:58:13 +02:00
|
|
|
'first_page_only' : self.createFirstPageOnly,
|
|
|
|
'ljdc_last' : self.createLJDCLast,
|
2014-06-05 19:56:47 +02:00
|
|
|
'comments_count' : self.createCommentsCount,
|
2014-06-01 18:58:13 +02:00
|
|
|
}
|
2013-01-06 12:10:29 +01:00
|
|
|
|
2013-01-06 17:59:46 +01:00
|
|
|
self.first_try = True
|
|
|
|
|
2012-12-10 19:30:25 +01:00
|
|
|
def createReplace(self, posts, dom, root, replace_elem):
|
|
|
|
if not replace_elem.hasAttribute('div_name'):
|
|
|
|
self.addError('No attribute div_name for a replace tag')
|
|
|
|
return
|
|
|
|
|
|
|
|
div_element = replace_elem.cloneNode(True)
|
|
|
|
div_element.tagName = replace_elem.getAttribute('div_name')
|
|
|
|
div_element.removeAttribute('div_name')
|
|
|
|
for key,value in replace_elem.attributes.items():
|
|
|
|
if key == 'div_name': continue
|
|
|
|
|
|
|
|
value = value.replace('dyn:blog_id', str(self.blog.id))
|
|
|
|
|
2013-02-09 08:55:06 +01:00
|
|
|
if not self.cur_post_obj is None:
|
|
|
|
url = self.cur_post_obj.getPath()
|
|
|
|
full_url = self.cur_post_obj.blog.name + url
|
|
|
|
value = value.replace('dyn:post_url', url)
|
|
|
|
value = value.replace('dyn:post_full_url', full_url)
|
|
|
|
|
2012-12-10 19:30:25 +01:00
|
|
|
div_element.setAttribute(key, value)
|
|
|
|
|
|
|
|
root.replaceChild(div_element, replace_elem)
|
|
|
|
return div_element
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2014-06-05 19:56:47 +02:00
|
|
|
def createCommentsCount(self, posts, dom, root, node):
|
|
|
|
from dynastie.models import Comment
|
|
|
|
if self.cur_post_obj is None:
|
|
|
|
count = 0
|
|
|
|
else:
|
|
|
|
count = Comment.objects.filter(post=self.cur_post_obj.id).count()
|
|
|
|
self.replaceByText(dom, root, node, str(count))
|
|
|
|
|
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
|
|
|
|
|
2013-01-06 12:10:29 +01:00
|
|
|
def createTitle(self, posts, dom, root, title_elem):
|
|
|
|
create_link = (title_elem.getAttribute('link') == '1')
|
|
|
|
post = self.cur_post_obj
|
|
|
|
if create_link == True:
|
|
|
|
node = self.createElement(dom, 'title')
|
|
|
|
node.appendChild(self.createLinkElem(dom, post.getPath(), post.title))
|
|
|
|
else:
|
|
|
|
node = self.createElement(dom, 'title', post.title)
|
|
|
|
|
|
|
|
root.replaceChild(node, title_elem)
|
|
|
|
|
2014-06-01 18:58:13 +02:00
|
|
|
def createFirstPageOnly(self, posts, dom, root, node):
|
|
|
|
if self.cur_page == 0:
|
|
|
|
for n in node.childNodes:
|
|
|
|
root.insertBefore(n.cloneNode(True), node)
|
|
|
|
root.removeChild(node)
|
|
|
|
|
|
|
|
def createLJDCLast(self, posts, dom, root, node):
|
|
|
|
from dynastie.generators.ljdc import LJDC
|
|
|
|
|
|
|
|
l = LJDC()
|
|
|
|
img = l.getLast(dom, self.blog.src_path)
|
|
|
|
|
|
|
|
if not img is None:
|
|
|
|
root.replaceChild(img, node)
|
|
|
|
else:
|
|
|
|
root.removeChild(node)
|
|
|
|
|
2013-01-13 18:55:20 +01:00
|
|
|
def createDate(self, posts, dom, root, date_elem):
|
|
|
|
date_format = date_elem.getAttribute('format')
|
|
|
|
if date_format == '':
|
|
|
|
date_format = '%A, %d %B %Y %H:%m'
|
|
|
|
|
|
|
|
post = self.cur_post_obj
|
|
|
|
node = self.createElement(dom, 'date', post.creation_date.strftime(date_format))
|
|
|
|
|
|
|
|
root.replaceChild(node, date_elem)
|
|
|
|
|
2013-01-31 19:24:29 +01:00
|
|
|
def pygmentCode(self, code):
|
|
|
|
while True:
|
|
|
|
start = code.find('<dyn:code')
|
|
|
|
|
|
|
|
if start == -1: break
|
|
|
|
end = code.find('</dyn:code>')
|
|
|
|
|
|
|
|
if end < start:
|
|
|
|
self.addError('Invalid <dyn:code> tags in ' + filename)
|
|
|
|
break
|
|
|
|
|
|
|
|
try:
|
|
|
|
dom = parseString(code[start:end+11])
|
|
|
|
except xml.dom.DOMException as e:
|
|
|
|
self.addError('Error parsing ' + filename)
|
|
|
|
break
|
|
|
|
|
|
|
|
res = self.createCode(dom, dom.firstChild)
|
|
|
|
code = code.replace(code[start:end+11], res)
|
|
|
|
|
|
|
|
return code
|
|
|
|
|
2013-01-06 12:10:29 +01:00
|
|
|
def createPost(self, posts, dom, post_elem, root):
|
2014-01-04 13:43:38 +01:00
|
|
|
from dynastie.models import Post
|
2013-01-06 12:10:29 +01:00
|
|
|
post = self.cur_post_obj
|
2013-01-06 17:59:46 +01:00
|
|
|
|
|
|
|
if post.id in self.hash_posts and not self.first_try:
|
|
|
|
node = self.hash_posts[post.id]
|
|
|
|
return node.cloneNode(0)
|
|
|
|
|
2012-07-18 11:30:54 +02:00
|
|
|
values = {}
|
2013-01-29 18:51:23 +01:00
|
|
|
try:
|
|
|
|
values['author'] = post.author.first_name + ' ' + post.author.last_name
|
|
|
|
except:
|
|
|
|
values['author'] = 'Unknown'
|
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)
|
2013-01-06 17:59:46 +01:00
|
|
|
return None
|
2012-08-05 13:36:55 +02:00
|
|
|
|
2013-01-06 18:28:03 +01:00
|
|
|
if not filename in self.hash_posts_content:
|
|
|
|
f = open(filename, 'rb')
|
|
|
|
post_content = f.read()
|
|
|
|
f.close()
|
2014-01-04 13:43:38 +01:00
|
|
|
if post.content_format == Post.CONTENT_TEXT:
|
|
|
|
from dynastie.generators import markdown2
|
|
|
|
post_content = markdown2.markdown(post_content)
|
2013-01-06 18:28:03 +01:00
|
|
|
self.hash_posts_content[filename] = post_content
|
|
|
|
else:
|
|
|
|
post_content = self.hash_posts_content[filename]
|
2012-08-14 21:26:48 +02:00
|
|
|
|
2013-01-31 19:24:29 +01:00
|
|
|
post_content = self.pygmentCode(post_content)
|
2012-11-13 18:33:38 +01: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)
|
|
|
|
|
2013-01-06 17:59:46 +01:00
|
|
|
if post.id in self.hash_posts:
|
|
|
|
import hashlib
|
|
|
|
|
|
|
|
# Here, we are in first_try, check that computed
|
|
|
|
# post has the same result than the one in cache
|
|
|
|
self.first_try = False
|
|
|
|
|
|
|
|
writer = StrictUTF8Writer()
|
|
|
|
node = self.hash_posts[post.id]
|
|
|
|
node.writexml(writer)
|
|
|
|
content1 = writer.getvalue().encode('utf-8')
|
|
|
|
|
|
|
|
writer = StrictUTF8Writer()
|
|
|
|
post_elem.writexml(writer)
|
|
|
|
content2 = writer.getvalue().encode('utf-8')
|
|
|
|
|
|
|
|
md5_1 = hashlib.md5()
|
|
|
|
md5_1.update(content1)
|
|
|
|
|
|
|
|
md5_2 = hashlib.md5()
|
|
|
|
md5_2.update(content2)
|
|
|
|
|
|
|
|
# If not, clear cache
|
|
|
|
if md5_1.digest() != md5_2.digest():
|
|
|
|
self.hash_posts = {}
|
|
|
|
|
|
|
|
self.hash_posts[post.id] = post_elem.cloneNode(0)
|
|
|
|
|
|
|
|
return post_elem
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
def createPosts(self, posts, dom, root, node):
|
|
|
|
posts_elem = self.createElement(dom, 'posts')
|
2013-01-06 12:10:29 +01:00
|
|
|
create_link = (node.getAttribute('link') == '1')
|
2012-08-28 09:09:14 +02:00
|
|
|
for i in range(0, self.posts_per_page):
|
|
|
|
post_elem = self.createElement(dom, 'post')
|
|
|
|
if len(posts) > self.cur_post:
|
2013-01-06 12:10:29 +01:00
|
|
|
self.cur_post_obj = posts[self.cur_post]
|
2013-01-06 17:59:46 +01:00
|
|
|
post_elem = self.createPost(posts, dom, post_elem, node)
|
2013-01-31 19:21:58 +01:00
|
|
|
if post_elem is None: continue
|
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>')
|
2013-01-06 12:10:29 +01:00
|
|
|
self.cur_post_obj = None
|
2013-01-06 17:59:46 +01:00
|
|
|
if not post_elem is None:
|
|
|
|
posts_elem.appendChild(post_elem)
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-11-08 21:23:30 +01:00
|
|
|
# Parse inner HTML
|
|
|
|
self._parse(self.hooks, posts, dom, post_elem)
|
|
|
|
|
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-11-08 21:23:30 +01:00
|
|
|
def createTags(self, posts, dom, root, node):
|
|
|
|
from dynastie.models import Post
|
|
|
|
tags_elem = self.createElement(dom, 'tags')
|
|
|
|
create_link = (node.getAttribute('link') == '1')
|
2013-02-17 08:38:58 +01:00
|
|
|
if type(posts) == models.query.QuerySet or type(posts) == list:
|
2012-11-08 21:23:30 +01:00
|
|
|
if len(posts) > self.cur_post:
|
|
|
|
cur_post = posts[self.cur_post]
|
|
|
|
else:
|
|
|
|
cur_post = None
|
|
|
|
elif type(posts) == Post:
|
|
|
|
cur_post = posts
|
|
|
|
else:
|
|
|
|
cur_post = None
|
|
|
|
|
|
|
|
if not cur_post is None:
|
|
|
|
for tag in cur_post.tags.all():
|
|
|
|
if create_link:
|
|
|
|
tag_elem = self.createElement(dom, 'tag')
|
|
|
|
link_elem = self.createLinkElem(dom, '/tag/' + tag.name_slug, '#' + tag.name)
|
|
|
|
tag_elem.appendChild(link_elem)
|
|
|
|
else:
|
|
|
|
tag_elem = self.createElement(dom, 'tag', '#' + tag.name)
|
|
|
|
tags_elem.appendChild(tag_elem)
|
|
|
|
|
|
|
|
if len(cur_post.tags.all()) == 0:
|
|
|
|
root.removeChild(node)
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
root.replaceChild(tags_elem, node)
|
|
|
|
else:
|
|
|
|
root.removeChild(node)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return tags_elem
|
|
|
|
|
2012-11-13 18:33:38 +01:00
|
|
|
def createCode(self, dom, node):
|
|
|
|
try:
|
|
|
|
from pygments import highlight
|
|
|
|
from pygments.util import ClassNotFound
|
|
|
|
from pygments.lexers import get_lexer_by_name
|
|
|
|
from pygments.formatters import get_formatter_by_name
|
|
|
|
except ImportError:
|
|
|
|
self.addError('Pygments package is missing, please install it in order to use <dyn:code>')
|
|
|
|
return
|
|
|
|
|
|
|
|
language = node.getAttribute('language')
|
|
|
|
|
|
|
|
if language is None:
|
|
|
|
self.addWarning('No language defined for <dyn:code> assuming C language')
|
|
|
|
language = "c"
|
|
|
|
else:
|
|
|
|
language = language.lower()
|
|
|
|
|
|
|
|
lexer_options = {}
|
|
|
|
try:
|
|
|
|
lexer = get_lexer_by_name(language, **lexer_options)
|
|
|
|
except ClassNotFound:
|
|
|
|
self.addWarning('Language ' + language + ' not supported by current version of pygments')
|
|
|
|
lexer = get_lexer_by_name('c', **lexer_options)
|
|
|
|
|
2012-11-15 18:39:17 +01:00
|
|
|
formatter_options = {'classprefix' : 'color_emacs_', 'style' : 'emacs'}
|
2012-11-13 18:33:38 +01:00
|
|
|
|
|
|
|
for k in node.attributes.keys():
|
|
|
|
attr = node.attributes[k]
|
|
|
|
if attr.prefix != '': continue
|
|
|
|
if attr.name == 'language': continue
|
|
|
|
name = attr.name
|
|
|
|
value = attr.value
|
2012-11-15 18:39:17 +01:00
|
|
|
if name == 'colouring':
|
|
|
|
name = 'style'
|
|
|
|
formatter_options['classprefix'] = 'color_' + value + '_'
|
2012-11-13 18:33:38 +01:00
|
|
|
formatter_options[name] = value
|
|
|
|
|
|
|
|
formatter = get_formatter_by_name('html', **formatter_options)
|
|
|
|
|
|
|
|
lexer.encoding = 'utf-8'
|
|
|
|
formatter.encoding = 'utf-8'
|
2012-11-13 18:58:50 +01:00
|
|
|
|
2012-11-13 18:33:38 +01:00
|
|
|
writer = StrictUTF8Writer()
|
2012-11-19 19:11:04 +01:00
|
|
|
node.firstChild.writexml(writer)
|
2012-11-13 18:33:38 +01:00
|
|
|
code = writer.getvalue().encode('utf-8')
|
|
|
|
|
|
|
|
r,w = os.pipe()
|
|
|
|
r,w=os.fdopen(r,'r',0), os.fdopen(w,'w',0)
|
2012-11-19 19:11:04 +01:00
|
|
|
highlight(code, lexer, formatter, w)
|
2012-11-13 18:33:38 +01:00
|
|
|
w.close()
|
|
|
|
|
|
|
|
code = r.read()
|
|
|
|
r.close()
|
|
|
|
|
2012-11-19 19:11:04 +01:00
|
|
|
# Remove <pre> after <div class="highlight">
|
|
|
|
code = code[28:-13]
|
|
|
|
code = '<div class="highlight">' + code + '</div>'
|
|
|
|
|
2012-11-13 18:33:38 +01:00
|
|
|
return code
|
|
|
|
|
2013-02-17 08:38:58 +01:00
|
|
|
def parseTemplate(self, blog, src, output, name, directory=None, parsePostsTag=True):
|
2012-12-10 19:30:25 +01:00
|
|
|
self.blog = blog
|
2012-07-22 19:07:02 +02:00
|
|
|
|
2012-12-31 13:58:47 +01:00
|
|
|
if not os.path.exists(src + '/_%s.html' % name):
|
|
|
|
self.addError('No _%s.html found, exiting' % name)
|
|
|
|
return None
|
2012-07-18 11:30:54 +02:00
|
|
|
|
|
|
|
try:
|
2012-12-31 13:58:47 +01:00
|
|
|
dom = parse(src + '/_%s.html' % name)
|
2012-07-18 11:30:54 +02:00
|
|
|
except xml.dom.DOMException as e:
|
2012-12-31 13:58:47 +01:00
|
|
|
self.addError('Error parsing _%s.html : ' + e)
|
|
|
|
return None
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-12-31 13:58:47 +01:00
|
|
|
if not directory is None and not os.path.exists(output + '/' + directory):
|
|
|
|
os.mkdir(output + '/' + directory)
|
|
|
|
|
2013-02-17 08:38:58 +01:00
|
|
|
if not parsePostsTag: return dom
|
|
|
|
|
2012-08-28 09:09:14 +02:00
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2014-03-27 18:24:58 +01:00
|
|
|
if not post_nodes is None and len(post_nodes) > 0:
|
2012-08-28 09:09:14 +02:00
|
|
|
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:
|
2014-03-27 18:24:58 +01:00
|
|
|
self.addWarning('No tag dyn:posts found')
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2012-12-31 13:58:47 +01:00
|
|
|
return dom
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2013-02-17 08:38:58 +01:00
|
|
|
def generatePages(self, dom, posts, src, output, name):
|
2012-12-31 13:58:47 +01:00
|
|
|
if len(posts) > self.posts_per_page:
|
|
|
|
self.nb_pages = self.computeNbPages(len(posts), self.posts_per_page)
|
2012-07-18 11:30:54 +02:00
|
|
|
|
2013-02-17 08:38:58 +01:00
|
|
|
if not os.path.exists(output + self.dirname):
|
2012-12-31 13:58:47 +01:00
|
|
|
os.mkdir(output + self.dirname)
|
|
|
|
|
2012-12-31 16:50:11 +01:00
|
|
|
filename = self.dirname + '/' + self.filename + '.html'
|
2012-12-31 17:45:21 +01:00
|
|
|
|
|
|
|
impl = xml.dom.getDOMImplementation()
|
2012-08-15 11:52:13 +02:00
|
|
|
|
2012-07-20 21:54:43 +02:00
|
|
|
while self.cur_page <= self.nb_pages:
|
|
|
|
#print 'Generate ' + filename
|
2012-12-31 17:45:21 +01:00
|
|
|
dom_ = impl.createDocument('', 'xml', None)
|
2014-03-27 18:24:58 +01:00
|
|
|
dom_.replaceChild(dom.firstChild.cloneNode(True), dom_.firstChild)
|
|
|
|
nodes = self.parse(src, self.hooks, posts, dom_, dom_.firstChild)
|
|
|
|
self.writeIfNotTheSame(output + filename, nodes)
|
2012-07-18 11:30:54 +02:00
|
|
|
self.cur_page = self.cur_page + 1
|
2012-12-31 16:50:11 +01:00
|
|
|
filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
2012-07-20 21:54:43 +02:00
|
|
|
|
2012-12-31 16:50:11 +01:00
|
|
|
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-12-31 16:50:11 +01:00
|
|
|
filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
|
|
|
|
filename = output + filename
|
2012-12-31 13:58:47 +01:00
|
|
|
|
|
|
|
def generate(self, blog, src, output):
|
|
|
|
from dynastie.models import Post, Blog
|
|
|
|
|
2014-06-01 18:58:13 +02:00
|
|
|
self.blog = blog
|
|
|
|
|
2012-12-31 13:58:47 +01:00
|
|
|
dom = self.parseTemplate(blog, src, output, 'index')
|
|
|
|
if dom is None: return self.report
|
|
|
|
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
cur_year = now.year
|
|
|
|
posts = Post.objects.filter(creation_date__year=cur_year, published=True, front_page=True).order_by('-creation_date')
|
|
|
|
|
|
|
|
if posts.count() < self.posts_per_page:
|
2013-01-03 10:19:37 +01:00
|
|
|
posts = Post.objects.filter(published=True, front_page=True).order_by('-creation_date')[:self.posts_per_page]
|
2012-12-31 13:58:47 +01:00
|
|
|
|
|
|
|
self.dirname = ''
|
|
|
|
self.generatePages(dom, posts, src, output, 'index')
|
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
|