linkifier /

Filename Size Date modified Message
77 B
165 B
867 B
1.1 KB
23 B
1.4 KB
4.4 KB
8.4 KB
1.9 KB

Python Library Documentation: module linkifier

linkifier - Tools for turning links in plain text into HTML links
linkify(text, substitutions=None, wordregex=r"([w@$#~](?:[w!*'();:@&=+$,/?#[]_.~%-]*[w@&=+$/#_~])?)")

Turn URLs in text into links and perform custom URL substitutions.

For example:

>>> linkify(' is pretty neat')
'<a href=""></a> is pretty neat'

If substitutions are specified, words that don't look like URLs can be turned into links:

>>> linkify('see #123 for more info', [(r'#(\d+)', r'\1')])
'see <a href="">#123</a> for more info'

If multiple substitutions are specified, they are performed in order until one matches:

>>> linkify('fixes PROJ-123',
...         [(r'#(\d+)', r'/issues/\1'),
...          (r'([A-Z]{4})-(\d+)', r'\1/\2')])
'fixes <a href="">PROJ-123</a>'

If a substitution would return text that isn't a URL, it's ignored and the remaining substitutions are processed:

>>> linkify('fixes PROJ-123',
...         [(r'.+', r'javascript:alert("boo!")'),
...          (r'([A-Z]{4})-(\d+)', r'\1/\2')])
'fixes <a href="">PROJ-123</a>'

Note that substitution patterns will only match words in their entirety. In other words, the pattern implicitly begins with '^' and implicitly ends with '$':

>>> linkify('X Y XX YY',
...         [(r'^X', r''),
...          (r'Y$', r'')])
'<a href="">X</a> <a href="">Y</a> XX YY'

A replacement function can be provided instead of a string. If the function raises ValueError, the substitution is ignored and the remaining substitutions are processed:

>>> def replace(match):
...     number =
...     if int(number) % 3 != 0:
...         raise ValueError
...     return '' + number
>>> linkify('5 6 7 8 (', [(r'\d+', replace)])
'5 <a href="">6</a> 7 8 (<a href=""></a>)'

For the daring programmer, wordregex can be customized to allow different word matching semantics:

>>> linkify(' #123', [('#(\d+)', r'/issues/\1')], r'(#\d+)')
' <a href="/issues/123">#123</a>'
linkifyword(word, text=None, requiredomain=True)

Turn a word into an HTML anchor if it looks like a URL.

For example:

>>> linkifyword('')
'<a href=""></a>'
>>> linkifyword('')
'<a href=""></a>'
>>> linkifyword('/about.html', requiredomain=False)
'<a href="/about.html">/about.html</a>'

If text is specified, it's used as the anchor text:

>>> linkifyword('', 'baz')
'<a href="">baz</a>'

If it doesn't look like a URL, ValueError is raised:

>>> linkifyword('localhost/blah')
Traceback (most recent call last):
ValueError: not a URL
urlizeword(word, requiredomain=True)

Turn word into a URL if it looks valid.

Example usage:

>>> urlizeword('http://foo!')
>>> urlizeword('https://bar!')
>>> urlizeword('')

If it doesn't look like a URL, ValueError is raised:

>>> urlizeword('localhost/blah.blah.blah')
Traceback (most recent call last):
ValueError: not a URL

If requiredomain is False, URLs starting with a slash are allowed:

>>> urlizeword('/foo/bar', requiredomain=False)
__all__ = ['urlizeword', 'linkifyword', 'linkify']