@@ -80,12 +80,12 @@ For example, you could add a ``title`` attribute to all links:
80
80
>>> from bleach.linkifier import Linker
81
81
82
82
>>> def set_title (attrs , new = False ):
83
- ... attrs[(None , u ' title' )] = u ' link in user text'
83
+ ... attrs[(None , ' title' )] = ' link in user text'
84
84
... return attrs
85
85
...
86
86
>>> linker = Linker(callbacks = [set_title])
87
87
>>> linker.linkify(' abc http://example.com def' )
88
- u 'abc <a href="http://example.com" title="link in user text">http://example.com</a> def'
88
+ 'abc <a href="http://example.com" title="link in user text">http://example.com</a> def'
89
89
90
90
91
91
This would set the value of the ``rel `` attribute, stomping on a previous value
@@ -96,21 +96,21 @@ an external link:
96
96
97
97
.. doctest ::
98
98
99
- >>> from urlparse import urlparse
99
+ >>> from six.moves.urllib.parse import urlparse
100
100
>>> from bleach.linkifier import Linker
101
101
102
102
>>> def set_target (attrs , new = False ):
103
- ... p = urlparse(attrs[(None , u ' href' )])
103
+ ... p = urlparse(attrs[(None , ' href' )])
104
104
... if p.netloc not in [' my-domain.com' , ' other-domain.com' ]:
105
- ... attrs[(None , u ' target' )] = u ' _blank'
106
- ... attrs[(None , u ' class' )] = u ' external'
105
+ ... attrs[(None , ' target' )] = ' _blank'
106
+ ... attrs[(None , ' class' )] = ' external'
107
107
... else :
108
- ... attrs.pop((None , u ' target' ), None )
108
+ ... attrs.pop((None , ' target' ), None )
109
109
... return attrs
110
110
...
111
111
>>> linker = Linker(callbacks = [set_target])
112
112
>>> linker.linkify(' abc http://example.com def' )
113
- u 'abc <a class="external" href="http://example.com" target="_blank">http://example.com</a> def'
113
+ 'abc <a class="external" href="http://example.com" target="_blank">http://example.com</a> def'
114
114
115
115
116
116
Removing Attributes
@@ -127,17 +127,17 @@ sanitizing attributes.)
127
127
>>> def allowed_attrs (attrs , new = False ):
128
128
... """ Only allow href, target, rel and title."""
129
129
... allowed = [
130
- ... (None , u ' href' ),
131
- ... (None , u ' target' ),
132
- ... (None , u ' rel' ),
133
- ... (None , u ' title' ),
134
- ... u ' _text' ,
130
+ ... (None , ' href' ),
131
+ ... (None , ' target' ),
132
+ ... (None , ' rel' ),
133
+ ... (None , ' title' ),
134
+ ... ' _text' ,
135
135
... ]
136
136
... return dict ((k, v) for k, v in attrs.items() if k in allowed)
137
137
...
138
138
>>> linker = Linker(callbacks = [allowed_attrs])
139
139
>>> linker.linkify(' <a style="font-weight: super bold;" href="http://example.com">link</a>' )
140
- u '<a href="http://example.com">link</a>'
140
+ '<a href="http://example.com">link</a>'
141
141
142
142
143
143
Or you could remove a specific attribute, if it exists:
@@ -147,15 +147,15 @@ Or you could remove a specific attribute, if it exists:
147
147
>>> from bleach.linkifier import Linker
148
148
149
149
>>> def remove_title (attrs , new = False ):
150
- ... attrs.pop((None , u ' title' ), None )
150
+ ... attrs.pop((None , ' title' ), None )
151
151
... return attrs
152
152
...
153
153
>>> linker = Linker(callbacks = [remove_title])
154
154
>>> linker.linkify(' <a href="http://example.com">link</a>' )
155
- u '<a href="http://example.com">link</a>'
155
+ '<a href="http://example.com">link</a>'
156
156
157
157
>>> linker.linkify(' <a title="bad title" href="http://example.com">link</a>' )
158
- u '<a href="http://example.com">link</a>'
158
+ '<a href="http://example.com">link</a>'
159
159
160
160
161
161
Altering Attributes
@@ -177,14 +177,14 @@ Example of shortening link text:
177
177
... if not new:
178
178
... return attrs
179
179
... # _text will be the same as the URL for new links
180
- ... text = attrs[u ' _text' ]
180
+ ... text = attrs[' _text' ]
181
181
... if len (text) > 25 :
182
- ... attrs[u ' _text' ] = text[0 :22 ] + u ' ...'
182
+ ... attrs[' _text' ] = text[0 :22 ] + ' ...'
183
183
... return attrs
184
184
...
185
185
>>> linker = Linker(callbacks = [shorten_url])
186
186
>>> linker.linkify(' http://example.com/longlonglonglonglongurl' )
187
- u '<a href="http://example.com/longlonglonglonglongurl">http://example.com/lon...</a>'
187
+ '<a href="http://example.com/longlonglonglonglongurl">http://example.com/lon...</a>'
188
188
189
189
190
190
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:
196
196
197
197
>>> def outgoing_bouncer (attrs , new = False ):
198
198
... """ Send outgoing links through a bouncer."""
199
- ... href_key = (None , u ' href' )
199
+ ... href_key = (None , ' href' )
200
200
... p = urlparse(attrs.get(href_key, None ))
201
201
... if p.netloc not in [' example.com' , ' www.example.com' , ' ' ]:
202
202
... bouncer = ' http://bn.ce/?destination=%s '
@@ -205,10 +205,10 @@ Example of switching all links to go through a bouncer first:
205
205
...
206
206
>>> linker = Linker(callbacks = [outgoing_bouncer])
207
207
>>> linker.linkify(' http://example.com' )
208
- u '<a href="http://example.com">http://example.com</a>'
208
+ '<a href="http://example.com">http://example.com</a>'
209
209
210
210
>>> linker.linkify(' http://foo.com' )
211
- u '<a href="http://bn.ce/?destination=http%3A//foo.com">http://foo.com</a>'
211
+ '<a href="http://bn.ce/?destination=http%3A//foo.com">http://foo.com</a>'
212
212
213
213
214
214
Preventing Links
@@ -230,7 +230,7 @@ write the following callback:
230
230
... return attrs
231
231
... # If the TLD is '.py', make sure it starts with http: or https:.
232
232
... # Use _text because that's the original text
233
- ... link_text = attrs[u ' _text' ]
233
+ ... link_text = attrs[' _text' ]
234
234
... if link_text.endswith(' .py' ) and not link_text.startswith((' http:' , ' https:' )):
235
235
... # This looks like a Python file, not a URL. Don't make a link.
236
236
... return None
@@ -239,10 +239,10 @@ write the following callback:
239
239
...
240
240
>>> linker = Linker(callbacks = [dont_linkify_python])
241
241
>>> linker.linkify(' abc http://example.com def' )
242
- u 'abc <a href="http://example.com">http://example.com</a> def'
242
+ 'abc <a href="http://example.com">http://example.com</a> def'
243
243
244
244
>>> linker.linkify(' abc models.py def' )
245
- u 'abc models.py def'
245
+ 'abc models.py def'
246
246
247
247
248
248
.. _Crate : https://crate.io/
@@ -261,13 +261,13 @@ For example, this removes any ``mailto:`` links:
261
261
>>> from bleach.linkifier import Linker
262
262
263
263
>>> def remove_mailto (attrs , new = False ):
264
- ... if attrs[(None , u ' href' )].startswith(u ' mailto:' ):
264
+ ... if attrs[(None , ' href' )].startswith(' mailto:' ):
265
265
... return None
266
266
... return attrs
267
267
...
268
268
>>> linker = Linker(callbacks = [remove_mailto])
269
269
>>>
linker.linkify(' <a href="mailto:[email protected] ">mail janet!</a>' )
270
- u 'mail janet!'
270
+ 'mail janet!'
271
271
272
272
273
273
Skipping links in specified tag blocks (``skip_tags ``)
@@ -308,7 +308,7 @@ instance.
308
308
309
309
>>> linker = Linker(skip_tags = [' pre' ])
310
310
>>> linker.linkify(' a b c http://example.com d e f' )
311
- u 'a b c <a href="http://example.com" rel="nofollow">http://example.com</a> d e f'
311
+ 'a b c <a href="http://example.com" rel="nofollow">http://example.com</a> d e f'
312
312
313
313
314
314
.. autoclass :: bleach.linkifier.Linker
@@ -340,11 +340,11 @@ For example, using all the defaults:
340
340
341
341
>>> cleaner = Cleaner(tags = [' pre' ])
342
342
>>> cleaner.clean(' <pre>http://example.com</pre>' )
343
- u '<pre>http://example.com</pre>'
343
+ '<pre>http://example.com</pre>'
344
344
345
345
>>> cleaner = Cleaner(tags = [' pre' ], filters = [LinkifyFilter])
346
346
>>> cleaner.clean(' <pre>http://example.com</pre>' )
347
- u '<pre><a href="http://example.com">http://example.com</a></pre>'
347
+ '<pre><a href="http://example.com">http://example.com</a></pre>'
348
348
349
349
350
350
And passing parameters to ``LinkifyFilter ``:
@@ -362,7 +362,7 @@ And passing parameters to ``LinkifyFilter``:
362
362
... )
363
363
...
364
364
>>> cleaner.clean(' <pre>http://example.com</pre>' )
365
- u '<pre>http://example.com</pre>'
365
+ '<pre>http://example.com</pre>'
366
366
367
367
368
368
.. autoclass :: bleach.linkifier.LinkifyFilter
0 commit comments