module documentation

HTML utilities suitable for global use.
Function conditional​_escape Similar to escape(), except that it doesn't operate on pre-escaped strings.
Function escape Return the given text with ampersands, quotes and angle brackets encoded for use in HTML.
Function format​_html Similar to str.format, but pass all arguments through conditional_escape(), and call mark_safe() on the result. This function should be used instead of str.format or % interpolation to build up small HTML fragments.
Function format​_html​_join A wrapper of format_html, for the common case of a group of arguments that need to be formatted using the same format string, and then joined using 'sep'. 'sep' is also passed through conditional_escape.
Function html​_safe A decorator that defines the __html__ method. This helps non-Django templates to detect classes whose __str__ methods return SafeString.
Function strip​_tags Return the given HTML with all tags stripped.
Constant DOTS Undocumented
Constant TRAILING​_PUNCTUATION​_CHARS Undocumented
Constant WRAPPING​_PUNCTUATION Undocumented
Variable simple​_url​_2​_re Undocumented
Variable simple​_url​_re Undocumented
Variable word​_split​_re Undocumented
Class ​MLStripper Undocumented
Function ​_strip​_once Internal tag stripping utility used by strip_tags.
Function avoid​_wrapping Avoid text wrapping in the middle of a phrase by adding non-breaking spaces where there previously were normal spaces.
Function escapejs Hex encode characters for use in JavaScript strings.
Function json​_script Escape all the HTML/XML special characters with their unicode escapes, so value is safe to be output anywhere except for inside a tag attribute. Wrap the escaped JSON in a script tag.
Function linebreaks Convert newlines into <p> and <br>s.
Function smart​_urlquote Quote a URL if it isn't already quoted.
Function strip​_spaces​_between​_tags Return the given HTML with spaces between tags removed.
Function urlize Convert any URLs in text into clickable links.
Variable ​_js​_escapes Undocumented
Variable ​_json​_script​_escapes Undocumented
def conditional_escape(text):

Similar to escape(), except that it doesn't operate on pre-escaped strings.

This function relies on the __html__ convention used both by Django's SafeData class and by third-party libraries like markupsafe.

@keep_lazy(str, SafeString)
def escape(text):

Return the given text with ampersands, quotes and angle brackets encoded for use in HTML.

Always escape input, even if it's already escaped and marked as such. This may result in double-escaping. If this is a concern, use conditional_escape() instead.

def format_html(format_string, *args, **kwargs):
Similar to str.format, but pass all arguments through conditional_escape(), and call mark_safe() on the result. This function should be used instead of str.format or % interpolation to build up small HTML fragments.
def format_html_join(sep, format_string, args_generator):

A wrapper of format_html, for the common case of a group of arguments that need to be formatted using the same format string, and then joined using 'sep'. 'sep' is also passed through conditional_escape.

'args_generator' should be an iterator that returns the sequence of 'args' that will be passed to format_html.

Example:

format_html_join('
', "<li>{} {}</li>", ((u.first_name, u.last_name)
for u in users))
def html_safe(klass):
A decorator that defines the __html__ method. This helps non-Django templates to detect classes whose __str__ methods return SafeString.
@keep_lazy_text
def strip_tags(value):
Return the given HTML with all tags stripped.
DOTS: list[str] =

Undocumented

Value
['&middot;', '*', '', '&#149;', '&bull;', '&#8226;']
TRAILING_PUNCTUATION_CHARS: str =

Undocumented

Value
'.,:;!'
WRAPPING_PUNCTUATION: list =

Undocumented

Value
[('(', ')'), ('[', ']')]
simple_url_2_re =

Undocumented

simple_url_re =

Undocumented

word_split_re =

Undocumented

def _strip_once(value):
Internal tag stripping utility used by strip_tags.
def avoid_wrapping(value):
Avoid text wrapping in the middle of a phrase by adding non-breaking spaces where there previously were normal spaces.
@keep_lazy(str, SafeString)
def escapejs(value):
Hex encode characters for use in JavaScript strings.
def json_script(value, element_id):
Escape all the HTML/XML special characters with their unicode escapes, so value is safe to be output anywhere except for inside a tag attribute. Wrap the escaped JSON in a script tag.
@keep_lazy_text
def linebreaks(value, autoescape=False):
Convert newlines into <p> and <br>s.
def smart_urlquote(url):
Quote a URL if it isn't already quoted.
@keep_lazy_text
def strip_spaces_between_tags(value):
Return the given HTML with spaces between tags removed.
@keep_lazy_text
def urlize(text, trim_url_limit=None, nofollow=False, autoescape=False):

Convert any URLs in text into clickable links.

Works on http://, https://, www. links, and also on links ending in one of the original seven gTLDs (.com, .edu, .gov, .int, .mil, .net, and .org). Links can have trailing 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, truncate the URLs in the link text longer than this limit to trim_url_limit - 1 characters and append an ellipsis.

If nofollow is True, give the links a rel="nofollow" attribute.

If autoescape is True, autoescape the link text and URLs.

_js_escapes =

Undocumented

_json_script_escapes =

Undocumented