Skip to content

Instantly share code, notes, and snippets.

@avdg
Created June 7, 2012 15:44
Show Gist options
  • Save avdg/2889552 to your computer and use it in GitHub Desktop.
Save avdg/2889552 to your computer and use it in GitHub Desktop.
# Note htmlFilter is WiP (Work in Progress) ;-)
import cgi
buf_re = {
'basicTags': re.compile( \
r'<(' \
r'/?(b|c(ite|ode)|d([dlt]|el)|em|h[0-6]|i(ns)?|li|marquee|ol|p|' \
r's(mall|tr(ike|ong)|u[bp])?|t(able|[dhrt])|u[lm]?)|[bh]r'
r')\s*>'),
'closingComplexTags': re.compile('</(a)&\s*gt;'),
'optSpaces': re.compile(r'\s*'),
}
def htmlFilter(content):
# 1. Escape
content = cgi.escape(content)
# 2. Find basic tags
# 2.1 Basic tags without attributes
content = buf_re['basicTags'].sub(r'<\1>', content)
# 2.2 Special cases
content = content \
.replace('\n', '<br>') \
.replace('&lt;!--', '<!--') \
.replace('--&gt;', '-->')
return content
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment