Dynastie/dynastie/generators/index.py

593 lines
21 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
"""
2014-01-04 13:55:30 +01:00
Copyright 2012-2014 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/>.
"""
2012-07-20 21:54:43 +02:00
import os
2015-09-21 19:07:39 +02:00
import re
2012-07-18 11:30:54 +02:00
import datetime
2014-09-24 20:27:27 +02:00
import hashlib
2012-08-14 21:26:48 +02:00
import xml
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
2016-03-19 17:03:43 +01:00
import codecs
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
2012-07-15 18:21:26 +02:00
from django.db import models
2014-09-24 20:27:27 +02:00
from dynastie.generators import markdown2
2012-07-15 18:21:26 +02:00
class Index(DynastieGenerator):
def __init__(self, request=None, hash_posts={}, hash_posts_content={}):
DynastieGenerator.__init__(self, request, hash_posts, hash_posts_content)
self.hooks = {'posts' : self.createPosts,
'title' : self.createTitle,
2013-01-13 18:55:20 +01:00
'date' : self.createDate,
'navigation' : self.createNavigation,
2013-01-13 18:55:20 +01:00
'recents' : self.createRecents,
'tags' : self.createTags,
'replace' : self.createReplace,
'first_page_only' : self.createFirstPageOnly,
'ljdc_last' : self.createLJDCLast,
2014-06-05 19:56:47 +02:00
'comments_count' : self.createCommentsCount,
2014-06-09 11:46:25 +02:00
'category_name' : self.createCategoryName,
}
2013-01-06 17:59:46 +01:00
self.first_try = True
2014-09-24 20:27:27 +02:00
self.posts_per_page = 5
self.filename = 'index'
self.dirname = ''
self.blog = None
self.parent_posts = []
2014-09-24 20:27:27 +02:00
self.resetCounters()
def resetCounters(self):
self.nb_pages = 0
self.cur_page = 0
self.cur_post = 0
self.cur_post_obj = None
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))
2014-09-24 20:27:27 +02:00
if self.cur_post_obj:
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))
2014-06-09 11:46:25 +02:00
def createCategoryName(self, posts, dom, root, node):
from dynastie.models import Category
if self.cur_post_obj is None:
category = ''
else:
category = Category.objects.filter(id=self.cur_post_obj.category.id)[0].name
self.replaceByText(dom, root, node, category)
2014-06-05 19:56:47 +02:00
2012-08-28 09:09:14 +02:00
def createNavigation(self, posts, dom, root, node):
2014-09-24 20:27:27 +02:00
if 0 <= self.nb_pages <= 1:
return None
2012-07-20 21:54:43 +02:00
2014-09-24 20:27:27 +02:00
if self.dirname:
2012-08-14 21:26:48 +02:00
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">&lt;&lt; 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">&lt; 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">&lt; Prev</a> '
2012-07-20 21:54:43 +02:00
start = self.cur_page-5
if start < 0:
start = 0
2012-07-20 21:54:43 +02:00
end = start + 10
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
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 &gt;</a> '
nav = nav + href + str(self.nb_pages-1) + '.html">Last &gt;&gt;</a>'
2012-07-20 21:54:43 +02:00
s = '<div class="navigation">' + nav + '</div>'
new_dom = parseString(s.encode('utf-8'))
new_node = new_dom.getElementsByTagName('div')[0]
res = new_node.cloneNode(True)
root.replaceChild(res, node)
return res
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)
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:
2014-09-24 20:27:27 +02:00
self.addError('Invalid <dyn:code> tags in ' + self.filename)
2013-01-31 19:24:29 +01:00
break
2015-09-21 19:07:39 +02:00
2013-01-31 19:24:29 +01:00
try:
2016-03-19 17:03:43 +01:00
try:
dom = parseString(code[start:end+11])
except UnicodeEncodeError:
dom = parseString(code[start:end+11].encode('utf-8'))
2013-01-31 19:24:29 +01:00
except xml.dom.DOMException as e:
2014-09-24 20:27:27 +02:00
self.addError('Error parsing ' + self.filename)
2013-01-31 19:24:29 +01:00
break
2015-09-21 19:07:39 +02:00
2013-01-31 19:24:29 +01:00
res = self.createCode(dom, dom.firstChild)
if res:
code = code.replace(code[start:end+11], res)
2013-01-31 19:24:29 +01:00
return code
2015-09-21 19:07:39 +02:00
def _have_I_right(self, user, post_id):
from dynastie.models import Post, Blog
2013-01-06 17:59:46 +01:00
2015-09-21 19:07:39 +02:00
p = Post.objects.get(pk=post_id)
if p is None: return None
2013-01-06 17:59:46 +01:00
2015-09-21 19:07:39 +02:00
blog_id = p.blog.id
if not user.is_superuser:
b = Blog.objects.filter(pk=blog_id, writers=user.id)
if not b: return None
b = b[0]
else:
b = Blog.objects.get(pk=blog_id)
if b is None: return None
return (b, p)
def _manageInternalPosts(self, post, text, user=None):
2015-09-21 19:07:39 +02:00
from dynastie.models import Post
if not user: user = post.author
# Markdown replace
if not post or (post and post.content_format == Post.CONTENT_TEXT):
internal_posts = re.search('\[\[([0-9]+)\]\]', text)
if internal_posts:
for post_id in internal_posts.groups():
post_id = int(post_id)
if post_id in self.parent_posts: continue
_,post = self._have_I_right(user, post_id)
if not post: continue
new_content = self._loadPostContent(post)
if new_content:
2016-03-19 17:03:43 +01:00
p = '[[' + str(post_id) + ']]'
text = text.replace(p, new_content)
if internal_posts: return text
# HTML replace
if not post or (post and post.content_format == Post.CONTENT_HTML):
while True:
start = text.find('<dyn:postinclude')
if start == -1: break
end = text.find('</dyn:postinclude>')
if end < start:
self.addError('Invalid <dyn:postinclude> tags in ' + self.filename)
break
internal_posts = re.search('post_id="([0-9]+)"', text[start:])
if not internal_posts: break
for post_id in internal_posts.groups():
_,post = self._have_I_right(user, post_id)
if not post: break
new_content = self._loadPostContent(post)
if new_content:
2016-03-19 17:03:43 +01:00
text = text.replace(text[start:end+18], new_content)
2015-09-21 19:07:39 +02:00
return text
def _loadPostContent(self, post):
2015-09-21 19:07:39 +02:00
from dynastie.models import Post
2012-07-18 11:30:54 +02:00
2012-08-28 09:09:14 +02:00
blog = post.blog
blog.create_paths()
2012-08-28 09:09:14 +02:00
filename = blog.src_path + '/_post/' + str(post.id)
if not os.path.exists(filename):
filename2 = blog.src_path + '/_draft/' + str(post.id)
if not os.path.exists(filename2):
self.addError('File does not exists ' + filename)
return None
else:
filename = filename2
2013-01-06 18:28:03 +01:00
if not filename in self.hash_posts_content:
2016-03-19 17:03:43 +01:00
f = codecs.open(filename, 'rb', 'utf-8')
2013-01-06 18:28:03 +01:00
post_content = f.read()
f.close()
self.parent_posts.append(post.id)
post_content = self._manageInternalPosts(post, post_content)
2014-01-04 13:43:38 +01:00
if post.content_format == Post.CONTENT_TEXT:
2015-09-21 19:07:39 +02:00
post_content = markdown2.markdown(post_content, extras=['fenced-code-blocks'])
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
2015-09-21 19:07:39 +02:00
return post_content
2015-09-21 19:07:39 +02:00
def createPost(self, posts, dom, post_elem, root):
from dynastie.models import Post
post = self.cur_post_obj
if post.id in self.hash_posts and not self.first_try:
node,_ = self.hash_posts[post.id]
2015-09-21 19:07:39 +02:00
return node.cloneNode(0)
values = {'post_content': '', 'author': 'Unknown'}
try:
values['author'] = post.author.first_name + ' ' + post.author.last_name
except:
pass
self.parent_posts = []
post_content = self._loadPostContent(post)
2015-09-21 19:07:39 +02:00
if not post_content: return None
2013-01-31 19:24:29 +01:00
post_content = self.pygmentCode(post_content)
2012-08-28 09:09:14 +02:00
self.simpleTransform(values, dom, post_elem, root)
content_nodes = post_elem.getElementsByTagName('div')
post_transform = ['post_content']
for content_node in content_nodes:
the_class = content_node.getAttribute('class')
if not the_class in post_transform:
continue
new_node = dom.createTextNode(post_content)
content_node.appendChild(new_node)
writer = StrictUTF8Writer()
post_elem.writexml(writer)
content = writer.getvalue().encode('utf-8')
md5 = hashlib.md5()
md5.update(content)
2013-01-06 17:59:46 +01:00
if post.id in self.hash_posts:
# Here, we are in first_try, check that computed
# post has the same result than the one in cache
self.first_try = False
_,md5_2 = self.hash_posts[post.id]
2013-01-06 17:59:46 +01:00
# If not, clear cache
if md5.digest() != md5_2:
2013-01-06 17:59:46 +01:00
self.hash_posts = {}
self.hash_posts[post.id] = (post_elem.cloneNode(0), md5.digest())
2014-09-24 20:27:27 +02:00
else:
self.hash_posts[post.id] = (post_elem.cloneNode(0), md5.digest())
2013-01-06 17:59:46 +01:00
return post_elem
2012-08-28 09:09:14 +02:00
def createPosts(self, posts, dom, root, node):
posts_elem = self.createElement(dom, 'posts')
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:
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)
if post_elem is None: continue
else:
2012-08-28 09:09:14 +02:00
post_elem = self.createElement(dom, '', '<b>No posts yet</b>')
self.cur_post_obj = None
2014-09-24 20:27:27 +02:00
if post_elem:
2013-01-06 17:59:46 +01:00
posts_elem.appendChild(post_elem)
2014-09-24 20:27:27 +02: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):
break
2012-08-28 09:09:14 +02:00
root.replaceChild(posts_elem, node)
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):
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
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
recents_elem.appendChild(list_elem)
root.replaceChild(recents_elem, node)
return recents_elem
2012-08-01 22:04:41 +02: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')
if type(posts) == models.query.QuerySet or type(posts) == list:
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
2014-09-24 20:27:27 +02:00
if cur_post:
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
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 None
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)
formatter_options = {'classprefix' : 'color_emacs_', 'style' : 'emacs'}
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
if name == 'colouring':
name = 'style'
formatter_options['classprefix'] = 'color_' + value + '_'
formatter_options[name] = value
formatter = get_formatter_by_name('html', **formatter_options)
lexer.encoding = 'utf-8'
formatter.encoding = 'utf-8'
writer = StrictUTF8Writer()
2012-11-19 19:11:04 +01:00
node.firstChild.writexml(writer)
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)
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]
2016-03-19 17:03:43 +01:00
code = u'<div class="highlight">' + unicode(code, 'utf-8') + u'</div>'
2012-11-19 19:11:04 +01:00
return code
def parseTemplate(self, blog, src, output, name, directory=None, parsePostsTag=True):
2012-12-10 19:30:25 +01:00
self.blog = blog
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:
dom = parse(src + '/_%s.html' % name)
2012-07-18 11:30:54 +02:00
except xml.dom.DOMException as e:
self.addError('Error parsing _%s.html : ' + e)
return None
2012-07-18 11:30:54 +02:00
2014-09-24 20:27:27 +02:00
if directory and not os.path.exists(output + '/' + directory):
os.makedirs(output + '/' + directory)
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-09-24 20:27:27 +02:00
if post_nodes:
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-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
return dom
2012-07-18 11:30:54 +02:00
def generatePages(self, dom, posts, src, output, name):
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
if not os.path.exists(output + self.dirname):
os.mkdir(output + self.dirname)
filename = self.dirname + '/' + self.filename + '.html'
impl = xml.dom.getDOMImplementation()
2012-07-20 21:54:43 +02:00
while self.cur_page <= self.nb_pages:
#print 'Generate ' + filename
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
filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
2012-07-20 21:54:43 +02: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
filename = self.dirname + '/' + self.filename + str(self.cur_page) + '.html'
filename = output + filename
def generate(self, blog, src, output):
from dynastie.models import Post, Blog
self.blog = blog
self.parent_posts = []
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:
posts = Post.objects.filter(published=True, front_page=True).order_by('-creation_date')[:self.posts_per_page]
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