#!/usr/bin/env python ''' Wikipath Extension for Python-Markdown ====================================== Converts [Link Name](wiki:ArticleName) to relative links pointing to article. Requires Python-Markdown 2.0+ Basic usage: >>> import markdown >>> text = "Some text with a [Link Name](wiki:ArticleName)." >>> html = markdown.markdown(text, ['wikipath(base_url="/wiki/view/")']) >>> html u'

Some text with a Link Name.

' Dependencies: * [Python 2.3+](http://python.org) * [Markdown 2.0+](http://www.freewisdom.org/projects/python-markdown/) ''' import markdown try: # Markdown 2.1.0 changed from 2.0.3. We try importing the new version first, # but import the 2.0.3 version if it fails from markdown.util import etree except: from markdown import etree class WikiPathExtension(markdown.Extension): def __init__(self, configs): # set extension defaults self.config = { 'default_namespace': ['edX', 'Default namespace for when one isn\'t specified.'], 'html_class': ['wikipath', 'CSS hook. Leave blank for none.'] } # Override defaults with user settings for key, value in configs: # self.config[key][0] = value self.setConfig(key, value) def extendMarkdown(self, md, md_globals): self.md = md # append to end of inline patterns WIKI_RE = r'\[(?P.+?)\]\(wiki:(?P[a-zA-Z\d/_-]*)\)' wikiPathPattern = WikiPath(WIKI_RE, self.config) wikiPathPattern.md = md md.inlinePatterns.add('wikipath', wikiPathPattern, "