forked from selcuk/markdown-urlify
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurlify.py
24 lines (17 loc) · 770 Bytes
/
urlify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
"""
URLify Extension for Python-Markdown
=====================================
Converts URLs in the markdown text to clickable links.
"""
import re
from markdown.preprocessors import Preprocessor
from markdown.extensions import Extension
urlfinder = re.compile(r'((([A-Za-z]{3,9}:(?:\/\/)?)(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(:[0-9]+)?|'
r'(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)((?:/[\+~%/\.\w\-_]*)?\??'
r'(?:[\-\+=&;%@\.\w_]*)#?(?:[\.!/\\\w]*))?)')
class URLify(Preprocessor):
def run(self, lines):
return [urlfinder.sub(r'<\1>', line) for line in lines]
class URLifyExtension(Extension):
def extendMarkdown(self, md, md_globals):
md.preprocessors.add('urlify', URLify(md), '_end')