Login

Filter for adding quote marks

Author:
olau
Posted:
November 27, 2007
Language:
Python
Version:
.96
Score:
0 (after 0 ratings)

A template filter for adding curly quotes around a string. The filter understands enough HTML to put the quotes inside an initial paragraph begin and ending paragraph end, if they exist.

Put the code inside a file in a templatetags subdir in your app, add a {% load myfile %} statement and you're ready to go with {{somebodysays|addquotes}}. Of course, beware the script kiddies, be careful with escaping.

 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
from django import template

register = template.Library()

import re

@register.filter
def addquotes(text):
    text = unicode(text)

    start_paragraph = re.compile(r"^\s*<\s*p\s*>\s*", re.UNICODE | re.IGNORECASE)
    end_paragraph = re.compile(r"\s*</\s*p\s*>\s*$", re.UNICODE | re.IGNORECASE)

    match = start_paragraph.search(text)
    if match:
        s = match.group()
    else:
        s = u""
    text = s + u"&#8220;" + text[len(s):]

    match = end_paragraph.search(text)
    if match:
        s = match.group()
    else:
        s = u""
    text = text[:len(text)-len(s)] + u"&#8221;" + s
    print match
        
    return text

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

Please login first before commenting.