Extend Markdown to support wiki links

from markdown import Markdown, Pattern

class WikiLinkPattern(Pattern):
    """A pattern to turn wiki links into Markdown_ links.

            -> [SomePage](/wiki/view/SomePage)
        [[SomePage|some page]]
            -> [some page](/wiki/view/SomePage)

    Must be applied before link patterns.

    .. _Markdown: http://daringfireball.net/projects/markdown/

    _pattern = r'\[\[([A-Z].+)\]\]'

    def __init__(self, pattern=None):
        Pattern.__init__(self, pattern or self._pattern)

    def handleMatch(self, m, doc):
        parts = m.group(2).split('|', 1)
        if len(parts) == 2:
            href, text = parts
            href = text = m.group(2)
        el = doc.createElement('a')
        el.setAttribute('href', '/wiki/view/%s' % href)
        return el

# Usage
md = Markdown()
md.inlinePatterns.insert(0, WikiLinkPattern())
text = '...'
text = md.toString(text)