1 # -*- coding: utf-8 -*-
8 :copyright: 2008 by Armin Ronacher.
9 :license: BSD, see LICENSE for more details.
15 def escape(obj, attribute=False):
16 """HTML escape an object."""
17 if hasattr(obj, '__html__'):
20 .replace('&', '&') \
21 .replace('>', '>') \
22 .replace('<', '<') \
23 .replace('"', '"')
26 def pformat(obj, verbose=False):
28 Prettyprint an object. Either use the `pretty` library or the
32 from pretty import pretty
33 return pretty(obj, verbose=verbose)
35 from pprint import pformat
39 _word_split_re = re.compile(r'(\s+)')
41 _punctuation_re = re.compile(
42 '^(?P<lead>(?:%s)*)(?P<middle>.*?)(?P<trail>(?:%s)*)$' % (
43 '|'.join([re.escape(p) for p in ('(', '<', '<')]),
44 '|'.join([re.escape(p) for p in ('.', ',', ')', '>', '\n', '>')])
48 _simple_email_re = re.compile(r'^\S+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+$')
51 def urlize(text, trim_url_limit=None, nofollow=False):
53 Converts any URLs in text into clickable links. Works on http://,
54 https:// and www. links. Links can have trailing punctuation (periods,
55 commas, close-parens) and leading punctuation (opening parens) and
56 it'll still do the right thing.
58 If trim_url_limit is not None, the URLs in link text will be limited
59 to trim_url_limit characters.
61 If nofollow is True, the URLs in link text will get a rel="nofollow"
64 trim_url = lambda x, limit=trim_url_limit: limit is not None \
65 and (x[:limit] + (len(x) >=limit and '...'
67 words = _word_split_re.split(text)
68 nofollow_attr = nofollow and ' rel="nofollow"' or ''
69 for i, word in enumerate(words):
70 match = _punctuation_re.match(word)
72 lead, middle, trail = match.groups()
73 if middle.startswith('www.') or (
75 not middle.startswith('http://') and
77 middle[0] in string.letters + string.digits and (
78 middle.endswith('.org') or
79 middle.endswith('.net') or
80 middle.endswith('.com')
82 middle = '<a href="http://%s"%s>%s</a>' % (middle,
83 nofollow_attr, trim_url(middle))
84 if middle.startswith('http://') or \
85 middle.startswith('https://'):
86 middle = '<a href="%s"%s>%s</a>' % (middle,
87 nofollow_attr, trim_url(middle))
88 if '@' in middle and not middle.startswith('www.') and \
89 not ':' in middle and _simple_email_re.match(middle):
90 middle = '<a href="mailto:%s">%s</a>' % (middle, middle)
91 if lead + middle + trail != word:
92 words[i] = lead + middle + trail
93 return u''.join(words)