How to use the mistune.BlockLexer function in mistune

To help you get started, we’ve selected a few mistune examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Azure / simdem / tests / test_parser.py View on Github external
def setUp(self):
        config = configparser.ConfigParser()
        config.read("content/config/unit_test.ini")

        self.parser = simdem.Parser(mistune.BlockLexer())
github lepture / mistune / tests / test_extra.py View on Github external
def test_trigger_more_cases():
    markdown = mistune.Markdown(
        inline=mistune.InlineLexer,
        block=mistune.BlockLexer,
        skip_html=True
    )
    ret = markdown.render('foo[^foo]\n\n[^foo]: foo\n\n[^foo]: bar\n')
    assert 'bar' not in ret
github lepture / mistune / tests / test_subclassing.py View on Github external
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):
github f1nnix / magplan / xmd / lexers.py View on Github external
import re

from mistune import BlockLexer


class PanelBlockLexer(BlockLexer):
    def __init__(self, *args, **kwargs):
        super(PanelBlockLexer, self).__init__(*args, **kwargs)
        self.has_lead = False

        self.enable_panel()
        self.enable_paywall()

    def enable_panel(self):
        self.rules.panel_block = re.compile(r'\[ (.+)\n'
                                            r'\n'
                                            r'((?:.*?\n)*?)'
                                            r'\n'
                                            r'\]'
                                            )
        self.default_rules.insert(3, 'panel_block')
github laixintao / iredis / iredis / markdown.py View on Github external
return f"{text}\n{'='*len(text)}\n"

    def paragraph(self, text):
        return text + "\n\n"

    def block_code(self, code, language=None):
        code = "\n".join(["  " + line for line in code.splitlines()])
        return super().block_code(code)

    def header(self, text, level, raw=None):
        if level == 2:
            header_text = self._to_title(text)
            return super().header(header_text, 2)


class RedisDocLexer(mistune.BlockLexer):
    def enable_at_title(self):
        self.rules.at_title = re.compile(r"^@(\w+) *(?:\n+|$)")  # @example
        self.default_rules.insert(0, "at_title")

    def parse_at_title(self, m):
        text = m.group(1)
        self.tokens.append({"type": "heading", "level": 2, "text": text})


renderer = TerminalRender()
block_lexer = RedisDocLexer()
block_lexer.enable_at_title()
markdown_render = mistune.Markdown(renderer, block=block_lexer)


def render(text):
github hypothesis / h / h / util / markdown.py View on Github external
class MathMarkdown(mistune.Markdown):
    def output_block_math(self):
        return self.renderer.block_math(self.token["text"])


class MathInlineLexer(mistune.InlineLexer):
    def __init__(self, *args, **kwargs):
        super(MathInlineLexer, self).__init__(*args, **kwargs)
        self.rules.inline_math = re.compile(r"\\\((.*?)\\\)", re.DOTALL)
        self.default_rules.insert(0, "inline_math")

    def output_inline_math(self, m):
        return self.renderer.inline_math(m.group(1))


class MathBlockLexer(mistune.BlockLexer):
    def __init__(self, *args, **kwargs):
        super(MathBlockLexer, self).__init__(*args, **kwargs)
        self.rules.block_math = re.compile(r"^\$\$(.*?)\$\$", re.DOTALL)
        self.default_rules.insert(0, "block_math")

    def parse_block_math(self, m):
        self.tokens.append({"type": "block_math", "text": m.group(1)})


class MathRenderer(mistune.Renderer):
    def __init__(self, **kwargs):
        super(MathRenderer, self).__init__(**kwargs)

    def block_math(self, text):
        return "<p>$$%s$$</p>\n" % text
github chriskuehl / fluffy / fluffy / component / markdown.py View on Github external
class FluffyMarkdownRenderer(
    CodeRendererMixin,
    mistune.Renderer,
):
    pass


class FluffyMarkdownInlineLexer(
    mistune.InlineLexer,
    HtmlCommentsInlineLexerMixin,
):
    pass


class FluffyMarkdownBlockLexer(
    mistune.BlockLexer,
    HtmlCommentsBlockLexerMixin,
):
    pass


_renderer = FluffyMarkdownRenderer(
    escape=True,
    hard_wrap=False,
)

_inline = FluffyMarkdownInlineLexer(_renderer)
_inline.enable_html_comments()

_block = FluffyMarkdownBlockLexer(mistune.BlockGrammar())
_block.enable_html_comments()