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*$", 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