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-09-11 22:25:58 +02:00
|
|
|
import datetime
|
2012-08-28 09:09:14 +02:00
|
|
|
import os
|
2014-09-24 20:27:27 +02:00
|
|
|
import xml
|
2012-08-28 09:09:14 +02:00
|
|
|
from xml.dom.minidom import parse, parseString
|
2012-09-11 22:25:58 +02:00
|
|
|
from dynastie.generators.generator import DynastieGenerator, StrictUTF8Writer
|
2012-08-28 09:09:14 +02:00
|
|
|
from dynastie.generators.index import Index
|
|
|
|
from django.db import models
|
2012-10-20 19:05:29 +02:00
|
|
|
from dynastie.tree import TreeNode
|
2012-08-28 09:09:14 +02:00
|
|
|
|
|
|
|
class Post(Index):
|
|
|
|
|
2012-10-16 22:06:33 +02:00
|
|
|
def createReplace(self, post, 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:post_id', str(post.id))
|
|
|
|
if self.cur_comment is None:
|
2012-10-20 19:05:29 +02:00
|
|
|
value = value.replace('dyn:comment_index', '0')
|
|
|
|
value = value.replace('dyn:comment_id', '0')
|
2012-10-16 22:06:33 +02:00
|
|
|
else:
|
2014-09-24 20:27:27 +02:00
|
|
|
value = value.replace('dyn:comment_index', str(self.comment_index[self.cur_comment.id]))
|
2012-10-20 19:05:29 +02:00
|
|
|
value = value.replace('dyn:comment_id', str(self.cur_comment.id))
|
2012-12-10 19:30:25 +01:00
|
|
|
value = value.replace('dyn:blog_id', str(self.blog.id))
|
2013-02-09 08:55:06 +01:00
|
|
|
url = post.getPath()
|
|
|
|
full_url = post.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
|
|
|
|
2012-10-16 22:06:33 +02:00
|
|
|
div_element.setAttribute(key, value)
|
|
|
|
|
|
|
|
root.replaceChild(div_element, replace_elem)
|
|
|
|
return div_element
|
|
|
|
|
|
|
|
def createComment(self, comment, dom, comment_elem, root):
|
|
|
|
values = {}
|
2012-10-20 19:05:29 +02:00
|
|
|
values['comment_index'] = str(self.comment_index[comment.id])
|
2012-10-16 22:06:33 +02:00
|
|
|
values['comment_author'] = comment.author
|
|
|
|
values['comment_date'] = comment.date.strftime('%d %B %Y %H:%m')
|
|
|
|
values['comment_content'] = comment.the_comment
|
|
|
|
|
|
|
|
self.simpleTransform(values, dom, comment_elem, root)
|
|
|
|
|
2012-10-20 19:05:29 +02:00
|
|
|
def _createComments(self, rootNode, post, dom, root_comment, root):
|
|
|
|
self.cur_comment = rootNode.value
|
|
|
|
comment_element = self.createElement(dom, 'comment')
|
|
|
|
self.createComment(self.cur_comment, dom, comment_element, root)
|
|
|
|
root_comment.appendChild(comment_element)
|
|
|
|
# Parse inner HTML
|
|
|
|
self._parse(self.hooks, post, dom, comment_element)
|
|
|
|
|
|
|
|
for commentNode in rootNode.childs:
|
|
|
|
self._createComments(commentNode, post, dom, comment_element, root)
|
|
|
|
|
2012-10-11 22:26:47 +02:00
|
|
|
def createComments(self, post, dom, post_elem, root):
|
|
|
|
from dynastie.models import Post, Blog, Comment
|
|
|
|
|
2012-10-16 22:06:33 +02:00
|
|
|
comments = Comment.objects.filter(post=post).order_by('date')
|
2012-10-11 22:26:47 +02:00
|
|
|
|
2014-09-24 20:27:27 +02:00
|
|
|
self.cur_comment = None
|
|
|
|
self.comment_index = {}
|
2012-10-16 22:06:33 +02:00
|
|
|
index = 1
|
2012-10-20 19:05:29 +02:00
|
|
|
|
|
|
|
rootNode = TreeNode('', '')
|
2012-10-11 22:26:47 +02:00
|
|
|
for comment in comments:
|
2012-10-16 22:06:33 +02:00
|
|
|
self.comment_index[comment.id] = index
|
|
|
|
index = index + 1
|
2012-10-20 19:05:29 +02:00
|
|
|
tnode = TreeNode(comment.id, comment)
|
|
|
|
if comment.parent is None:
|
|
|
|
rootNode.addChildNode(tnode)
|
|
|
|
else:
|
|
|
|
temp = rootNode.find(comment.parent.id)
|
|
|
|
if temp is None:
|
|
|
|
self.addWarning('Error with comments chain')
|
|
|
|
rootNode.addChildNode(tnode)
|
2012-10-11 22:26:47 +02:00
|
|
|
else:
|
2012-10-20 19:05:29 +02:00
|
|
|
temp.addChildNode(tnode)
|
|
|
|
|
2012-10-11 22:26:47 +02:00
|
|
|
initial_root_comment = root_comment = self.createElement(dom, 'comments')
|
2012-10-20 19:05:29 +02:00
|
|
|
for tnode in rootNode.childs:
|
|
|
|
self._createComments(tnode, post, dom, root_comment, root)
|
2012-10-16 22:06:33 +02:00
|
|
|
|
|
|
|
# Empty tag seems to crap rendering
|
2012-10-20 19:05:29 +02:00
|
|
|
if len(rootNode.childs) == 0:
|
2012-10-16 22:06:33 +02:00
|
|
|
post_elem.removeChild(root)
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
post_elem.replaceChild(root_comment, root)
|
|
|
|
return root_comment
|
2012-10-11 22:26:47 +02:00
|
|
|
|
2012-09-25 21:49:56 +02:00
|
|
|
def createMetas(self, post, dom, meta_elem, root):
|
|
|
|
name = root.getAttribute('name')
|
|
|
|
if name is None:
|
|
|
|
self.addError('Missing name attribute in dyn:meta')
|
|
|
|
return
|
|
|
|
|
|
|
|
new_elem = None
|
|
|
|
if name == 'keywords':
|
2013-02-09 08:55:06 +01:00
|
|
|
value = post.keywords.replace('"', '“')
|
|
|
|
new_elem = self.createMeta(dom, name, value)
|
2012-09-25 21:49:56 +02:00
|
|
|
elif name == 'title':
|
2013-02-09 08:55:06 +01:00
|
|
|
value = post.title.replace('"', '“')
|
|
|
|
new_elem = self.createMeta(dom, name, value)
|
2012-09-25 21:49:56 +02:00
|
|
|
elif name == 'description':
|
2013-02-09 08:55:06 +01:00
|
|
|
value = post.description.replace('"', '“')
|
|
|
|
new_elem = self.createMeta(dom, name, value)
|
2012-09-25 21:49:56 +02:00
|
|
|
elif name == 'author':
|
2013-01-29 18:51:23 +01:00
|
|
|
try:
|
|
|
|
new_elem = self.createMeta(dom, name, post.author.first_name + ' ' + post.author.last_name)
|
|
|
|
except:
|
|
|
|
return None
|
2012-09-25 21:49:56 +02:00
|
|
|
|
|
|
|
if not new_elem is None:
|
|
|
|
root.parentNode.replaceChild(new_elem, root)
|
2012-10-16 22:06:33 +02:00
|
|
|
return new_elem
|
2012-09-25 21:49:56 +02:00
|
|
|
else:
|
|
|
|
self.addError('name attribute \'' + name + '\' unknown for dyn:meta' )
|
2012-10-16 22:06:33 +02:00
|
|
|
return None
|
2012-09-25 21:49:56 +02:00
|
|
|
|
2014-09-24 20:27:27 +02:00
|
|
|
def createPostTitle(self, post, dom, root, node):
|
|
|
|
value = post.title.replace('"', '“')
|
|
|
|
self.replaceByText(dom, root, node, value)
|
|
|
|
return None
|
2012-10-11 22:26:47 +02:00
|
|
|
|
2014-09-24 20:27:27 +02:00
|
|
|
def _createPost(self, post, dom, post_elem, root):
|
2013-01-06 12:10:29 +01:00
|
|
|
self.cur_post_obj = post
|
|
|
|
posts = [post]
|
|
|
|
self.createPost(posts, dom, post_elem, root)
|
2012-10-11 22:26:47 +02:00
|
|
|
|
|
|
|
# Post are appended by index. Remove template
|
2012-09-25 21:49:56 +02:00
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, 'post')
|
2012-08-28 09:09:14 +02:00
|
|
|
post_elem = post_nodes[0]
|
|
|
|
post_elem.parentNode.removeChild(post_elem)
|
|
|
|
|
2012-09-25 21:49:56 +02:00
|
|
|
title_nodes = dom.getElementsByTagName('title')
|
2012-08-28 10:42:33 +02:00
|
|
|
|
|
|
|
# Set title to be title's post
|
|
|
|
for node in title_nodes:
|
|
|
|
if node.hasChildNodes():
|
|
|
|
node.removeChild(node.childNodes[0])
|
|
|
|
node.appendChild(dom.createTextNode(post.title))
|
|
|
|
|
2012-10-16 22:06:33 +02:00
|
|
|
return node
|
|
|
|
|
2012-10-09 20:47:12 +02:00
|
|
|
def _generate(self, blog, src, output, posts):
|
2014-09-24 20:27:27 +02:00
|
|
|
from dynastie.search import Search
|
|
|
|
|
|
|
|
self.cur_comment = None
|
|
|
|
self.comment_index = {}
|
2013-01-06 12:10:29 +01:00
|
|
|
|
|
|
|
self.hooks['post'] = self._createPost
|
|
|
|
self.hooks['meta'] = self.createMetas
|
|
|
|
self.hooks['comments'] = self.createComments
|
|
|
|
self.hooks['replace'] = self.createReplace
|
2014-09-24 20:27:27 +02:00
|
|
|
self.hooks['post_title'] = self.createPostTitle
|
2013-01-06 12:10:29 +01:00
|
|
|
del self.hooks['navigation']
|
2013-01-12 09:02:08 +01:00
|
|
|
del self.hooks['recents']
|
|
|
|
del self.hooks['posts']
|
2012-08-28 09:09:14 +02:00
|
|
|
|
2013-02-17 08:38:58 +01:00
|
|
|
dom = self.parseTemplate(blog, src, output, 'post', None, False)
|
|
|
|
if dom is None: return self.report
|
2012-08-28 09:09:14 +02:00
|
|
|
|
2012-12-31 17:45:21 +01:00
|
|
|
impl = xml.dom.getDOMImplementation()
|
2014-09-24 20:27:27 +02:00
|
|
|
s = Search()
|
2012-08-28 09:09:14 +02:00
|
|
|
for post in posts:
|
|
|
|
filename = output + '/post/'
|
|
|
|
filename = filename + post.creation_date.strftime("%Y") + '/' + post.creation_date.strftime("%m") + '/'
|
|
|
|
if not os.path.exists(filename):
|
|
|
|
os.makedirs(filename)
|
|
|
|
filename = filename + post.title_slug + '.html'
|
2014-06-01 11:28:47 +02:00
|
|
|
|
|
|
|
if not post.published:
|
|
|
|
if os.path.exists(filename):
|
|
|
|
os.unlink(filename)
|
|
|
|
os.unlink(filename + '.gz')
|
|
|
|
self.addReport('Remove ' + filename)
|
|
|
|
s.delete_post(blog, post.id)
|
|
|
|
continue
|
|
|
|
#print 'Generate ' + filename
|
|
|
|
dom_ = impl.createDocument('', 'xml', None)
|
|
|
|
dom_.replaceChild(dom.firstChild.cloneNode(True), dom_.firstChild)
|
|
|
|
nodes = self.parse(src, self.hooks, post, dom_, dom_.firstChild)
|
|
|
|
|
2014-03-27 18:24:58 +01:00
|
|
|
self.writeIfNotTheSame(filename, nodes)
|
2012-08-28 09:09:14 +02:00
|
|
|
|
|
|
|
if not self.somethingWrote:
|
|
|
|
self.addReport('Nothing changed')
|
|
|
|
|
|
|
|
return self.report
|
|
|
|
|
2012-10-04 21:49:33 +02:00
|
|
|
def generate(self, blog, src, output):
|
|
|
|
from dynastie.models import Post, Blog
|
|
|
|
|
2012-12-10 19:30:25 +01:00
|
|
|
self.blog = blog
|
|
|
|
|
2016-01-09 20:10:27 +01:00
|
|
|
self.parent_posts = []
|
2012-10-04 21:49:33 +02:00
|
|
|
posts = Post.objects.all()
|
|
|
|
|
2012-10-09 20:47:12 +02:00
|
|
|
return self._generate(blog, src, output, posts)
|
2012-10-04 21:49:33 +02:00
|
|
|
|
2012-09-11 22:25:58 +02:00
|
|
|
def createPreview(self, values, dom, root, node):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
|
|
|
|
v = {}
|
|
|
|
v['title'] = self.createLinkElem(dom, '/preview.html', values['title'])
|
|
|
|
v['author'] = values['author']
|
|
|
|
v['date'] = now.strftime("%A, %d %B %Y %H:%m")
|
|
|
|
v['post_content'] = ''
|
|
|
|
|
2016-01-09 20:10:27 +01:00
|
|
|
post_content = self._manageInternalPosts(None, values['content'], self.user)
|
2015-09-21 19:07:39 +02:00
|
|
|
post_content = self.pygmentCode(post_content)
|
2013-01-31 19:24:29 +01:00
|
|
|
|
2012-09-11 22:25:58 +02:00
|
|
|
self.simpleTransform(v, dom, root, node)
|
|
|
|
|
|
|
|
content_nodes = root.getElementsByTagName("div")
|
|
|
|
post_transform = ('post_content')
|
2016-01-09 20:10:27 +01:00
|
|
|
content_node = None
|
2012-09-11 22:25:58 +02:00
|
|
|
for content_node in content_nodes:
|
|
|
|
the_class = content_node.getAttribute('class')
|
|
|
|
if not the_class in post_transform:
|
|
|
|
continue
|
|
|
|
if the_class == 'post_content':
|
2016-01-09 20:10:27 +01:00
|
|
|
s = u'<div>' + post_content + u'</div>'
|
|
|
|
new_node = parseString(s.encode('utf-8'))
|
|
|
|
for n in new_node.childNodes[0].childNodes:
|
|
|
|
content_node.appendChild(n)
|
|
|
|
break
|
2012-09-11 22:25:58 +02:00
|
|
|
|
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "post")
|
|
|
|
post_elem = post_nodes[0]
|
|
|
|
post_elem.parentNode.removeChild(post_elem)
|
|
|
|
|
2016-01-09 20:10:27 +01:00
|
|
|
return content_node
|
2012-10-16 22:06:33 +02:00
|
|
|
|
2015-09-21 19:07:39 +02:00
|
|
|
def preview(self, request, src, values):
|
2012-09-11 22:25:58 +02:00
|
|
|
from dynastie.models import Blog
|
|
|
|
|
2015-09-21 19:07:39 +02:00
|
|
|
self.user = request.user
|
|
|
|
|
2013-01-06 12:10:29 +01:00
|
|
|
# Override all hooks
|
2012-11-08 21:23:30 +01:00
|
|
|
self.hooks = {'post' : self.createPreview,
|
2016-01-09 20:10:27 +01:00
|
|
|
'tags' : self.createTags,
|
|
|
|
}
|
2012-09-11 22:25:58 +02:00
|
|
|
|
|
|
|
if not os.path.exists(src + '/_post.html'):
|
|
|
|
self.addError('No _post.html found, exiting')
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
try:
|
|
|
|
dom = parse(src + '/_post.html')
|
|
|
|
except xml.dom.DOMException as e:
|
|
|
|
self.addError('Error parsing _post.html : ' + e)
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
post_nodes = dom.getElementsByTagNameNS(self.URI, "posts")
|
|
|
|
|
|
|
|
if post_nodes is None:
|
|
|
|
self.addError('No tag dyn:posts found')
|
|
|
|
return self.report
|
|
|
|
|
|
|
|
nodes = dom.getElementsByTagName("*")
|
2012-10-20 19:05:29 +02:00
|
|
|
nodes[0] = self.parse(src, self.hooks, values, dom, nodes[0])
|
2012-09-11 22:25:58 +02:00
|
|
|
writer = StrictUTF8Writer()
|
|
|
|
nodes[0].writexml(writer)
|
|
|
|
|
|
|
|
return writer.getvalue().encode('utf-8')
|