Converts any URLs in text into clickable links. Works on http://, https:// and www. links. Links can have trailing punctuation (periods, commas, close-parens) and leading punctuation (opening parens) and it'll still do the right thing. If trim_url_limit is not None, the URLs in link
(text, trim_url_limit=None, rel=None, target=None)
| 187 | |
| 188 | |
| 189 | def urlize(text, trim_url_limit=None, rel=None, target=None): |
| 190 | """Converts any URLs in text into clickable links. Works on http://, |
| 191 | https:// and www. links. Links can have trailing punctuation (periods, |
| 192 | commas, close-parens) and leading punctuation (opening parens) and |
| 193 | it'll still do the right thing. |
| 194 | |
| 195 | If trim_url_limit is not None, the URLs in link text will be limited |
| 196 | to trim_url_limit characters. |
| 197 | |
| 198 | If nofollow is True, the URLs in link text will get a rel="nofollow" |
| 199 | attribute. |
| 200 | |
| 201 | If target is not None, a target attribute will be added to the link. |
| 202 | """ |
| 203 | trim_url = lambda x, limit=trim_url_limit: limit is not None \ |
| 204 | and (x[:limit] + (len(x) >=limit and '...' |
| 205 | or '')) or x |
| 206 | words = _word_split_re.split(text_type(escape(text))) |
| 207 | rel_attr = rel and ' rel="%s"' % text_type(escape(rel)) or '' |
| 208 | target_attr = target and ' target="%s"' % escape(target) or '' |
| 209 | |
| 210 | for i, word in enumerate(words): |
| 211 | match = _punctuation_re.match(word) |
| 212 | if match: |
| 213 | lead, middle, trail = match.groups() |
| 214 | if middle.startswith('www.') or ( |
| 215 | '@' not in middle and |
| 216 | not middle.startswith('http://') and |
| 217 | not middle.startswith('https://') and |
| 218 | len(middle) > 0 and |
| 219 | middle[0] in _letters + _digits and ( |
| 220 | middle.endswith('.org') or |
| 221 | middle.endswith('.net') or |
| 222 | middle.endswith('.com') |
| 223 | )): |
| 224 | middle = '<a href="http://%s"%s%s>%s</a>' % (middle, |
| 225 | rel_attr, target_attr, trim_url(middle)) |
| 226 | if middle.startswith('http://') or \ |
| 227 | middle.startswith('https://'): |
| 228 | middle = '<a href="%s"%s%s>%s</a>' % (middle, |
| 229 | rel_attr, target_attr, trim_url(middle)) |
| 230 | if '@' in middle and not middle.startswith('www.') and \ |
| 231 | not ':' in middle and _simple_email_re.match(middle): |
| 232 | middle = '<a href="mailto:%s">%s</a>' % (middle, middle) |
| 233 | if lead + middle + trail != word: |
| 234 | words[i] = lead + middle + trail |
| 235 | return u''.join(words) |
| 236 | |
| 237 | |
| 238 | def generate_lorem_ipsum(n=5, html=True, min=20, max=100): |