diff --git a/docs/clean.rst b/docs/clean.rst index 68178ce5..c786f2a3 100644 --- a/docs/clean.rst +++ b/docs/clean.rst @@ -63,10 +63,10 @@ For example: >>> import bleach >>> bleach.clean( - ... u'an example', + ... 'an example', ... tags=['b'], ... ) - u'<i>an example</i>' + '<i>an example</i>' The default value is a relatively conservative list found in @@ -106,12 +106,12 @@ For example: >>> import bleach >>> bleach.clean( - ... u'

blah blah blah

', + ... '

blah blah blah

', ... tags=['p'], ... attributes=['style'], ... styles=['color'], ... ) - u'

blah blah blah

' + '

blah blah blah

' As a dict @@ -135,11 +135,11 @@ and "class" for any tag (including "a" and "img"): ... } >>> bleach.clean( - ... u'an example', + ... 'an example', ... tags=['img'], ... attributes=attrs ... ) - u'an example' + 'an example' Using functions @@ -161,11 +161,11 @@ For example: ... return name[0] == 'h' >>> bleach.clean( - ... u'link', + ... 'link', ... tags=['a'], ... attributes=allow_h, ... ) - u'link' + 'link' You can also pass a callable as a value in an attributes dict and it'll run for @@ -173,7 +173,7 @@ attributes for specified tags: .. doctest:: - >>> from urlparse import urlparse + >>> from six.moves.urllib.parse import urlparse >>> import bleach >>> def allow_src(tag, name, value): @@ -185,13 +185,13 @@ attributes for specified tags: ... return False >>> bleach.clean( - ... u'an example', + ... 'an example', ... tags=['img'], ... attributes={ ... 'img': allow_src ... } ... ) - u'an example' + 'an example' .. versionchanged:: 2.0 @@ -223,12 +223,12 @@ For example, to allow users to set the color and font-weight of text: >>> styles = ['color', 'font-weight'] >>> bleach.clean( - ... u'

my html

', + ... '

my html

', ... tags=tags, ... attributes=attrs, ... styles=styles ... ) - u'

my html

' + '

my html

' Default styles are stored in ``bleach.sanitizer.ALLOWED_STYLES``. @@ -252,7 +252,7 @@ For example, this sets allowed protocols to http, https and smb: ... 'allowed protocol', ... protocols=['http', 'https', 'smb'] ... ) - u'allowed protocol' + 'allowed protocol' This adds smb to the Bleach-specified set of allowed protocols: @@ -265,7 +265,7 @@ This adds smb to the Bleach-specified set of allowed protocols: ... 'allowed protocol', ... protocols=bleach.ALLOWED_PROTOCOLS + ['smb'] ... ) - u'allowed protocol' + 'allowed protocol' Default protocols are in ``bleach.sanitizer.ALLOWED_PROTOCOLS``. @@ -284,10 +284,10 @@ and invalid markup. For example: >>> import bleach >>> bleach.clean('is not allowed') - u'<span>is not allowed</span>' + '<span>is not allowed</span>' >>> bleach.clean('is not allowed', tags=['b']) - u'<span>is not allowed</span>' + '<span>is not allowed</span>' If you would rather Bleach stripped this markup entirely, you can pass @@ -298,10 +298,10 @@ If you would rather Bleach stripped this markup entirely, you can pass >>> import bleach >>> bleach.clean('is not allowed', strip=True) - u'is not allowed' + 'is not allowed' >>> bleach.clean('is not allowed', tags=['b'], strip=True) - u'is not allowed' + 'is not allowed' Stripping comments (``strip_comments``) @@ -317,10 +317,10 @@ By default, Bleach will strip out HTML comments. To disable this behavior, set >>> html = 'my html' >>> bleach.clean(html) - u'my html' + 'my html' >>> bleach.clean(html, strip_comments=False) - u'my html' + 'my html' Using ``bleach.sanitizer.Cleaner`` @@ -353,7 +353,7 @@ Trivial Filter example: .. doctest:: >>> from bleach.sanitizer import Cleaner - >>> from html5lib.filters.base import Filter + >>> from bleach.html5lib_shim import Filter >>> class MooFilter(Filter): ... def __iter__(self): @@ -371,7 +371,7 @@ Trivial Filter example: >>> cleaner = Cleaner(tags=TAGS, attributes=ATTRS, filters=[MooFilter]) >>> dirty = 'this is cute! ' >>> cleaner.clean(dirty) - u'this is cute! ' + 'this is cute! ' .. Warning:: diff --git a/docs/linkify.rst b/docs/linkify.rst index b8e7884e..b5d9d20f 100644 --- a/docs/linkify.rst +++ b/docs/linkify.rst @@ -80,12 +80,12 @@ For example, you could add a ``title`` attribute to all links: >>> from bleach.linkifier import Linker >>> def set_title(attrs, new=False): - ... attrs[(None, u'title')] = u'link in user text' + ... attrs[(None, 'title')] = 'link in user text' ... return attrs ... >>> linker = Linker(callbacks=[set_title]) >>> linker.linkify('abc http://example.com def') - u'abc http://example.com def' + 'abc http://example.com def' This would set the value of the ``rel`` attribute, stomping on a previous value @@ -96,21 +96,21 @@ an external link: .. doctest:: - >>> from urlparse import urlparse + >>> from six.moves.urllib.parse import urlparse >>> from bleach.linkifier import Linker >>> def set_target(attrs, new=False): - ... p = urlparse(attrs[(None, u'href')]) + ... p = urlparse(attrs[(None, 'href')]) ... if p.netloc not in ['my-domain.com', 'other-domain.com']: - ... attrs[(None, u'target')] = u'_blank' - ... attrs[(None, u'class')] = u'external' + ... attrs[(None, 'target')] = '_blank' + ... attrs[(None, 'class')] = 'external' ... else: - ... attrs.pop((None, u'target'), None) + ... attrs.pop((None, 'target'), None) ... return attrs ... >>> linker = Linker(callbacks=[set_target]) >>> linker.linkify('abc http://example.com def') - u'abc http://example.com def' + 'abc http://example.com def' Removing Attributes @@ -127,17 +127,17 @@ sanitizing attributes.) >>> def allowed_attrs(attrs, new=False): ... """Only allow href, target, rel and title.""" ... allowed = [ - ... (None, u'href'), - ... (None, u'target'), - ... (None, u'rel'), - ... (None, u'title'), - ... u'_text', + ... (None, 'href'), + ... (None, 'target'), + ... (None, 'rel'), + ... (None, 'title'), + ... '_text', ... ] ... return dict((k, v) for k, v in attrs.items() if k in allowed) ... >>> linker = Linker(callbacks=[allowed_attrs]) >>> linker.linkify('link') - u'link' + 'link' Or you could remove a specific attribute, if it exists: @@ -147,15 +147,15 @@ Or you could remove a specific attribute, if it exists: >>> from bleach.linkifier import Linker >>> def remove_title(attrs, new=False): - ... attrs.pop((None, u'title'), None) + ... attrs.pop((None, 'title'), None) ... return attrs ... >>> linker = Linker(callbacks=[remove_title]) >>> linker.linkify('link') - u'link' + 'link' >>> linker.linkify('link') - u'link' + 'link' Altering Attributes @@ -177,14 +177,14 @@ Example of shortening link text: ... if not new: ... return attrs ... # _text will be the same as the URL for new links - ... text = attrs[u'_text'] + ... text = attrs['_text'] ... if len(text) > 25: - ... attrs[u'_text'] = text[0:22] + u'...' + ... attrs['_text'] = text[0:22] + '...' ... return attrs ... >>> linker = Linker(callbacks=[shorten_url]) >>> linker.linkify('http://example.com/longlonglonglonglongurl') - u'http://example.com/lon...' + 'http://example.com/lon...' Example of switching all links to go through a bouncer first: @@ -196,7 +196,7 @@ Example of switching all links to go through a bouncer first: >>> def outgoing_bouncer(attrs, new=False): ... """Send outgoing links through a bouncer.""" - ... href_key = (None, u'href') + ... href_key = (None, 'href') ... p = urlparse(attrs.get(href_key, None)) ... if p.netloc not in ['example.com', 'www.example.com', '']: ... bouncer = 'http://bn.ce/?destination=%s' @@ -205,10 +205,10 @@ Example of switching all links to go through a bouncer first: ... >>> linker = Linker(callbacks=[outgoing_bouncer]) >>> linker.linkify('http://example.com') - u'http://example.com' + 'http://example.com' >>> linker.linkify('http://foo.com') - u'http://foo.com' + 'http://foo.com' Preventing Links @@ -230,7 +230,7 @@ write the following callback: ... return attrs ... # If the TLD is '.py', make sure it starts with http: or https:. ... # Use _text because that's the original text - ... link_text = attrs[u'_text'] + ... link_text = attrs['_text'] ... if link_text.endswith('.py') and not link_text.startswith(('http:', 'https:')): ... # This looks like a Python file, not a URL. Don't make a link. ... return None @@ -239,10 +239,10 @@ write the following callback: ... >>> linker = Linker(callbacks=[dont_linkify_python]) >>> linker.linkify('abc http://example.com def') - u'abc http://example.com def' + 'abc http://example.com def' >>> linker.linkify('abc models.py def') - u'abc models.py def' + 'abc models.py def' .. _Crate: https://crate.io/ @@ -261,13 +261,13 @@ For example, this removes any ``mailto:`` links: >>> from bleach.linkifier import Linker >>> def remove_mailto(attrs, new=False): - ... if attrs[(None, u'href')].startswith(u'mailto:'): + ... if attrs[(None, 'href')].startswith('mailto:'): ... return None ... return attrs ... >>> linker = Linker(callbacks=[remove_mailto]) >>> linker.linkify('mail janet!') - u'mail janet!' + 'mail janet!' Skipping links in specified tag blocks (``skip_tags``) @@ -308,7 +308,7 @@ instance. >>> linker = Linker(skip_tags=['pre']) >>> linker.linkify('a b c http://example.com d e f') - u'a b c http://example.com d e f' + 'a b c http://example.com d e f' .. autoclass:: bleach.linkifier.Linker @@ -340,11 +340,11 @@ For example, using all the defaults: >>> cleaner = Cleaner(tags=['pre']) >>> cleaner.clean('
http://example.com
') - u'
http://example.com
' + '
http://example.com
' >>> cleaner = Cleaner(tags=['pre'], filters=[LinkifyFilter]) >>> cleaner.clean('
http://example.com
') - u'
http://example.com
' + '
http://example.com
' And passing parameters to ``LinkifyFilter``: @@ -362,7 +362,7 @@ And passing parameters to ``LinkifyFilter``: ... ) ... >>> cleaner.clean('
http://example.com
') - u'
http://example.com
' + '
http://example.com
' .. autoclass:: bleach.linkifier.LinkifyFilter