Add insert_code_chunks template filter
This basic version actually removes them. Addition to be coming later
This commit is contained in:
parent
ef2791dde3
commit
a5b6e90183
@ -1,10 +1,7 @@
|
||||
<div class="post">
|
||||
<h3>{% if title_links %}<a href="{% url 'blog:read' post.created_at.year post.created_at.month post.created_at.day post.slug %}">{% endif %}{{ post.title }}{% if title_links %}</a>{% else %}<div class="plusone-container"><div class="g-plusone" data-annotation="inline" data-size="small" data-width="300"></div></div>{% endif %}</h3>
|
||||
<p class="article-date">{{ post.created_at|date:'m-d-Y :: H:i' }} by {{ post.user.first_name }} {{ post.user.last_name }}</p>
|
||||
{% comment %}
|
||||
{{ post.content|insert_code_chunks }}
|
||||
{% endcomment %}
|
||||
{{ post.content|safe }}
|
||||
{% if post.tags.all %}
|
||||
<p class="article-tags">Tags:
|
||||
{% for tag in post.tags.all %}
|
||||
|
@ -1,11 +1,12 @@
|
||||
from django import template
|
||||
from django.template.defaultfilters import stringfilter
|
||||
from django.utils.safestring import mark_safe
|
||||
import logging
|
||||
import logging, re
|
||||
from pygments import highlight
|
||||
from pygments.lexers import get_lexer_by_name
|
||||
from pygments.formatters import HtmlFormatter
|
||||
from gergelypolonkaiweb.solarized_dark import SolarizedDarkStyle
|
||||
from blog.models import CodeChunk
|
||||
|
||||
register = template.Library()
|
||||
|
||||
@ -27,6 +28,48 @@ def hilite(language, code):
|
||||
formatter = CodeFormatter(style = SolarizedDarkStyle, linenos = False, cssclass = language + " code", noclasses = True)
|
||||
return highlight(code, lexer, formatter)
|
||||
|
||||
@register.filter(needs_autoescape=True)
|
||||
@stringfilter
|
||||
def insert_code_chunks(value, autoescape=None):
|
||||
p = re.compile('\[\$ code:(?P<lang>[^:]+):(?P<slug>[^ ]+) \$\]')
|
||||
i = p.finditer(value)
|
||||
diff = 0
|
||||
|
||||
for match in i:
|
||||
end, start = match.span()
|
||||
oldlen = start - end
|
||||
start += diff
|
||||
end += diff
|
||||
try:
|
||||
chunk = CodeChunk.objects.get(language = match.group('lang'), slug = match.group('slug'))
|
||||
# TODO: This is an ugly hack, as it includes template logic in code. BAD!
|
||||
newstr = "<div class=\"code-chunk\">" + hilite(match.group('lang'), chunk.content) + "</div>"
|
||||
except CodeChunk.DoesNotExist:
|
||||
newstr = ""
|
||||
|
||||
newlen = len(newstr)
|
||||
value = value[:end] + newstr + value[start:]
|
||||
diff += newlen - oldlen
|
||||
|
||||
p = re.compile('\[\$ code:(?P<lang>[^:]+):(?P<code>.+?) \$\]', 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 = "<div class=\"code-chunk\">" + hilite(match.group('lang'), match.group('code')) + "</div>"
|
||||
|
||||
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):
|
||||
|
Loading…
Reference in New Issue
Block a user