comparison planemo/lib/python3.7/site-packages/bleach/__init__.py @ 0:d30785e31577 draft

"planemo upload commit 6eee67778febed82ddd413c3ca40b3183a3898f1"
author guerler
date Fri, 31 Jul 2020 00:18:57 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d30785e31577
1 # -*- coding: utf-8 -*-
2
3 from __future__ import unicode_literals
4
5 import packaging.version
6
7 from bleach.linkifier import (
8 DEFAULT_CALLBACKS,
9 Linker,
10 )
11 from bleach.sanitizer import (
12 ALLOWED_ATTRIBUTES,
13 ALLOWED_PROTOCOLS,
14 ALLOWED_STYLES,
15 ALLOWED_TAGS,
16 Cleaner,
17 )
18
19
20 # yyyymmdd
21 __releasedate__ = '20200429'
22 # x.y.z or x.y.z.dev0 -- semver
23 __version__ = '3.1.5'
24 VERSION = packaging.version.Version(__version__)
25
26
27 __all__ = ['clean', 'linkify']
28
29
30 def clean(text, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES,
31 styles=ALLOWED_STYLES, protocols=ALLOWED_PROTOCOLS, strip=False,
32 strip_comments=True):
33 """Clean an HTML fragment of malicious content and return it
34
35 This function is a security-focused function whose sole purpose is to
36 remove malicious content from a string such that it can be displayed as
37 content in a web page.
38
39 This function is not designed to use to transform content to be used in
40 non-web-page contexts.
41
42 Example::
43
44 import bleach
45
46 better_text = bleach.clean(yucky_text)
47
48
49 .. Note::
50
51 If you're cleaning a lot of text and passing the same argument values or
52 you want more configurability, consider using a
53 :py:class:`bleach.sanitizer.Cleaner` instance.
54
55 :arg str text: the text to clean
56
57 :arg list tags: allowed list of tags; defaults to
58 ``bleach.sanitizer.ALLOWED_TAGS``
59
60 :arg dict attributes: allowed attributes; can be a callable, list or dict;
61 defaults to ``bleach.sanitizer.ALLOWED_ATTRIBUTES``
62
63 :arg list styles: allowed list of css styles; defaults to
64 ``bleach.sanitizer.ALLOWED_STYLES``
65
66 :arg list protocols: allowed list of protocols for links; defaults
67 to ``bleach.sanitizer.ALLOWED_PROTOCOLS``
68
69 :arg bool strip: whether or not to strip disallowed elements
70
71 :arg bool strip_comments: whether or not to strip HTML comments
72
73 :returns: cleaned text as unicode
74
75 """
76 cleaner = Cleaner(
77 tags=tags,
78 attributes=attributes,
79 styles=styles,
80 protocols=protocols,
81 strip=strip,
82 strip_comments=strip_comments,
83 )
84 return cleaner.clean(text)
85
86
87 def linkify(text, callbacks=DEFAULT_CALLBACKS, skip_tags=None, parse_email=False):
88 """Convert URL-like strings in an HTML fragment to links
89
90 This function converts strings that look like URLs, domain names and email
91 addresses in text that may be an HTML fragment to links, while preserving:
92
93 1. links already in the string
94 2. urls found in attributes
95 3. email addresses
96
97 linkify does a best-effort approach and tries to recover from bad
98 situations due to crazy text.
99
100 .. Note::
101
102 If you're linking a lot of text and passing the same argument values or
103 you want more configurability, consider using a
104 :py:class:`bleach.linkifier.Linker` instance.
105
106 .. Note::
107
108 If you have text that you want to clean and then linkify, consider using
109 the :py:class:`bleach.linkifier.LinkifyFilter` as a filter in the clean
110 pass. That way you're not parsing the HTML twice.
111
112 :arg str text: the text to linkify
113
114 :arg list callbacks: list of callbacks to run when adjusting tag attributes;
115 defaults to ``bleach.linkifier.DEFAULT_CALLBACKS``
116
117 :arg list skip_tags: list of tags that you don't want to linkify the
118 contents of; for example, you could set this to ``['pre']`` to skip
119 linkifying contents of ``pre`` tags
120
121 :arg bool parse_email: whether or not to linkify email addresses
122
123 :returns: linkified text as unicode
124
125 """
126 linker = Linker(
127 callbacks=callbacks,
128 skip_tags=skip_tags,
129 parse_email=parse_email
130 )
131 return linker.linkify(text)