Created
August 12, 2014 12:01
-
-
Save oberstet/d03acd7e2c805ff78163 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import mistune | |
from pygments import highlight | |
from pygments.lexers import get_lexer_by_name | |
from pygments.formatters import HtmlFormatter | |
import json | |
import re | |
import copy | |
class MyInlineGrammar(mistune.InlineGrammar): | |
# it would take a while for creating the right regex | |
wiki_link = re.compile( | |
r'\[\[' # [[ | |
r'([\s\S]+?\|[\s\S]+?)' # Page 2|Page 2 | |
r'\]\](?!\])' # ]] | |
) | |
wiki_short_link = re.compile( | |
r'\[\[' # [[ | |
r'([\s\S]+?)' # Page 2 | |
r'\]\](?!\])' # ]] | |
) | |
class MyInlineLexer(mistune.InlineLexer): | |
default_features = copy.copy(mistune.InlineLexer.default_features) | |
# Add wiki_link parser to default features | |
# you can insert it any place you like | |
default_features.insert(3, 'wiki_link') | |
default_features.insert(3, 'wiki_short_link') | |
def __init__(self, renderer, rules=None, **kwargs): | |
if rules is None: | |
# use the inline grammar | |
rules = MyInlineGrammar() | |
super(MyInlineLexer, self).__init__(renderer, rules, **kwargs) | |
def output_wiki_link(self, m): | |
text = m.group(1) | |
alt, link = text.split('|') | |
# you can create an custom render | |
# you can also return the html if you like | |
return self.renderer.wiki_link(alt, link) | |
def output_wiki_short_link(self, m): | |
text = m.group(1) | |
alt, link = text, text | |
# you can create an custom render | |
# you can also return the html if you like | |
return self.renderer.wiki_link(alt, link) | |
class DocPageRenderer(mistune.Renderer): | |
def __init__(self, pages, debug = False): | |
mistune.Renderer.__init__(self) | |
self.debug = debug | |
class DocPages: | |
def __init__(self, docroot, extensions = ['.md'], debug = False): | |
rend = DocPageRenderer(self, debug) | |
inline = MyInlineLexer(rend) | |
self._renderer = mistune.Markdown(renderer = rend, inline = inline) | |
def render(self, path): | |
return self._pages.get(path, None) | |
pages = DocPages('../crossbar.wiki') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment