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