Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #429 -- Small cleanup to code in utils/html.py. Thanks, pb@e-sc…

…ribe.com

git-svn-id: http://code.djangoproject.com/svn/django/trunk@611 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 64d3da63ae87c0f2fb4e7a723bdfbc091bdeb7b4 1 parent 3357045
Adrian Holovaty authored September 02, 2005

Showing 1 changed file with 17 additions and 17 deletions. Show diff stats Hide diff stats

  1. 34  django/utils/html.py
34  django/utils/html.py
... ...
@@ -1,4 +1,4 @@
1  
-"Useful HTML utilities suitable for global use by World Online projects."
  1
+"HTML utilities suitable for global use."
2 2
 
3 3
 import re, string
4 4
 
@@ -9,16 +9,16 @@
9 9
 # list of possible strings used for bullets in bulleted lists
10 10
 DOTS = ['·', '*', '\xe2\x80\xa2', '•', '•', '•']
11 11
 
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)*)$' % \
  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)*)$' % \
15 15
     ('|'.join([re.escape(p) for p in LEADING_PUNCTUATION]),
16 16
     '|'.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')
  17
+simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
  18
+link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+')
  19
+html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE)
  20
+hard_coded_bullets_re = 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 = re.compile(r'(?:<p>(?:&nbsp;|\s|<br \/>)*?</p>\s*)+\Z')
22 22
 
23 23
 def escape(html):
24 24
     "Returns the given HTML with ampersands, quotes and carets encoded"
@@ -43,7 +43,7 @@ def strip_entities(value):
43 43
 
44 44
 def fix_ampersands(value):
45 45
     "Returns the given HTML with all unencoded ampersands encoded correctly"
46  
-    return UNENCODED_AMPERSANDS_RE.sub('&amp;', value)
  46
+    return unencoded_ampersands_re.sub('&amp;', value)
47 47
 
48 48
 def urlize(text, trim_url_limit=None, nofollow=False):
49 49
     """
@@ -57,10 +57,10 @@ def urlize(text, trim_url_limit=None, nofollow=False):
57 57
     If nofollow is True, the URLs in link text will get a rel="nofollow" attribute.
58 58
     """
59 59
     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)
  60
+    words = word_split_re.split(text)
61 61
     nofollow_attr = nofollow and ' rel="nofollow"' or ''
62 62
     for i, word in enumerate(words):
63  
-        match = PUNCTUATION_RE.match(word)
  63
+        match = punctuation_re.match(word)
64 64
         if match:
65 65
             lead, middle, trail = match.groups()
66 66
             if middle.startswith('www.') or ('@' not in middle and not middle.startswith('http://') and \
@@ -70,7 +70,7 @@ def urlize(text, trim_url_limit=None, nofollow=False):
70 70
             if middle.startswith('http://') or middle.startswith('https://'):
71 71
                 middle = '<a href="%s"%s>%s</a>' % (middle, nofollow_attr, trim_url(middle))
72 72
             if '@' in middle and not middle.startswith('www.') and not ':' in middle \
73  
-                and SIMPLE_EMAIL_RE.match(middle):
  73
+                and simple_email_re.match(middle):
74 74
                 middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
75 75
             if lead + middle + trail != word:
76 76
                 words[i] = lead + middle + trail
@@ -94,17 +94,17 @@ def clean_html(text):
94 94
     text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text)
95 95
     text = fix_ampersands(text)
96 96
     # Remove all target="" attributes from <a> tags.
97  
-    text = LINK_TARGET_ATTRIBUTE.sub('\\1', text)
  97
+    text = link_target_attribute_re.sub('\\1', text)
98 98
     # Trim stupid HTML such as <br clear="all">.
99  
-    text = HTML_GUNK.sub('', text)
  99
+    text = html_gunk_re.sub('', text)
100 100
     # Convert hard-coded bullets into HTML unordered lists.
101 101
     def replace_p_tags(match):
102 102
         s = match.group().replace('</p>', '</li>')
103 103
         for d in DOTS:
104 104
             s = s.replace('<p>%s' % d, '<li>')
105 105
         return '<ul>\n%s\n</ul>' % s
106  
-    text = HARD_CODED_BULLETS.sub(replace_p_tags, text)
  106
+    text = hard_coded_bullets_re.sub(replace_p_tags, text)
107 107
     # 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)
  108
+    text = trailing_empty_content_re.sub('', text)
109 109
     return text
110 110
 

0 notes on commit 64d3da6

Please sign in to comment.
Something went wrong with that request. Please try again.