import re @register.filter def forumFormat (value): allowedhtml = ['br', 'strong', 'b', 'p', 'div', 'em', 'u', 'strike', 'ul', 'li', 'ol', 'a', 'img', 'highlight', 'sup', 'sub', 'span', 'big', 'small', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'h7', 'h8', 'pre', 'address', 'code', 'kbd', 'samp', 'var', 'del', 'ins', 'cite', 'q', 'bdo'] ret = "" ok = False closed = True for i in range(0, len(value)): c = value[i:i + 1] if c == '<': if closed: ok = False for a in allowedhtml: if re.search("^\s*" + a, value[i + 1:], re.I) or re.search("^/\s*" + a, value[i + 1:], re.I): ok = True closed = False break if not ok: c = "<" else: c = "<" elif c == ">": if ok: ok = False closed = True else: c = ">" ret += c return ret