extending generic_markup.apply_markup

 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

  1. Template filter implementing the Trac wiki markup language by simon 5 years, 7 months ago
  2. Parse TemplateTag Variables Safely by evan_schulz 5 years, 9 months ago
  3. "If in" templatetag by bitprophet 7 years ago
  4. Boolean Image Flag TemplateTag by MichaelAnckaert 5 years, 3 months ago
  5. grouper tag by visgean 2 years, 8 months ago

Comments

(Forgotten your password?)