# coding: utf-8 import os import re import copy import mistune root = os.path.dirname(__file__) class MathBlockGrammar(mistune.BlockGrammar): block_math = re.compile(r'^\$\$(.*?)\$\$', re.DOTALL) latex_environment = re.compile( r"^\\begin\{([a-z]*\*?)\}(.*?)\\end\{\1\}", re.DOTALL ) class MathBlockLexer(mistune.BlockLexer): default_rules = ['block_math', 'latex_environment'] + \ mistune.BlockLexer.default_rules def __init__(self, rules=None, **kwargs): if rules is None: rules = MathBlockGrammar() super(MathBlockLexer, self).__init__(rules, **kwargs) def parse_block_math(self, m): """Parse a $$math$$ block""" self.tokens.append({ 'type': 'block_math', 'text': m.group(1) }) def parse_latex_environment(self, m): self.tokens.append({ 'type': 'latex_environment', 'name': m.group(1), 'text': m.group(2) }) class MathInlineGrammar(mistune.InlineGrammar): math = re.compile(r'^\$(.+?)\$') text = re.compile(r'^[\s\S]+?(?=[\\%s' % (link, alt) def test_custom_lexer(): markdown = mistune.Markdown(inline=WikiInlineLexer) ret = markdown('[[Link Text|Wiki Link]]') assert '