Add Markdown support

This commit is contained in:
Gregory Soutade 2014-01-04 13:43:38 +01:00
parent 9de9cea99a
commit 88e6ebd3a4
11 changed files with 2521 additions and 12 deletions

View File

@ -32,7 +32,7 @@ class PostForm(ModelForm):
class Meta:
model = Post
exclude = ('title_slug', 'creation_date', 'modification_date', 'author', 'blog', 'tags')
exclude = ('title_slug', 'creation_date', 'modification_date', 'author', 'blog', 'tags', 'content_format')
def __init__(self, *args, **kwargs):
super(PostForm, self).__init__(*args, **kwargs)
@ -46,7 +46,7 @@ class CategoryForm(ModelForm):
class UserForm(ModelForm):
class Meta:
model = User
exclude = ('is_staff', 'is_active', 'last_login', 'last_joined', 'user_permissions', 'groups', 'date_joined', 'password')
exclude = ('password')
class CommentForm(ModelForm):
class Meta:

View File

@ -165,6 +165,7 @@ class Index(DynastieGenerator):
return code
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:
@ -191,6 +192,9 @@ class Index(DynastieGenerator):
f = open(filename, 'rb')
post_content = f.read()
f.close()
if post.content_format == Post.CONTENT_TEXT:
from dynastie.generators import markdown2
post_content = markdown2.markdown(post_content)
self.hash_posts_content[filename] = post_content
else:
post_content = self.hash_posts_content[filename]

2320
generators/markdown2.py Normal file

File diff suppressed because it is too large Load Diff

View File

@ -235,6 +235,12 @@ class Post(models.Model):
keywords = models.TextField(blank=True)
tags = models.ManyToManyField(Tag, blank=True, null=True)
blog = models.ForeignKey(Blog)
CONTENT_HTML = 0
CONTENT_TEXT = 1
CONTENT_FORMAT = (
(CONTENT_HTML, 'HTML'),
(CONTENT_TEXT, 'Text'))
content_format = models.IntegerField(choices=CONTENT_FORMAT, default=CONTENT_HTML, blank=False, null=False)
def getPath(self):
filename = '/post/'
@ -355,6 +361,13 @@ class Post(models.Model):
if os.path.exists(filename) and len(os.listdir(filename)) == 0:
os.rmdir(filename)
def get_editor(self):
if self.content_format == Post.CONTENT_HTML:
return 'html'
else:
return 'text'
class Comment(models.Model):
post = models.ForeignKey(Post)
parent = models.ForeignKey('self', null=True, blank=True)

View File

@ -549,3 +549,15 @@ div.all_posts div.post
margin-bottom:2px;
margin-left:20px;
}
.post_content > p > img
{
display:block;
margin-left: auto;
margin-right: auto;
}
.post_content > p
{
text-align:justify;
}

View File

@ -2,4 +2,9 @@
{
color:green;
font-weight:bold;
}
.markdown_help
{
padding-right:20px;
}

View File

@ -67,4 +67,22 @@ function addTag()
else
text_tags.value = cur_elem;
}
}
function switchEditor()
{
options = document.getElementById("editor");
help = document.getElementById("markdown_help");
if (options.selectedIndex == 0) // HTML
{
tinyMCE.execCommand('mceAddControl', false, 'content');
help.style.display="none";
// tinymce.execCommand('mceToggleEditor',true,'content');
} else // Text
{
tinyMCE.execCommand('mceRemoveControl', false, 'content');
help.style.display="block";
// tinymce.execCommand('mceToggleEditor',false,'content');
}
}

View File

@ -17,7 +17,69 @@ Available tags:
{% endfor %}
</select>
<input type="button" onclick="addTag();" value="Add Tag"/>
<textarea name="content" class="mceAdvanced"></textarea><br/><br/>
<br/>Editor <select name="editor" id="editor" onchange="switchEditor();">
{% if editor == "html" %}
<option value="html" selected="selected">HTML
<option value="text">Text
{% else %}
<option value="html">HTML
<option value="text" selected="selected">Text
{% endif %}
</select><br/>
{% if editor == "html" %}
<textarea id="content" name="content" cols="100" rows="25" class="mceAdvanced"></textarea>
{% else %}
<textarea id="content" name="content" cols="100" rows="25"></textarea>
{% endif %}
<br/>
<input type="submit" name="add" value="Add" /><input type="button" name="preview" value="Preview" onClick="previewPost({{ blog_id }});"/><input type="submit" name="cancel" value="Cancel" />
</form>
{% if editor == "html" %}
<div id="markdown_help" style="display:none">
{% else %}
<div id="markdown_help">
{% endif %}
<b>Markdown syntax</b><br /><br />
<table>
<tr>
<td class="markdown_help">
<pre style="display:inline">_italic_</pre> <span style="font-style:italic">italic</span><br/>
<pre style="display:inline">**bold**</pre> <span style="font-weight:bold">bold</span><br/>
<pre style="display:inline">~~line through~~</pre> <span style="text-decoration:line-through">line through</span><br/>
<pre style="display:inline">>Citation</pre><br/>
<pre>
* Unordered list
* Second element
</pre>
<ul>
<li>Unordered list
<li>Second element
</ul>
<pre>
1. Ordered list
1. Second element
</pre>
<ol>
<li>Ordered list
<li>Second element
</ol>
<pre style="display:inline">![Picture](https://bits.wikimedia.org/images/wikimedia-button.png)</pre><img src="https://bits.wikimedia.org/images/wikimedia-button.png" alt="Picture"/><br/>
<pre style="display:inline">[Link](http://www.wikipedia.org)</pre> <a href="http://www.wikipedia.org">Link</a><br/><br/>
</td>
<td>
<pre># Title # or
Title
=====</pre>
<h1>Title</h1>
<pre>## Sub title ## or
Sub title
---------</pre>
<h2>Sub title</h2>
<pre>### Sub sub title ###</pre>
<h3>Sub sub title</h3>
</td>
</tr>
</table>
</div>
<br/><br/>
{% endblock %}

View File

@ -17,7 +17,20 @@ Available tags:
{% endfor %}
</select>
<input type="button" onclick="addTag();" value="Add Tag"/>
<textarea name="content" class="mceAdvanced">{{ content }}</textarea>
<br/>Editor <select name="editor" id="editor" onchange="switchEditor();">
{% if editor == "html" %}
<option value="html" selected="selected">HTML
<option value="text">Text
{% else %}
<option value="html">HTML
<option value="text" selected="selected">Text
{% endif %}
</select><br/>
{% if editor == "html" %}
<textarea id="content" name="content" cols="100" rows="25" class="mceAdvanced">{{ content }}</textarea><br/>
{% else %}
<textarea id="content" name="content" cols="100" rows="25">{{ content }}</textarea><br/>
{% endif %}
<input type="submit" name="edit" value="Edit" /><input type="button" name="preview" value="Preview" onClick="previewPost({{ blog_id }});"/><input type="submit" name="cancel" value="Cancel" />
</form>
<div class="comments">
@ -32,5 +45,52 @@ Available tags:
{% endautoescape %}
</div>
{% endfor %}
</div><br/>
{% if editor == "html" %}
<div id="markdown_help" style="display:none">
{% else %}
<div id="markdown_help">
{% endif %}
<b>Markdown syntax</b><br /><br />
<table>
<tr>
<td class="markdown_help">
<pre style="display:inline">_italic_</pre> <span style="font-style:italic">italic</span><br/>
<pre style="display:inline">**bold**</pre> <span style="font-weight:bold">bold</span><br/>
<pre style="display:inline">~~line through~~</pre> <span style="text-decoration:line-through">line through</span><br/>
<pre style="display:inline">>Citation</pre><br/>
<pre>
* Unordered list
* Second element
</pre>
<ul>
<li>Unordered list
<li>Second element
</ul>
<pre>
1. Ordered list
1. Second element
</pre>
<ol>
<li>Ordered list
<li>Second element
</ol>
<pre style="display:inline">![Picture](https://bits.wikimedia.org/images/wikimedia-button.png)</pre><img src="https://bits.wikimedia.org/images/wikimedia-button.png" alt="Picture"/><br/>
<pre style="display:inline">[Link](http://www.wikipedia.org)</pre> <a href="http://www.wikipedia.org">Link</a><br/><br/>
</td>
<td>
<pre># Title # or
Title
=====</pre>
<h1>Title</h1>
<pre>## Sub title ## or
Sub title
---------</pre>
<h2>Sub title</h2>
<pre>### Sub sub title ###</pre>
<h3>Sub sub title</h3>
</td>
</tr>
</table>
</div>
{% endblock %}

View File

@ -22,7 +22,7 @@
</head>
<body>
<div class="logo">
<img src="{{ STATIC_URL }}images/logo.png"/>
<a href="http://indefero.soutade.fr/p/dynastie>"><img src="{{ STATIC_URL }}images/logo.png"/></a>
</div>
<div class="form">
<form method="post" action="/index">

View File

@ -415,7 +415,10 @@ def add_post(request, blog_id):
if request.method == 'POST':
if 'add' in request.POST:
post = Post(blog=Blog.objects.get(pk=blog_id), author=User.objects.get(pk=request.user.id), creation_date=datetime.now(), modification_date=datetime.now())
content_format = Post.CONTENT_HTML
if request.POST['editor'] == 'text':
content_format = Post.CONTENT_TEXT
post = Post(blog=Blog.objects.get(pk=blog_id), author=User.objects.get(pk=request.user.id), creation_date=datetime.now(), modification_date=datetime.now(), content_format=content_format)
content = request.POST['content']
# del request.POST['content']
form = PostForm(request.POST, instance=post)
@ -433,7 +436,8 @@ def add_post(request, blog_id):
return render(request, 'add_post.html', {
'form': form, 'blog_id' : blog_id,
'all_tags' : Tag.objects.all()
'all_tags' : Tag.objects.all(),
'editor' : 'html'
})
@login_required
@ -445,6 +449,10 @@ def edit_post(request, post_id):
if request.method == 'POST':
if 'edit' in request.POST:
content_format = Post.CONTENT_HTML
if request.POST['editor'] == 'text':
content_format = Post.CONTENT_TEXT
post.content_format = content_format
form = PostForm(request.POST, instance=post)
if form.is_valid():
if title != request.POST['title']:
@ -472,10 +480,12 @@ def edit_post(request, post_id):
comment_list = []
for comment in comments:
comment_list.append(comment)
return render(request, 'edit_post.html', {
'form': form, 'post_id' : post_id, 'content' : content,
'blog_id' : blog_id, 'comments' : comment_list,
'all_tags' : Tag.objects.all()
'all_tags' : Tag.objects.all(),
'editor' : post.get_editor()
})
@login_required
@ -564,7 +574,7 @@ def search(request, blog_id):
c = {'result' : res}
# Simple wrapper to HTML content
# Simple wrapper to HTML content
return render(request, 'templates/search.html', c)
@login_required
@ -573,9 +583,14 @@ def preview(request, blog_id):
(b, p) = have_I_right(request, blog_id)
content = request.POST['content']
if request.POST['editor'] == 'text':
from dynastie.generators import markdown2
content = markdown2.markdown(content)
values = {'title' : request.POST['title'], \
'author' : request.user.first_name + ' ' + request.user.last_name, \
'content' : request.POST['content']
'content' : content
}
engine = globals()['post']
@ -646,8 +661,8 @@ def tinymcelist_edit(request, post_id):
except Http404:
return HttpResponse('', content_type='application/x-javascript')
year = post.creation_date.year()
month = post.creation_date.month()
year = post.creation_date.year
month = post.creation_date.month
ret = _tinymcelist(request, b, year, month)