Serotonin Storm

source>template_utils>templatetags>generic_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
40
41
42
43
44
45
46
47
"""
Filters for converting plain text to HTML and enhancing the
typographic appeal of text on the Web.

"""


from django.conf import settings
from django.template import Library
from django.utils.safestring import mark_safe

from template_utils.markup import formatter


def apply_markup(value, arg=None):
    """
    Applies text-to-HTML conversion.
    
    Takes an optional argument to specify the name of a filter to use.
    
    """
    if arg is not None:
        return mark_safe(formatter(value, filter_name=arg))
    return mark_safe(formatter(value))
apply_markup.is_safe = True

def smartypants(value):
    """
    Applies SmartyPants to a piece of text, applying typographic
    niceties.
    
    Requires the Python SmartyPants library to be installed; see
    http://web.chad.org/projects/smartypants.py/
    
    """
    try:
        from smartypants import smartyPants
    except ImportError:
        if settings.DEBUG:
            raise template.TemplateSyntaxError("Error in smartypants filter: the Python smartypants module is not installed or could not be imported")
        return value
    else:
        return mark_safe(smartyPants(value))

register = Library()
register.filter(apply_markup)
register.filter(smartypants)