Login

Filter for adding quote marks

Author:
olau
Posted:
November 27, 2007
Language:
Python
Version:
.96
Tags:
templatetag quotes
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. filter for extracting a number of paragraphs from any HTML code by rafadev 4 years, 1 month ago
  2. order_by template filter by marinho 7 years, 2 months ago
  3. E-mail quoting filters and tags by ChipX86 8 years, 1 month ago
  4. getattr template filter by joshua 8 years, 5 months ago
  5. Type checking templatetag filters by marcorogers 5 years, 5 months ago

Comments

Please login first before commenting.