Skip to content

Commit

Permalink
Cleaned up a couple unused imports and fixed docstrings to follow Pyt…
Browse files Browse the repository at this point in the history
…hon Style Guide.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5717 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
gdub committed Jul 16, 2007
1 parent f6ced03 commit 7aac81d
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions django/utils/html.py
Expand Up @@ -2,8 +2,8 @@


import re import re
import string import string
import urllib
from django.utils.encoding import force_unicode, smart_str from django.utils.encoding import force_unicode
from django.utils.functional import allow_lazy from django.utils.functional import allow_lazy


# Configuration for urlize() function # Configuration for urlize() function
Expand All @@ -26,44 +26,45 @@
del x # Temporary variable del x # Temporary variable


def escape(html): def escape(html):
"Returns the given HTML with ampersands, quotes and carets encoded" "Return the given HTML with ampersands, quotes and carets encoded."
return force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;') return force_unicode(html).replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;').replace('"', '&quot;').replace("'", '&#39;')
escape = allow_lazy(escape, unicode) escape = allow_lazy(escape, unicode)


def linebreaks(value): def linebreaks(value):
"Converts newlines into <p> and <br />s" "Convert newlines into <p> and <br />s."
value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines value = re.sub(r'\r\n|\r|\n', '\n', force_unicode(value)) # normalize newlines
paras = re.split('\n{2,}', value) paras = re.split('\n{2,}', value)
paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras] paras = [u'<p>%s</p>' % p.strip().replace('\n', '<br />') for p in paras]
return u'\n\n'.join(paras) return u'\n\n'.join(paras)
linebreaks = allow_lazy(linebreaks, unicode) linebreaks = allow_lazy(linebreaks, unicode)


def strip_tags(value): def strip_tags(value):
"Returns the given HTML with all tags stripped" "Return the given HTML with all tags stripped."
return re.sub(r'<[^>]*?>', '', force_unicode(value)) return re.sub(r'<[^>]*?>', '', force_unicode(value))
strip_tags = allow_lazy(strip_tags) strip_tags = allow_lazy(strip_tags)


def strip_spaces_between_tags(value): def strip_spaces_between_tags(value):
"Returns the given HTML with spaces between tags removed" "Return the given HTML with spaces between tags removed."
return re.sub(r'>\s+<', '><', force_unicode(value)) return re.sub(r'>\s+<', '><', force_unicode(value))
strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode) strip_spaces_between_tags = allow_lazy(strip_spaces_between_tags, unicode)


def strip_entities(value): def strip_entities(value):
"Returns the given HTML with all entities (&something;) stripped" "Return the given HTML with all entities (&something;) stripped."
return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value)) return re.sub(r'&(?:\w+|#\d+);', '', force_unicode(value))
strip_entities = allow_lazy(strip_entities, unicode) strip_entities = allow_lazy(strip_entities, unicode)


def fix_ampersands(value): def fix_ampersands(value):
"Returns the given HTML with all unencoded ampersands encoded correctly" "Return the given HTML with all unencoded ampersands encoded correctly."
return unencoded_ampersands_re.sub('&amp;', force_unicode(value)) return unencoded_ampersands_re.sub('&amp;', force_unicode(value))
fix_ampersands = allow_lazy(fix_ampersands, unicode) fix_ampersands = allow_lazy(fix_ampersands, unicode)


def urlize(text, trim_url_limit=None, nofollow=False): def urlize(text, trim_url_limit=None, nofollow=False):
""" """
Converts any URLs in text into clickable links. Works on http://, https:// Convert any URLs in text into clickable links.
and www. links. Links can have trailing punctuation (periods, commas,
close-parens) and leading punctuation (opening parens) and it'll still do Works on http://, https://, and www. links. Links can have trailing
the right thing. 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 longer than this limit If trim_url_limit is not None, the URLs in link text longer than this limit
will truncated to trim_url_limit-3 characters and appended with an elipsis. will truncated to trim_url_limit-3 characters and appended with an elipsis.
Expand Down Expand Up @@ -94,14 +95,14 @@ def urlize(text, trim_url_limit=None, nofollow=False):


def clean_html(text): def clean_html(text):
""" """
Cleans the given HTML. Specifically, it does the following: Clean the given HTML. Specifically, do the following:
* Converts <b> and <i> to <strong> and <em>. * Convert <b> and <i> to <strong> and <em>.
* Encodes all ampersands correctly. * Encode all ampersands correctly.
* Removes all "target" attributes from <a> tags. * Remove all "target" attributes from <a> tags.
* Removes extraneous HTML, such as presentational tags that open and * Remove extraneous HTML, such as presentational tags that open and
immediately close and <br clear="all">. immediately close and <br clear="all">.
* Converts hard-coded bullets into HTML unordered lists. * Convert hard-coded bullets into HTML unordered lists.
* Removes stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the * Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the
bottom of the text. bottom of the text.
""" """
from django.utils.text import normalize_newlines from django.utils.text import normalize_newlines
Expand All @@ -120,8 +121,8 @@ def replace_p_tags(match):
s = s.replace('<p>%s' % d, '<li>') s = s.replace('<p>%s' % d, '<li>')
return u'<ul>\n%s\n</ul>' % s return u'<ul>\n%s\n</ul>' % s
text = hard_coded_bullets_re.sub(replace_p_tags, text) text = hard_coded_bullets_re.sub(replace_p_tags, text)
# Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom of the text. # Remove stuff like "<p>&nbsp;&nbsp;</p>", but only if it's at the bottom
# of the text.
text = trailing_empty_content_re.sub('', text) text = trailing_empty_content_re.sub('', text)
return text return text
clean_html = allow_lazy(clean_html, unicode) clean_html = allow_lazy(clean_html, unicode)

0 comments on commit 7aac81d

Please sign in to comment.