Skip to content

Commit

Permalink
Reworked how comments and text are being rendered to be more flexible…
Browse files Browse the repository at this point in the history
… for user expansion.
  • Loading branch information
lethain committed Jul 9, 2008
1 parent 5827731 commit 8e3efc1
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 49 deletions.
10 changes: 8 additions & 2 deletions editor/views.py
Expand Up @@ -20,6 +20,7 @@
from django.shortcuts import render_to_response
from django.http import Http404, HttpResponseRedirect, HttpResponseServerError
from lifeflow.models import *
from lifeflow.text_filters import entry_markup
from django.http import HttpResponse
from django.contrib.auth.decorators import login_required
from django.conf import settings
Expand Down Expand Up @@ -545,8 +546,13 @@ def create_author(request,id=None):

@login_required
def render(request, model=None, id=None):
print request, id, model
if id is None and request.POST.has_key('pk'):
id = request.POST['pk']
print request, id

if id is None:
txt = dbc_markup(request.POST['txt'])
txt = entry_markup(request.POST['txt'])
else:
if model == u"draft":
obj = Draft.objects.get(pk=id)
Expand All @@ -555,7 +561,7 @@ def render(request, model=None, id=None):
elif model == u"project":
obj = Project.objects.get(pk=id)
if obj.use_markdown:
txt = dbc_markup(obj.body, obj)
txt = entry_markup(obj.body, obj)
else:
txt = obj.body
return HttpResponse(txt)
26 changes: 2 additions & 24 deletions forms.py
@@ -1,6 +1,6 @@
import cgi
from django import newforms as forms
from lifeflow.models import comment_markup
from lifeflow.text_filters import comment_markup


class CommentForm(forms.Form):
Expand Down Expand Up @@ -40,28 +40,6 @@ def clean_webpage(self):

def clean_body(self):
body = self.cleaned_data['body']
lines = body.split("\n")
new_lines = []
in_code = False

# at the moment you could disable all parsing simply by
# having initial @@'s but no closing @@'s. Instead this
# should only disable code escaping for code-syntax blocks
# if there is a closing @@ as well as an opening @@
# (admittedly, it may well crash the markdownpp if
# it is improperly formed... if thats any consulation)
for line in lines:
if line.startswith("@@") and in_code is True:
in_code = False
elif line.startswith("@@") and in_code is False:
in_code = True
if in_code is False:
if line.startswith(">"):
line = ">%s" % cgi.escape(line[1:])
else:
line = cgi.escape(line)
new_lines.append(line)
escaped = u"\n".join(new_lines)
self.cleaned_data['rendered'] = unicode(comment_markup(escaped))
self.cleaned_data['html'] = unicode(comment_markup(body))
return body

2 changes: 1 addition & 1 deletion markdown/mdx_code.py
Expand Up @@ -16,7 +16,7 @@ def extendMarkdown(self, md, md_global):
md.textPreprocessors.insert(0, preprocessor)


CODE_BLOCK_REGEX = re.compile(r"\r?\n(?P<spaces>[ ]*)(?P<fence>^@{2,})[[ ]*(?P<syntax>[a-zA-Z0-9_+-]+)[ ]*(?P<linenos>[a-zA-Z]*)[ ]*\r?\n(?P<code>.*?)(?P=fence)[ ]*\r?\n", re.DOTALL | re.MULTILINE)
CODE_BLOCK_REGEX = re.compile(r"\r?\n(?P<spaces>[ ]*)(?P<fence>@{2,})[[ ]*(?P<syntax>[a-zA-Z0-9_+-]+)[ ]*(?P<linenos>[a-zA-Z]*)[ ]*\r?\n(?P<code>.*?)(?P=fence)[ ]*\r?\n", re.DOTALL | re.MULTILINE)

class CodeBlockPreprocessor :
def run (self, text):
Expand Down
21 changes: 3 additions & 18 deletions models.py
Expand Up @@ -6,22 +6,7 @@
from django.dispatch import dispatcher
from django.db.models import signals
from django.core.mail import mail_admins
from lifeflow.markdown.markdown import Markdown
from lifeflow.markdown import mdx_lifeflow
from lifeflow.markdown import mdx_code
from lifeflow.markdown import mdx_footnotes
from lifeflow.markdown import mdx_foreign_formats

def dbc_markup(txt, obj=None):
"Apply Dynamic Blog Context markup"
exts = [mdx_footnotes,mdx_code,mdx_foreign_formats, mdx_lifeflow]
md = Markdown(txt,extensions=exts,extension_configs={'lifeflow':obj})
return md.convert()


def comment_markup(txt, obj=None):
md = Markdown(txt, extensions=[mdx_code])
return md.convert()
from lifeflow.text_filters import entry_markup, comment_markup


class Author(models.Model):
Expand Down Expand Up @@ -226,7 +211,7 @@ def get_absolute_url(self):

def save(self):
if self.use_markdown:
self.body_html = dbc_markup(self.body, self)
self.body_html = entry_markup(self.body, self)
else:
self.body_html = self.body
if self.send_ping is True: self.ping()
Expand Down Expand Up @@ -426,7 +411,7 @@ def get_absolute_url(self):

def save(self):
if self.use_markdown:
self.body_html = dbc_markup(self.body, self)
self.body_html = entry_markup(self.body, self)
else:
self.body_html = self.body
super(Project,self).save()
Expand Down
4 changes: 2 additions & 2 deletions templates/lifeflow/comment.html
Expand Up @@ -11,10 +11,10 @@ <h3> You are writing a comment about <a href="{{ object.get_absolute_url }}">{{
<h3> You are responding to <a href="{{ object.get_absolute_url }}#comment_{{ parent.id }}"> this comment</a> written by {{ parent.name }} on {{ parent.date|date:"F jS Y, H:i" }}.</h3>
<p> {{ parent.html|safe }} </p>
{% endif %}
{% if form.cleaned_data.rendered %}
{% if form.cleaned_data.html %}
<hr>
<h3> Preview of your comment: </h3>
{{ form.cleaned_data.rendered|safe }}
{{ form.cleaned_data.html|safe }}
{% endif %}
<hr>
<p> Please be aware that comment forms go stale after one hour.</p>
Expand Down
58 changes: 58 additions & 0 deletions text_filters.py
@@ -0,0 +1,58 @@
"""
This file contains filters which are used for pre and post
processing various kinds of text within LifeFlow.
Which values are applied is controlled by a number of global
variables within the project's settings.py file. These vars
are:
LIFEFLOW_ENTRY_FILTERS
LIFEFLOW_COMMENT_FILTERS
If you wish to add your own filters, you don't
have to add them to this file, they can exist anywhere, and
simply import them into the settings.py file and add them
to the appropriate global variable.
The API for these processing functions is very simple:
they accept two parameters, a string to process,
and optionally a related model.
"""

import cgi, re
from django.conf import settings
from lifeflow.markdown.markdown import Markdown
from lifeflow.markdown import mdx_lifeflow
from lifeflow.markdown import mdx_code
from lifeflow.markdown import mdx_footnotes
from lifeflow.markdown import mdx_foreign_formats



def comment_markup(txt,obj=None):
filters = getattr(settings,'LIFEFLOW_COMMENT_FILTERS', DEFAULT_COMMENT_FILTERS)
for filter in filters:
txt = filter(txt)
return txt

def entry_markup(txt,obj=None):
filters = getattr(settings,'LIFEFLOW_ENTRY_FILTERS', DEFAULT_ENTRY_FILTERS)
for filter in filters:
txt = filter(txt)
return txt


def comment_markdown(txt,obj=None):
exts = (mdx_code,)
md = Markdown(txt,extensions=exts,safe_mode=True)
return md.convert()


def entry_markdown(txt,obj=None):
exts = (mdx_code, mdx_footnotes,mdx_foreign_formats, mdx_lifeflow)
md = Markdown(txt,extensions=exts,extension_configs={'lifeflow':obj})
return md.convert()


DEFAULT_COMMENT_FILTERS = (comment_markdown,)
DEFAULT_ENTRY_FILTERS = (entry_markdown,)
4 changes: 2 additions & 2 deletions views.py
Expand Up @@ -82,10 +82,10 @@ def make_identifier(id, time):
name = form.cleaned_data['name']
email = form.cleaned_data['email']
webpage = form.cleaned_data['webpage']
rendered = form.cleaned_data['rendered']
html = form.cleaned_data['html']
body = form.cleaned_data['body']
c = Comment(entry=entry,parent=parent,name=name,email=email,
webpage=webpage,body=body,html=rendered)
webpage=webpage,body=body,html=html)
c.save()
url = u"%s#comment_%s" % (entry.get_absolute_url(), c.pk)
return HttpResponseRedirect(url)
Expand Down

0 comments on commit 8e3efc1

Please sign in to comment.