Login

extending generic_markup.apply_markup

Author:
mandric
Posted:
May 22, 2007
Language:
Python
Version:
.96
Tags:
markup markdown templatetags
Score:
0 (after 0 ratings)

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

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

Comments

Please login first before commenting.