Changeset 5717
- Timestamp:
- 07/16/07 00:28:13 (1 year ago)
- Files:
-
- django/trunk/django/utils/html.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/trunk/django/utils/html.py
r5701 r5717 3 3 import re 4 4 import string 5 import urllib 6 from django.utils.encoding import force_unicode , smart_str5 6 from django.utils.encoding import force_unicode 7 7 from django.utils.functional import allow_lazy 8 8 … … 27 27 28 28 def escape(html): 29 "Return s the given HTML with ampersands, quotes and carets encoded"29 "Return the given HTML with ampersands, quotes and carets encoded." 30 30 return force_unicode(html).replace('&', '&').replace('<', '<').replace('>', '>').replace('"', '"').replace("'", ''') 31 31 escape = allow_lazy(escape, unicode) 32 32 33 33 def linebreaks(value): 34 "Convert s newlines into <p> and <br />s"34 "Convert newlines into <p> and <br />s." 35 35 value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines 36 36 paras = re.split('\n{2,}', value) … … 40 40 41 41 def strip_tags(value): 42 "Return s the given HTML with all tags stripped"42 "Return the given HTML with all tags stripped." 43 43 return re.sub(r'<[^>]*?>', '', force_unicode(value)) 44 44 strip_tags = allow_lazy(strip_tags) 45 45 46 46 def strip_spaces_between_tags(value): 47 "Return s the given HTML with spaces between tags removed"47 "Return the given HTML with spaces between tags removed." 48 48 return re.sub(r'>\s+<', '><', force_unicode(value)) 49 49 strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode) 50 50 51 51 def strip_entities(value): 52 "Return s the given HTML with all entities (&something;) stripped"52 "Return the given HTML with all entities (&something;) stripped." 53 53 return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value)) 54 54 strip_entities = allow_lazy(strip_entities, unicode) 55 55 56 56 def fix_ampersands(value): 57 "Return s the given HTML with all unencoded ampersands encoded correctly"57 "Return the given HTML with all unencoded ampersands encoded correctly." 58 58 return unencoded_ampersands_re.sub('&', force_unicode(value)) 59 59 fix_ampersands = allow_lazy(fix_ampersands, unicode) … … 61 61 def urlize(text, trim_url_limit=None, nofollow=False): 62 62 """ 63 Converts any URLs in text into clickable links. Works on http://, https:// 64 and www. links. Links can have trailing punctuation (periods, commas, 65 close-parens) and leading punctuation (opening parens) and it'll still do 66 the right thing. 63 Convert any URLs in text into clickable links. 64 65 Works on http://, https://, and www. links. Links can have trailing 66 punctuation (periods, commas, close-parens) and leading punctuation 67 (opening parens) and it'll still do the right thing. 67 68 68 69 If trim_url_limit is not None, the URLs in link text longer than this limit … … 95 96 def clean_html(text): 96 97 """ 97 Clean s the given HTML. Specifically, it doesthe following:98 * Convert s<b> and <i> to <strong> and <em>.99 * Encode sall ampersands correctly.100 * Remove sall "target" attributes from <a> tags.101 * Remove sextraneous HTML, such as presentational tags that open and98 Clean the given HTML. Specifically, do the following: 99 * Convert <b> and <i> to <strong> and <em>. 100 * Encode all ampersands correctly. 101 * Remove all "target" attributes from <a> tags. 102 * Remove extraneous HTML, such as presentational tags that open and 102 103 immediately close and <br clear="all">. 103 * Convert shard-coded bullets into HTML unordered lists.104 * Remove sstuff like "<p> </p>", but only if it's at the104 * Convert hard-coded bullets into HTML unordered lists. 105 * Remove stuff like "<p> </p>", but only if it's at the 105 106 bottom of the text. 106 107 """ … … 121 122 return u'<ul>\n%s\n</ul>' % s 122 123 text = hard_coded_bullets_re.sub(replace_p_tags, text) 123 # Remove stuff like "<p> </p>", but only if it's at the bottom of the text. 124 # Remove stuff like "<p> </p>", but only if it's at the bottom 125 # of the text. 124 126 text = trailing_empty_content_re.sub('', text) 125 127 return text 126 128 clean_html = allow_lazy(clean_html, unicode) 127
