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"“" + text[len(s):]
match = end_paragraph.search(text)
if match:
s = match.group()
else:
s = u""
text = text[:len(text)-len(s)] + u"”" + s
print match
return text
|
More like this
- Template tag - list punctuation for a list of items by shapiromatron 10 months, 2 weeks ago
- JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
- Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
- Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
- Help text hyperlinks by sa2812 1 year, 6 months ago
Comments
Please login first before commenting.