I wanted to keep the flexibility of generic_markup but add my own filtering option for apply_markup. So I created app/templatetags/mm_markup.py.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | from template_utils.templatetags.generic_markup import apply_markup, register
from template_utils.markup import formatter
from django.template import Library
def mm_markdown(text, **kwargs):
"""
do some media trickery before applying markdown like parsing ![File:123]
"""
import markdown
from journalism_mm.tutorials.models import File
import re
# Fixup the source text
text = text.strip()
text = text.replace("\r\n", "\n").replace("\r", "\n")
text += "\n\n"
# Split into lines and run the preprocessors that will work with
# self.lines
lines = text.split("\n")
for i in range(len(lines)) :
if lines[i].startswith('![File:'):
try:
f=File.objects.get(id=lines[i].replace(']','').split(':')[1])
except File.DoesNotExist:
lines[i]='<p>File Not Found</p>'
pass
else :
if re.search('(\.gif|\.png|\.jpg|\.jpeg)', f.file):
lines[i]='<img src="/media/%s" title="%s" alt="%s" />' % (f.file, f.title, f.alt)
else :
lines[i]='no mimetype match'
text = '\n'.join(lines)
return markdown.markdown(text, **kwargs)
formatter.register('mm_markdown',mm_markdown)
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 3 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.