| OLD | NEW |
| (Empty) |
| 1 ''' | |
| 2 WikiLinks Extension for Python-Markdown | |
| 3 ====================================== | |
| 4 | |
| 5 Converts [[WikiLinks]] to relative links. Requires Python-Markdown 2.0+ | |
| 6 | |
| 7 Basic usage: | |
| 8 | |
| 9 >>> import markdown | |
| 10 >>> text = "Some text with a [[WikiLink]]." | |
| 11 >>> html = markdown.markdown(text, ['wikilinks']) | |
| 12 >>> print html | |
| 13 <p>Some text with a <a class="wikilink" href="/WikiLink/">WikiLink</a>.</p> | |
| 14 | |
| 15 Whitespace behavior: | |
| 16 | |
| 17 >>> print markdown.markdown('[[ foo bar_baz ]]', ['wikilinks']) | |
| 18 <p><a class="wikilink" href="/foo_bar_baz/">foo bar_baz</a></p> | |
| 19 >>> print markdown.markdown('foo [[ ]] bar', ['wikilinks']) | |
| 20 <p>foo bar</p> | |
| 21 | |
| 22 To define custom settings the simple way: | |
| 23 | |
| 24 >>> print markdown.markdown(text, | |
| 25 ... ['wikilinks(base_url=/wiki/,end_url=.html,html_class=foo)'] | |
| 26 ... ) | |
| 27 <p>Some text with a <a class="foo" href="/wiki/WikiLink.html">WikiLink</a>.<
/p> | |
| 28 | |
| 29 Custom settings the complex way: | |
| 30 | |
| 31 >>> md = markdown.Markdown( | |
| 32 ... extensions = ['wikilinks'], | |
| 33 ... extension_configs = {'wikilinks': [ | |
| 34 ... ('base_url', 'http://example.com/'), | |
| 35 ... ('end_url', '.html'), | |
| 36 ... ('html_class', '') ]}, | |
| 37 ... safe_mode = True) | |
| 38 >>> print md.convert(text) | |
| 39 <p>Some text with a <a href="http://example.com/WikiLink.html">WikiLink</a>.
</p> | |
| 40 | |
| 41 Use MetaData with mdx_meta.py (Note the blank html_class in MetaData): | |
| 42 | |
| 43 >>> text = """wiki_base_url: http://example.com/ | |
| 44 ... wiki_end_url: .html | |
| 45 ... wiki_html_class: | |
| 46 ... | |
| 47 ... Some text with a [[WikiLink]].""" | |
| 48 >>> md = markdown.Markdown(extensions=['meta', 'wikilinks']) | |
| 49 >>> print md.convert(text) | |
| 50 <p>Some text with a <a href="http://example.com/WikiLink.html">WikiLink</a>.
</p> | |
| 51 | |
| 52 MetaData should not carry over to next document: | |
| 53 | |
| 54 >>> print md.convert("No [[MetaData]] here.") | |
| 55 <p>No <a class="wikilink" href="/MetaData/">MetaData</a> here.</p> | |
| 56 | |
| 57 Define a custom URL builder: | |
| 58 | |
| 59 >>> def my_url_builder(label, base, end): | |
| 60 ... return '/bar/' | |
| 61 >>> md = markdown.Markdown(extensions=['wikilinks'], | |
| 62 ... extension_configs={'wikilinks' : [('build_url', my_url_builder)]
}) | |
| 63 >>> print md.convert('[[foo]]') | |
| 64 <p><a class="wikilink" href="/bar/">foo</a></p> | |
| 65 | |
| 66 From the command line: | |
| 67 | |
| 68 python markdown.py -x wikilinks(base_url=http://example.com/,end_url=.html,h
tml_class=foo) src.txt | |
| 69 | |
| 70 By [Waylan Limberg](http://achinghead.com/). | |
| 71 | |
| 72 License: [BSD](http://www.opensource.org/licenses/bsd-license.php) | |
| 73 | |
| 74 Dependencies: | |
| 75 * [Python 2.3+](http://python.org) | |
| 76 * [Markdown 2.0+](http://packages.python.org/Markdown/) | |
| 77 ''' | |
| 78 | |
| 79 from __future__ import absolute_import | |
| 80 from __future__ import unicode_literals | |
| 81 from . import Extension | |
| 82 from ..inlinepatterns import Pattern | |
| 83 from ..util import etree | |
| 84 import re | |
| 85 | |
| 86 def build_url(label, base, end): | |
| 87 """ Build a url from the label, a base, and an end. """ | |
| 88 clean_label = re.sub(r'([ ]+_)|(_[ ]+)|([ ]+)', '_', label) | |
| 89 return '%s%s%s'% (base, clean_label, end) | |
| 90 | |
| 91 | |
| 92 class WikiLinkExtension(Extension): | |
| 93 def __init__(self, configs): | |
| 94 # set extension defaults | |
| 95 self.config = { | |
| 96 'base_url' : ['/', 'String to append to beginning or URL
.'], | |
| 97 'end_url' : ['/', 'String to append to end of URL.'], | |
| 98 'html_class' : ['wikilink', 'CSS hook. Leave blank for n
one.'], | |
| 99 'build_url' : [build_url, 'Callable formats URL from lab
el.'], | |
| 100 } | |
| 101 | |
| 102 # Override defaults with user settings | |
| 103 for key, value in configs : | |
| 104 self.setConfig(key, value) | |
| 105 | |
| 106 def extendMarkdown(self, md, md_globals): | |
| 107 self.md = md | |
| 108 | |
| 109 # append to end of inline patterns | |
| 110 WIKILINK_RE = r'\[\[([\w0-9_ -]+)\]\]' | |
| 111 wikilinkPattern = WikiLinks(WIKILINK_RE, self.getConfigs()) | |
| 112 wikilinkPattern.md = md | |
| 113 md.inlinePatterns.add('wikilink', wikilinkPattern, "<not_strong") | |
| 114 | |
| 115 | |
| 116 class WikiLinks(Pattern): | |
| 117 def __init__(self, pattern, config): | |
| 118 super(WikiLinks, self).__init__(pattern) | |
| 119 self.config = config | |
| 120 | |
| 121 def handleMatch(self, m): | |
| 122 if m.group(2).strip(): | |
| 123 base_url, end_url, html_class = self._getMeta() | |
| 124 label = m.group(2).strip() | |
| 125 url = self.config['build_url'](label, base_url, end_url) | |
| 126 a = etree.Element('a') | |
| 127 a.text = label | |
| 128 a.set('href', url) | |
| 129 if html_class: | |
| 130 a.set('class', html_class) | |
| 131 else: | |
| 132 a = '' | |
| 133 return a | |
| 134 | |
| 135 def _getMeta(self): | |
| 136 """ Return meta data or config data. """ | |
| 137 base_url = self.config['base_url'] | |
| 138 end_url = self.config['end_url'] | |
| 139 html_class = self.config['html_class'] | |
| 140 if hasattr(self.md, 'Meta'): | |
| 141 if 'wiki_base_url' in self.md.Meta: | |
| 142 base_url = self.md.Meta['wiki_base_url'][0] | |
| 143 if 'wiki_end_url' in self.md.Meta: | |
| 144 end_url = self.md.Meta['wiki_end_url'][0] | |
| 145 if 'wiki_html_class' in self.md.Meta: | |
| 146 html_class = self.md.Meta['wiki_html_class'][0] | |
| 147 return base_url, end_url, html_class | |
| 148 | |
| 149 | |
| 150 def makeExtension(configs=None) : | |
| 151 return WikiLinkExtension(configs=configs) | |
| OLD | NEW |