2005-09-03 02:51:14 +08:00
|
|
|
"HTML utilities suitable for global use."
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
import re, string
|
|
|
|
|
|
|
|
# Configuration for urlize() function
|
|
|
|
LEADING_PUNCTUATION = ['(', '<', '<']
|
|
|
|
TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>']
|
|
|
|
|
|
|
|
# list of possible strings used for bullets in bulleted lists
|
|
|
|
DOTS = ['·', '*', '\xe2\x80\xa2', '•', '•', '•']
|
|
|
|
|
2005-09-03 02:51:14 +08:00
|
|
|
unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
|
|
|
|
word_split_re = re.compile(r'(\s+)')
|
|
|
|
punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
|
2006-01-19 09:06:12 +08:00
|
|
|
('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]),
|
|
|
|
'|'.join([re.escape(x) for x in TRAILING_PUNCTUATION])))
|
2005-09-03 02:51:14 +08:00
|
|
|
simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
|
|
|
|
link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+')
|
|
|
|
html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
|
2006-01-19 09:06:12 +08:00
|
|
|
hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL)
|
2005-09-03 02:51:14 +08:00
|
|
|
trailing_empty_content_re = re.compile(r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z')
|
2006-01-19 09:06:12 +08:00
|
|
|
del x # Temporary variable
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def escape(html):
|
|
|
|
"Returns the given HTML with ampersands, quotes and carets encoded"
|
|
|
|
if not isinstance(html, basestring):
|
|
|
|
html = str(html)
|
2006-05-02 09:31:56 +08:00
|
|
|
return html.replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def linebreaks(value):
|
|
|
|
"Converts newlines into <p> and <br />s"
|
|
|
|
value = re.sub(r'\r\n|\r|\n', '\n', value) # normalize newlines
|
|
|
|
paras = re.split('\n{2,}', value)
|
|
|
|
paras = ['<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
|
|
|
|
return '\n\n'.join(paras)
|
|
|
|
|
|
|
|
def strip_tags(value):
|
|
|
|
"Returns the given HTML with all tags stripped"
|
|
|
|
return re.sub(r'<[^>]*?>', '', value)
|
|
|
|
|
2006-01-15 09:51:30 +08:00
|
|
|
def strip_spaces_between_tags(value):
|
2006-01-15 13:18:17 +08:00
|
|
|
"Returns the given HTML with spaces between tags normalized to a single space"
|
|
|
|
return re.sub(r'>\s+<', '> <', value)
|
2006-01-15 09:51:30 +08:00
|
|
|
|
2005-07-13 09:25:57 +08:00
|
|
|
def strip_entities(value):
|
|
|
|
"Returns the given HTML with all entities (&something;) stripped"
|
|
|
|
return re.sub(r'&(?:\w+|#\d);', '', value)
|
|
|
|
|
|
|
|
def fix_ampersands(value):
|
|
|
|
"Returns the given HTML with all unencoded ampersands encoded correctly"
|
2005-09-03 02:51:14 +08:00
|
|
|
return unencoded_ampersands_re.sub('&', value)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def urlize(text, trim_url_limit=None, nofollow=False):
|
|
|
|
"""
|
|
|
|
Converts any URLs in text into clickable links. Works on http://, https:// and
|
|
|
|
www. links. Links can have trailing punctuation (periods, commas, close-parens)
|
|
|
|
and leading punctuation (opening parens) and it'll still do the right thing.
|
|
|
|
|
|
|
|
If trim_url_limit is not None, the URLs in link text will be limited to
|
|
|
|
trim_url_limit characters.
|
|
|
|
|
|
|
|
If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.
|
|
|
|
"""
|
|
|
|
trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or '')) or x
|
2005-09-03 02:51:14 +08:00
|
|
|
words = word_split_re.split(text)
|
2005-07-13 09:25:57 +08:00
|
|
|
nofollow_attr = nofollow and ' rel="nofollow"' or ''
|
|
|
|
for i, word in enumerate(words):
|
2005-09-03 02:51:14 +08:00
|
|
|
match = punctuation_re.match(word)
|
2005-07-13 09:25:57 +08:00
|
|
|
if match:
|
|
|
|
lead, middle, trail = match.groups()
|
|
|
|
if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') and \
|
|
|
|
len(middle) > 0 and middle[0] in string.letters + string.digits and \
|
|
|
|
(middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))):
|
|
|
|
middle = '<a href="http://%s"%s>%s</a>' % (middle, nofollow_attr, trim_url(middle))
|
|
|
|
if middle.startswith('http://') or middle.startswith('https://'):
|
|
|
|
middle = '<a href="%s"%s>%s</a>' % (middle, nofollow_attr, trim_url(middle))
|
|
|
|
if '@' in middle and not middle.startswith('www.') and not ':' in middle \
|
2005-09-03 02:51:14 +08:00
|
|
|
and simple_email_re.match(middle):
|
2005-07-13 09:25:57 +08:00
|
|
|
middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
|
|
|
|
if lead + middle + trail != word:
|
|
|
|
words[i] = lead + middle + trail
|
|
|
|
return ''.join(words)
|
|
|
|
|
|
|
|
def clean_html(text):
|
|
|
|
"""
|
|
|
|
Cleans the given HTML. Specifically, it does the following:
|
|
|
|
* Converts <b> and <i> to <strong> and <em>.
|
|
|
|
* Encodes all ampersands correctly.
|
|
|
|
* Removes all "target" attributes from <a> tags.
|
|
|
|
* Removes extraneous HTML, such as presentational tags that open and
|
|
|
|
immediately close and <br clear="all">.
|
|
|
|
* Converts hard-coded bullets into HTML unordered lists.
|
|
|
|
* Removes stuff like "<p> </p>", but only if it's at the
|
|
|
|
bottom of the text.
|
|
|
|
"""
|
|
|
|
from django.utils.text import normalize_newlines
|
|
|
|
text = normalize_newlines(text)
|
|
|
|
text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text)
|
|
|
|
text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
|
|
|
|
text = fix_ampersands(text)
|
|
|
|
# Remove all target="" attributes from <a> tags.
|
2005-09-03 02:51:14 +08:00
|
|
|
text = link_target_attribute_re.sub('\\1', text)
|
2005-07-13 09:25:57 +08:00
|
|
|
# Trim stupid HTML such as <br clear="all">.
|
2005-09-03 02:51:14 +08:00
|
|
|
text = html_gunk_re.sub('', text)
|
2005-07-13 09:25:57 +08:00
|
|
|
# Convert hard-coded bullets into HTML unordered lists.
|
|
|
|
def replace_p_tags(match):
|
|
|
|
s = match.group().replace('</p>', '</li>')
|
|
|
|
for d in DOTS:
|
|
|
|
s = s.replace('<p>%s' % d, '<li>')
|
|
|
|
return '<ul>\n%s\n</ul>' % s
|
2005-09-03 02:51:14 +08:00
|
|
|
text = hard_coded_bullets_re.sub(replace_p_tags, text)
|
2005-07-13 09:25:57 +08:00
|
|
|
# Remove stuff like "<p> </p>", but only if it's at the bottom of the text.
|
2005-09-03 02:51:14 +08:00
|
|
|
text = trailing_empty_content_re.sub('', text)
|
2005-07-13 09:25:57 +08:00
|
|
|
return text
|
|
|
|
|