Add article inclusion (Mardown only)

This commit is contained in:
Gregory Soutade 2015-09-21 19:07:39 +02:00
parent dd6739461b
commit cde08b8cfa
4 changed files with 77 additions and 19 deletions

View File

@ -1,3 +1,12 @@
v0.5 (21/09/2015)
** User **
Enable code coloration support with Markdown syntax
Add article inclusion (Mardown only)
** Dev **
Support Django 1.8
v0.4 (09/08/2015) v0.4 (09/08/2015)
** User ** ** User **
Redirect user to comment when it's added and not to begining of page Redirect user to comment when it's added and not to begining of page

View File

@ -57,7 +57,7 @@ class DynastieGenerator:
URI = "http://indefero.soutade.fr/p/dynastie" URI = "http://indefero.soutade.fr/p/dynastie"
def __init__(self, hash_posts=None, hash_posts_content=None): def __init__(self, hash_posts={}, hash_posts_content={}):
self.report = '' self.report = ''
self.somethingWrote = False self.somethingWrote = False
self.hash_posts = hash_posts self.hash_posts = hash_posts

View File

@ -18,6 +18,7 @@
along with Dynastie. If not, see <http://www.gnu.org/licenses/>. along with Dynastie. If not, see <http://www.gnu.org/licenses/>.
""" """
import os import os
import re
import datetime import datetime
import hashlib import hashlib
import xml import xml
@ -30,7 +31,7 @@ from dynastie.generators import markdown2
class Index(DynastieGenerator): class Index(DynastieGenerator):
def __init__(self, hash_posts=None, hash_posts_content=None): def __init__(self, hash_posts={}, hash_posts_content={}):
DynastieGenerator.__init__(self, hash_posts, hash_posts_content) DynastieGenerator.__init__(self, hash_posts, hash_posts_content)
self.hooks = {'posts' : self.createPosts, self.hooks = {'posts' : self.createPosts,
@ -197,32 +198,56 @@ class Index(DynastieGenerator):
if end < start: if end < start:
self.addError('Invalid <dyn:code> tags in ' + self.filename) self.addError('Invalid <dyn:code> tags in ' + self.filename)
break break
try: try:
dom = parseString(code[start:end+11]) dom = parseString(code[start:end+11])
except xml.dom.DOMException as e: except xml.dom.DOMException as e:
self.addError('Error parsing ' + self.filename) self.addError('Error parsing ' + self.filename)
break break
res = self.createCode(dom, dom.firstChild) res = self.createCode(dom, dom.firstChild)
if res: if res:
code = code.replace(code[start:end+11], res) code = code.replace(code[start:end+11], res)
return code return code
def createPost(self, posts, dom, post_elem, root): def _have_I_right(self, user, post_id):
from dynastie.models import Post, Blog
p = Post.objects.get(pk=post_id)
if p is None: return None
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, parent_posts, user=None):
from dynastie.models import Post from dynastie.models import Post
post = self.cur_post_obj if not user: user = post.author
if post and post.content_format != Post.CONTENT_TEXT: return text
internal_posts = re.search('\[\[([0-9]+)\]\]', text)
if not internal_posts: return text
for post_id in internal_posts.groups():
post_id = int(post_id)
if post_id in parent_posts: continue
_,post = self._have_I_right(user, post_id)
if not post: continue
new_content = self._loadPostContent(post, parent_posts)
if new_content:
text = text.replace('[[' + str(post_id) + ']]', new_content)
return text
if post.id in self.hash_posts and not self.first_try: def _loadPostContent(self, post, parent_posts):
node = self.hash_posts[post.id] from dynastie.models import Post
return node.cloneNode(0)
values = {'post_content': '', 'author': 'Unknown'}
try:
values['author'] = post.author.first_name + ' ' + post.author.last_name
except:
pass
blog = post.blog blog = post.blog
blog.create_paths() blog.create_paths()
@ -238,11 +263,32 @@ class Index(DynastieGenerator):
post_content = f.read() post_content = f.read()
f.close() f.close()
if post.content_format == Post.CONTENT_TEXT: if post.content_format == Post.CONTENT_TEXT:
post_content = markdown2.markdown(post_content) parent_posts.append(post.id)
post_content = self._manageInternalPosts(post, post_content, parent_posts)
post_content = markdown2.markdown(post_content, extras=['fenced-code-blocks'])
self.hash_posts_content[filename] = post_content self.hash_posts_content[filename] = post_content
else: else:
post_content = self.hash_posts_content[filename] post_content = self.hash_posts_content[filename]
return post_content
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]
return node.cloneNode(0)
values = {'post_content': '', 'author': 'Unknown'}
try:
values['author'] = post.author.first_name + ' ' + post.author.last_name
except:
pass
post_content = _loadPostContent(post, [])
if not post_content: return None
post_content = self.pygmentCode(post_content) post_content = self.pygmentCode(post_content)
self.simpleTransform(values, dom, post_elem, root) self.simpleTransform(values, dom, post_elem, root)

View File

@ -230,7 +230,8 @@ class Post(Index):
v['date'] = now.strftime("%A, %d %B %Y %H:%m") v['date'] = now.strftime("%A, %d %B %Y %H:%m")
v['post_content'] = '' v['post_content'] = ''
values['content'] = self.pygmentCode(values['content']) post_content = self._manageInternalPosts(None, values['content'], [], self.user)
post_content = self.pygmentCode(post_content)
self.simpleTransform(v, dom, root, node) self.simpleTransform(v, dom, root, node)
@ -241,7 +242,7 @@ class Post(Index):
if not the_class in post_transform: if not the_class in post_transform:
continue continue
if the_class == 'post_content': if the_class == 'post_content':
new_node = dom.createTextNode(values['content']) new_node = dom.createTextNode(post_content)
content_node.appendChild(new_node) content_node.appendChild(new_node)
post_nodes = dom.getElementsByTagNameNS(self.URI, "post") post_nodes = dom.getElementsByTagNameNS(self.URI, "post")
@ -250,9 +251,11 @@ class Post(Index):
return post_elem return post_elem
def preview(self, src, values): def preview(self, request, src, values):
from dynastie.models import Blog from dynastie.models import Blog
self.user = request.user
# Override all hooks # Override all hooks
self.hooks = {'post' : self.createPreview, self.hooks = {'post' : self.createPreview,
'tags' : self.createTags} 'tags' : self.createTags}