-
Notifications
You must be signed in to change notification settings - Fork 438
link patterns
The link-patterns
extra enables mapping of given regex patterns in text to
links. The link_patterns
list on the Markdown
class provides
the mapping.
For example, the following with map "recipe NNNN" and "komodo bug NNNN" to appropriate links:
>>> import markdown2, re
>>> link_patterns = [
... (re.compile("recipe\s+(\d+)", re.I), r"http://code.activestate.com/recipes/\1/"),
... (re.compile("(?:komodo\s+)?bug\s+(\d+)", re.I), r"http://bugs.activestate.com/show_bug.cgi?id=\1"),
... ]
>>> markdown2.markdown('Recipe 123 and Komodo bug 234 are related.'
... extras=["link-patterns"], link_patterns=link_patterns)
'<p><a href="http://code.activestate.com/recipes/123/">Recipe 123</a> and \
<a href="http://bugs.activestate.com/show_bug.cgi?id=234">Komodo bug 234</a> are related.</p>'
Here is a script that uses link-patterns to auto-link UncycloWords (for Uncyclo syntaxes that do that):
https://github.com/trentm/python-markdown2/blob/master/sandbox/wiki.py
For the command-line interface, the --link-patterns-file
option has been
added. A "link patterns file" has one link pattern per line of the form:
<regex-pattern> <href-pattern>
For example:
/recipe\s+(\d+)/i http://code.activestate.com/recipes/\1/
/(?:komodo\s+)?bug\s+(\d+)/i http://bugs.activestate.com/show_bug.cgi?id=\1
Spaces are not allowed in the <href-pattern>
(to simplify parsing). Lines
beginning with a hash (#) are comments.
$ python markdown2.py -x link-patterns --link-patterns-file patterns foo.text
from markdown2 import Markdown
import re
pattern = (
r'((([A-Za-z]{3,9}:(?:\/\/)?)' # scheme
r'(?:[\-;:&=\+\$,\w]+@)?[A-Za-z0-9\.\-]+(:\[0-9]+)?' # user@hostname:port
r'|(?:www\.|[\-;:&=\+\$,\w]+@)[A-Za-z0-9\.\-]+)' # www.|user@hostname
r'((?:\/[\+~%\/\.\w\-_]*)?' # path
r'\??(?:[\-\+=&;%@\.\w_]*)' # query parameters
r'#?(?:[\.\!\/\\\w]*))?)' # fragment
r'(?![^<]*?(?:<\/\w+>|\/?>))' # ignore anchor HTML tags
r'(?![^\(]*?\))' # ignore links in brackets (Markdown links and images)
)
link_patterns = [(re.compile(pattern),r'\1')]
markdown=Markdown(extras=["link-patterns"],link_patterns=link_patterns)
markdown.convert('http://www.google.com')
markdown.convert('<a href="http://www.google.com">link</a>')
markdown.convert('[link](http://www.google.com)')
Output
u'<p><a href="http://www.google.com">http://www.google.com</a></p>\n'
u'<p><a href="http://www.google.com">link</a></p>\n'
u'<p><a href="http://www.google.com">link</a></p>\n'
Without specifying the link-patterns
extra, the module will use standard markdown link format [text](link)
. Please note that it only allows the URL schemas http(s) and ftp; otherwise it outputs a bookmark link #
.
(Return to Extras page.)