Ticket #429: html.py.2.patch

File html.py.2.patch, 4.5 KB (added by pb@…, 19 years ago)
  • django_src/django/utils/html.py

     
    1 "Useful HTML utilities suitable for global use by World Online projects."
     1"""
     2HTML utilities suitable for global use.
     3"""
    24
    35import re, string
    46
     
    911# list of possible strings used for bullets in bulleted lists
    1012DOTS = ['·', '*', '\xe2\x80\xa2', '•', '•', '•']
    1113
    12 UNENCODED_AMPERSANDS_RE = re.compile(r'&(?!(\w+|#\d+);)')
    13 WORD_SPLIT_RE = re.compile(r'(\s+)')
    14 PUNCTUATION_RE = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
     14unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)')
     15word_split_re = re.compile(r'(\s+)')
     16punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \
    1517    ('|'.join([re.escape(p) for p in LEADING_PUNCTUATION]),
    1618    '|'.join([re.escape(p) for p in TRAILING_PUNCTUATION])))
    17 SIMPLE_EMAIL_RE = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
    18 LINK_TARGET_ATTRIBUTE = re.compile(r'(<a [^>]*?)target=[^\s>]+')
    19 HTML_GUNK = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
    20 HARD_CODED_BULLETS = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(d) for d in DOTS]), re.DOTALL)
    21 TRAILING_EMPTY_CONTENT = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
     19simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
     20link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+')
     21html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
     22hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(d) for d in DOTS]), re.DOTALL)
     23trailing_empty_content_re = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
    2224
    2325def escape(html):
    2426    "Returns the given HTML with ampersands, quotes and carets encoded"
     
    4345
    4446def fix_ampersands(value):
    4547    "Returns the given HTML with all unencoded ampersands encoded correctly"
    46     return UNENCODED_AMPERSANDS_RE.sub('&amp;', value)
     48    return unencoded_ampersands_re.sub('&amp;', value)
    4749
    4850def urlize(text, trim_url_limit=None, nofollow=False):
    4951    """
     
    5759    If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.
    5860    """
    5961    trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or ''))  or x
    60     words = WORD_SPLIT_RE.split(text)
     62    words = word_split_re.split(text)
    6163    nofollow_attr = nofollow and ' rel="nofollow"' or ''
    6264    for i, word in enumerate(words):
    63         match = PUNCTUATION_RE.match(word)
     65        match = punctuation_re.match(word)
    6466        if match:
    6567            lead, middle, trail = match.groups()
    6668            if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') and \
     
    7072            if middle.startswith('http://') or middle.startswith('https://'):
    7173                middle = '<a href="%s"%s>%s</a>' % (middle, nofollow_attr, trim_url(middle))
    7274            if '@' in middle and not middle.startswith('www.') and not ':' in middle \
    73                 and SIMPLE_EMAIL_RE.match(middle):
     75                and simple_email_re.match(middle):
    7476                middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
    7577            if lead + middle + trail != word:
    7678                words[i] = lead + middle + trail
     
    9496    text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
    9597    text = fix_ampersands(text)
    9698    # Remove all target="" attributes from <a> tags.
    97     text = LINK_TARGET_ATTRIBUTE.sub('\\1', text)
     99    text = link_target_attribute_re.sub('\\1', text)
    98100    # Trim stupid HTML such as <br clear="all">.
    99     text = HTML_GUNK.sub('', text)
     101    text = html_gunk_re.sub('', text)
    100102    # Convert hard-coded bullets into HTML unordered lists.
    101103    def replace_p_tags(match):
    102104        s = match.group().replace('</p>', '</li>')
    103105        for d in DOTS:
    104106            s = s.replace('<p>%s' % d, '<li>')
    105107        return '<ul>\n%s\n</ul>' % s
    106     text = HARD_CODED_BULLETS.sub(replace_p_tags, text)
     108    text = hard_coded_bullets_re.sub(replace_p_tags, text)
    107109    # Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom of the text.
    108     text = TRAILING_EMPTY_CONTENT.sub('', text)
     110    text = trailing_empty_content_re.sub('', text)
    109111    return text
    110112
Back to Top