|
1 """HTML utilities suitable for global use.""" |
|
2 |
|
3 import re |
|
4 import string |
|
5 |
|
6 from django.utils.safestring import SafeData, mark_safe |
|
7 from django.utils.encoding import force_unicode |
|
8 from django.utils.functional import allow_lazy |
|
9 from django.utils.http import urlquote |
|
10 |
|
11 # Configuration for urlize() function. |
|
12 LEADING_PUNCTUATION = ['(', '<', '<'] |
|
13 TRAILING_PUNCTUATION = ['.', ',', ')', '>', '\n', '>'] |
|
14 |
|
15 # List of possible strings used for bullets in bulleted lists. |
|
16 DOTS = ['·', '*', '\xe2\x80\xa2', '•', '•', '•'] |
|
17 |
|
18 unencoded_ampersands_re = re.compile(r'&(?!(\w+|#\d+);)') |
|
19 word_split_re = re.compile(r'(\s+)') |
|
20 punctuation_re = re.compile('^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % \ |
|
21 ('|'.join([re.escape(x) for x in LEADING_PUNCTUATION]), |
|
22 '|'.join([re.escape(x) for x in TRAILING_PUNCTUATION]))) |
|
23 simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$') |
|
24 link_target_attribute_re = re.compile(r'(<a [^>]*?)target=[^\s>]+') |
|
25 html_gunk_re = re.compile(r'(?:<br clear="all">|<i><\/i>|<b><\/b>|<em><\/em>|<strong><\/strong>|<\/?smallcaps>|<\/?uppercase>)', re.IGNORECASE) |
|
26 hard_coded_bullets_re = re.compile(r'((?:<p>(?:%s).*?[a-zA-Z].*?</p>\s*)+)' % '|'.join([re.escape(x) for x in DOTS]), re.DOTALL) |
|
27 trailing_empty_content_re = re.compile(r'(?:<p>(?: |\s|<br \/>)*?</p>\s*)+\Z') |
|
28 del x # Temporary variable |
|
29 |
|
30 def escape(html): |
|
31 """ |
|
32 Returns the given HTML with ampersands, quotes and angle brackets encoded. |
|
33 """ |
|
34 return mark_safe(force_unicode(html).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''')) |
|
35 escape = allow_lazy(escape, unicode) |
|
36 |
|
37 def conditional_escape(html): |
|
38 """ |
|
39 Similar to escape(), except that it doesn't operate on pre-escaped strings. |
|
40 """ |
|
41 if isinstance(html, SafeData): |
|
42 return html |
|
43 else: |
|
44 return escape(html) |
|
45 |
|
46 def linebreaks(value, autoescape=False): |
|
47 """Converts newlines into <p> and <br />s.""" |
|
48 value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines |
|
49 paras = re.split('\n{2,}', value) |
|
50 if autoescape: |
|
51 paras = [u'<p>%s</p>' % escape(p).replace('\n', '<br />') for p in paras] |
|
52 else: |
|
53 paras = [u'<p>%s</p>' % p.replace('\n', '<br />') for p in paras] |
|
54 return u'\n\n'.join(paras) |
|
55 linebreaks = allow_lazy(linebreaks, unicode) |
|
56 |
|
57 def strip_tags(value): |
|
58 """Returns the given HTML with all tags stripped.""" |
|
59 return re.sub(r'<[^>]*?>', '', force_unicode(value)) |
|
60 strip_tags = allow_lazy(strip_tags) |
|
61 |
|
62 def strip_spaces_between_tags(value): |
|
63 """Returns the given HTML with spaces between tags removed.""" |
|
64 return re.sub(r'>\s+<', '><', force_unicode(value)) |
|
65 strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode) |
|
66 |
|
67 def strip_entities(value): |
|
68 """Returns the given HTML with all entities (&something;) stripped.""" |
|
69 return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value)) |
|
70 strip_entities = allow_lazy(strip_entities, unicode) |
|
71 |
|
72 def fix_ampersands(value): |
|
73 """Returns the given HTML with all unencoded ampersands encoded correctly.""" |
|
74 return unencoded_ampersands_re.sub('&', force_unicode(value)) |
|
75 fix_ampersands = allow_lazy(fix_ampersands, unicode) |
|
76 |
|
77 def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False): |
|
78 """ |
|
79 Converts any URLs in text into clickable links. |
|
80 |
|
81 Works on http://, https://, www. links and links ending in .org, .net or |
|
82 .com. Links can have trailing punctuation (periods, commas, close-parens) |
|
83 and leading punctuation (opening parens) and it'll still do the right |
|
84 thing. |
|
85 |
|
86 If trim_url_limit is not None, the URLs in link text longer than this limit |
|
87 will truncated to trim_url_limit-3 characters and appended with an elipsis. |
|
88 |
|
89 If nofollow is True, the URLs in link text will get a rel="nofollow" |
|
90 attribute. |
|
91 |
|
92 If autoescape is True, the link text and URLs will get autoescaped. |
|
93 """ |
|
94 trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x |
|
95 safe_input = isinstance(text, SafeData) |
|
96 words = word_split_re.split(force_unicode(text)) |
|
97 nofollow_attr = nofollow and ' rel="nofollow"' or '' |
|
98 for i, word in enumerate(words): |
|
99 match = None |
|
100 if '.' in word or '@' in word or ':' in word: |
|
101 match = punctuation_re.match(word) |
|
102 if match: |
|
103 lead, middle, trail = match.groups() |
|
104 # Make URL we want to point to. |
|
105 url = None |
|
106 if middle.startswith('http://') or middle.startswith('https://'): |
|
107 url = urlquote(middle, safe='/&=:;#?+*') |
|
108 elif middle.startswith('www.') or ('@' not in middle and \ |
|
109 middle and middle[0] in string.ascii_letters + string.digits and \ |
|
110 (middle.endswith('.org') or middle.endswith('.net') or middle.endswith('.com'))): |
|
111 url = urlquote('http://%s' % middle, safe='/&=:;#?+*') |
|
112 elif '@' in middle and not ':' in middle and simple_email_re.match(middle): |
|
113 url = 'mailto:%s' % middle |
|
114 nofollow_attr = '' |
|
115 # Make link. |
|
116 if url: |
|
117 trimmed = trim_url(middle) |
|
118 if autoescape and not safe_input: |
|
119 lead, trail = escape(lead), escape(trail) |
|
120 url, trimmed = escape(url), escape(trimmed) |
|
121 middle = '<a href="%s"%s>%s</a>' % (url, nofollow_attr, trimmed) |
|
122 words[i] = mark_safe('%s%s%s' % (lead, middle, trail)) |
|
123 else: |
|
124 if safe_input: |
|
125 words[i] = mark_safe(word) |
|
126 elif autoescape: |
|
127 words[i] = escape(word) |
|
128 elif safe_input: |
|
129 words[i] = mark_safe(word) |
|
130 elif autoescape: |
|
131 words[i] = escape(word) |
|
132 return u''.join(words) |
|
133 urlize = allow_lazy(urlize, unicode) |
|
134 |
|
135 def clean_html(text): |
|
136 """ |
|
137 Clean the given HTML. Specifically, do the following: |
|
138 * Convert <b> and <i> to <strong> and <em>. |
|
139 * Encode all ampersands correctly. |
|
140 * Remove all "target" attributes from <a> tags. |
|
141 * Remove extraneous HTML, such as presentational tags that open and |
|
142 immediately close and <br clear="all">. |
|
143 * Convert hard-coded bullets into HTML unordered lists. |
|
144 * Remove stuff like "<p> </p>", but only if it's at the |
|
145 bottom of the text. |
|
146 """ |
|
147 from django.utils.text import normalize_newlines |
|
148 text = normalize_newlines(force_unicode(text)) |
|
149 text = re.sub(r'<(/?)\s*b\s*>', '<\\1strong>', text) |
|
150 text = re.sub(r'<(/?)\s*i\s*>', '<\\1em>', text) |
|
151 text = fix_ampersands(text) |
|
152 # Remove all target="" attributes from <a> tags. |
|
153 text = link_target_attribute_re.sub('\\1', text) |
|
154 # Trim stupid HTML such as <br clear="all">. |
|
155 text = html_gunk_re.sub('', text) |
|
156 # Convert hard-coded bullets into HTML unordered lists. |
|
157 def replace_p_tags(match): |
|
158 s = match.group().replace('</p>', '</li>') |
|
159 for d in DOTS: |
|
160 s = s.replace('<p>%s' % d, '<li>') |
|
161 return u'<ul>\n%s\n</ul>' % s |
|
162 text = hard_coded_bullets_re.sub(replace_p_tags, text) |
|
163 # Remove stuff like "<p> </p>", but only if it's at the bottom |
|
164 # of the text. |
|
165 text = trailing_empty_content_re.sub('', text) |
|
166 return text |
|
167 clean_html = allow_lazy(clean_html, unicode) |