def on_markdown_config(self, config, **extra): # create inline and block lexers inline_lexer = mistune.InlineLexer(mistune.Renderer()) block_lexer = mistune.BlockLexer() # does an lexer already exist? if so, use that if 'inline' in config.options: inline_lexer = config.options['inline'] if 'block' in config.options: block_lexer = config.options['inline'] # loop through all enabled plugins for plugin in plugins: # select the correct lexer lexer = inline_lexer if plugin.type == 'block': lexer = block_lexer # add the plugin in setattr(lexer.rules, plugin.name, plugin.regex) # if there is a position and renderer, add that in too if hasattr(plugin, 'position') and hasattr(plugin, 'render'): lexer.default_rules.insert(plugin.position, plugin.name) setattr(lexer, 'output_%s' % plugin.name, plugin.render) # set the config to use these custom lexers config.options['inline'] = inline_lexer config.options['block'] = block_lexer
def update_test_from_md(md_file, test): """ update test cases and variables for the test """ ret = False test_cases = [] variables = {} with open(md_file, encoding='utf-8') as f: parser = mistune.BlockLexer() text = f.read() parser.parse(mistune.preprocessing(text)) for t in parser.tokens: if t["type"] == "table": table_header = t["header"][0].lower() if table_header == 'test case' or table_header == 'test cases': for c in t["cells"]: if not c[0] == '---': test_cases.append(c[0]) break if table_header == 'variable' or table_header == 'variables': list_var = None for c in t["cells"]: if c[0].startswith('#') or c[0].startswith('---'): continue if c[0].startswith('${'): list_var = None dict_var = None variables[filter_kw( c[0])] = c[1] if len(c) > 1 else '' elif c[0].startswith('@'): dict_var = None list_var = filter_kw(c[0]) variables[list_var] = c[1:] elif c[0].startswith('...'): if list_var: variables[list_var].extend(c[1:]) elif dict_var: for i in c[1:]: if not i: continue k, v = i.split('=') variables[dict_var][k] = v elif c[0].startswith('&'): list_var = None dict_var = filter_kw(c[0]) variables[dict_var] = {} for i in c[1:]: if not i: continue k, v = i.split('=') variables[dict_var][k] = v else: logger.error('Unknown tag: ' + c[0]) if test.test_cases != test_cases: test.test_cases = test_cases ret = True if test.variables != variables: test.variables = variables ret = True return ret
def __init__(self): treeRenderer = JuloDocRenderer() blockLexer = mistune.BlockLexer( mistune.BlockGrammar()) #JuloDocBlockLexer() inlineLexer = JuloDocInlineLexer(treeRenderer) super(JuloParser, self).__init__(renderer=treeRenderer, inline=inlineLexer, block=blockLexer)
def block_code(self, code, lang): block_lexer = mistune.BlockLexer() m = re.match(block_lexer.grammar_class.fences, code) if m: lang = m.group(2) code = m.group(3) if not lang: code = highlight(code.decode('utf-8'), PythonLexer(), HtmlFormatter()) return code.encode('utf-8') lexer = get_lexer_by_name(lang, stripall=True) formatter = HtmlFormatter() return highlight(code, lexer, formatter).encode('utf-8')
def test_block_lexer(): bl = mistune.BlockLexer() assert bl.default_rules is not mistune.BlockLexer.default_rules
def parse_markdown(source): """ Parse markdown and return an mistune AST. """ return mistune.BlockLexer()(source)
def mistune_parse(source): """Use mistune to parse given source string.""" return mistune.BlockLexer()(source)
def markdown(value): renderer = HighlightRenderer() block = mistune.BlockLexer(BetterBlockGrammar()) return mistune.Markdown(escape=False, renderer=renderer, block=block)(value)