link patterns

George Karakostas edited this page Jun 10, 2017 · 4 revisions

link-patterns extra

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"\1/"),
...    (re.compile("(?:komodo\s+)?bug\s+(\d+)", re.I), r"\1"),
... ]
>>> markdown2.markdown('Recipe 123 and Komodo bug 234 are related.'
...     extras=["link-patterns"], link_patterns=link_patterns)
'<p><a href="">Recipe 123</a> and \
<a href="">Komodo bug 234</a> are related.</p>'

Here is a script that uses link-patterns to auto-link WikiWords (for Wiki syntaxes that do that):

link patterns file

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     \1/

Spaces are not allowed in the <href-pattern> (to simplify parsing). Lines beginning with a hash (#) are comments.

$ python -x link-patterns --link-patterns-file patterns foo.text

Converting links into links automatically

from markdown2 import Markdown
import re


u'<p><a href=""></a></p>\n'

Default link behaviour

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.)