From a5b6e901830123909cafab93f32b8589d16a0cc1 Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Mon, 21 Oct 2013 02:36:19 +0200 Subject: [PATCH] Add insert_code_chunks template filter This basic version actually removes them. Addition to be coming later --- blog/templates/blog/post.html | 3 --- blog/templatetags/code_chunks.py | 45 +++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/blog/templates/blog/post.html b/blog/templates/blog/post.html index 39b1779..0934ff1 100644 --- a/blog/templates/blog/post.html +++ b/blog/templates/blog/post.html @@ -1,10 +1,7 @@

{% if title_links %}{% endif %}{{ post.title }}{% if title_links %}{% else %}
{% endif %}

- {% comment %} {{ post.content|insert_code_chunks }} - {% endcomment %} - {{ post.content|safe }} {% if post.tags.all %}
" + hilite(match.group('lang'), chunk.content) + "
" + except CodeChunk.DoesNotExist: + newstr = "" + + newlen = len(newstr) + value = value[:end] + newstr + value[start:] + diff += newlen - oldlen + + p = re.compile('\[\$ code:(?P[^:]+):(?P.+?) \$\]', re.DOTALL) + i = p.finditer(value) + diff = 0 + + for match in i: + end, start = match.span() + oldlen = start - end + start += diff + end += diff + + # TODO: This is an ugly hack, as it includes template logic in code. BAD! + newstr = "
" + hilite(match.group('lang'), match.group('code')) + "
" + + newlen = len(newstr) + value = value[:end] + newstr + value[start:] + diff += newlen - oldlen + + return mark_safe(value) + @register.filter(needs_autoescape=True) @stringfilter def syhilite(value, language, autoescape=None):