diff --git a/editor/views.py b/editor/views.py index 7873046..260b9f9 100644 --- a/editor/views.py +++ b/editor/views.py @@ -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 @@ -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) @@ -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) diff --git a/forms.py b/forms.py index b427631..699b7b4 100644 --- a/forms.py +++ b/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): @@ -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 diff --git a/markdown/mdx_code.py b/markdown/mdx_code.py index 402c50e..8a6e6f8 100644 --- a/markdown/mdx_code.py +++ b/markdown/mdx_code.py @@ -16,7 +16,7 @@ def extendMarkdown(self, md, md_global): md.textPreprocessors.insert(0, preprocessor) -CODE_BLOCK_REGEX = re.compile(r"\r?\n(?P[ ]*)(?P^@{2,})[[ ]*(?P[a-zA-Z0-9_+-]+)[ ]*(?P[a-zA-Z]*)[ ]*\r?\n(?P.*?)(?P=fence)[ ]*\r?\n", re.DOTALL | re.MULTILINE) +CODE_BLOCK_REGEX = re.compile(r"\r?\n(?P[ ]*)(?P@{2,})[[ ]*(?P[a-zA-Z0-9_+-]+)[ ]*(?P[a-zA-Z]*)[ ]*\r?\n(?P.*?)(?P=fence)[ ]*\r?\n", re.DOTALL | re.MULTILINE) class CodeBlockPreprocessor : def run (self, text): diff --git a/models.py b/models.py index c5359dc..cde9556 100644 --- a/models.py +++ b/models.py @@ -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): @@ -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() @@ -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() diff --git a/templates/lifeflow/comment.html b/templates/lifeflow/comment.html index 55341cf..2a7ea99 100644 --- a/templates/lifeflow/comment.html +++ b/templates/lifeflow/comment.html @@ -11,10 +11,10 @@

You are writing a comment about {{

You are responding to this comment written by {{ parent.name }} on {{ parent.date|date:"F jS Y, H:i" }}.

{{ parent.html|safe }}

{% endif %} -{% if form.cleaned_data.rendered %} +{% if form.cleaned_data.html %}

Preview of your comment:

-{{ form.cleaned_data.rendered|safe }} +{{ form.cleaned_data.html|safe }} {% endif %}

Please be aware that comment forms go stale after one hour.

diff --git a/text_filters.py b/text_filters.py new file mode 100644 index 0000000..cb01c0d --- /dev/null +++ b/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,) diff --git a/views.py b/views.py index b6b2392..2633282 100644 --- a/views.py +++ b/views.py @@ -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)