How to use the mistune.InlineLexer 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 yaqwsx / PcbDraw / pcbdraw / populate.py View on Github external
import mistune
import pcbdraw.mdrenderer
import re
import codecs
import pybars
import yaml
import argparse
import subprocess
import sysconfig
from copy import deepcopy

TEMPLATES_SUBDIR = 'templates'
data_path = [os.path.dirname(__file__)]


class PcbDrawInlineLexer(mistune.InlineLexer):
    def __init__(self, renderer, rules=None, **kwargs):
        super(PcbDrawInlineLexer, self).__init__(renderer, rules=None, **kwargs)
        self.enable_pcbdraw()

    def enable_pcbdraw(self):
        self.rules.pcbdraw = re.compile(
            r"\[\["                   # [[
            r"([\s\S]+?\|[\s\S]+?)"   # side| component
            r"\]\](?!\])"             # ]]
        )
        self.default_rules.insert(3, "pcbdraw")

    def output_pcbdraw(self, m):
        text = m.group(1)
        side, components = text.split("|")
        components = list(map(lambda x: x.strip(), components.split(",")))
github spacetelescope / asdf-standard / source / sphinxext / md2rst.py View on Github external
def enable_math(self):
        self.rules.text = re.compile(
            r'^[\s\S]+?(?=[\\
github chriskuehl / fluffy / fluffy / component / markdown.py View on Github external
def block_code(self, code, lang):
        return PygmentsHighlighter(
            guess_lexer(code, lang, None, opts={'stripnl': True}),
        ).highlight(code)


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,
)
github demisto / dockerfiles / docker / sane-doc-reports / src / sane_doc_reports / transform / markdown / md_helpers.py View on Github external
class ButtonRenderer(mistune.Renderer):
        '''
        Syntax for MD buttons
            %%%{JSON.message}%%%
        For example:
            %%%%{"message": "Something here"}%%%%
        Output:
            Something here
        '''

        def paragraph(self, text):
            text = _get_contents(text)
            return f'<p>{text}</p>'

    class ButtonInlineLexer(mistune.InlineLexer):
        def enable_md_button(self):
            self.rules.md_button = re.compile(r'%%%(.*?)%%%')
            self.default_rules.insert(3, 'md_button')

        def placeholder(self):
            pass

        def output_md_button(self, m):
            text = m.group(1)
            return self.renderer.paragraph(text)

    renderer = ButtonRenderer()
    inline_lexer = ButtonInlineLexer(renderer)
    inline_lexer.enable_md_button()

    md = mistune.Markdown(renderer, inline=inline_lexer)
github xalanq / ITree / imarkdown.py View on Github external
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"^\$(.+?)\$", re.DOTALL)
	block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL)
	text = re.compile(r'^[\s\S]+?(?=[\\
github thoppe / miniprez / block_custom_parser.py View on Github external
return s if s else None


class DivClassRenderer(Renderer):
    def OpenBlockClass(self, names):
        return f"<div class="{names}">"

    def CloseBlockClass(self):
        return f"</div>"

    def LineBlockClass(self, names, remaining):
        remaining = remaining.lstrip()
        return f"<div class="{names}">{remaining}</div>"


class DivClassInlineLexer(InlineLexer):

    rule_num = 0

    def enable(self):

        self.default_rules.remove("escape")
        self.default_rules.remove("linebreak")

        # def enable_OpenBlockClass(self):
        # Matching pattern, two dots then valid class names dot separated
        grammar = r"[^\\]\.\.[\-\w\d]+[\.[\-\w\d]+]?\s"

        self.rules.OpenBlockClass = re.compile(grammar)
        self.default_rules.insert(self.rule_num, "OpenBlockClass")

        # def enable_CloseBlockClass(self):
github nitely / Spirit / spirit / core / utils / markdown / inline.py View on Github external
r'^:(?P[A-Za-z0-9_\-\+]+?):'
    )

    mention = re.compile(
        r'^@(?P[\w.@+-]+)',
        flags=re.UNICODE
    )

    # Override
    def hard_wrap(self):
        # Adds ":" and "@" as an invalid text character, so we can match emojis and mentions.
        self.linebreak = _linebreak
        self.text = _text


class InlineLexer(mistune.InlineLexer):

    default_rules = copy.copy(mistune.InlineLexer.default_rules)
    default_rules.insert(0, 'math')
    default_rules.insert(2, 'emoji')
    default_rules.insert(2, 'mention')

    def __init__(self, renderer, rules=None, **kwargs):
        rules = InlineGrammar()
        rules.hard_wrap()

        super(InlineLexer, self).__init__(renderer, rules, **kwargs)

        self.mentions = {}
        self._mention_count = 0

    def output_math(self, m):
github ocf / ocfweb / ocfweb / component / markdown.py View on Github external
level=level,
            id=id,
            text=text,
        )


class OcfMarkdownRenderer(
    HeaderRendererMixin,
    CodeRendererMixin,
    mistune.Renderer,
):
    pass


class OcfMarkdownInlineLexer(
    mistune.InlineLexer,
    DjangoLinkInlineLexerMixin,
    HtmlCommentsLexerMixin,
    BackslashLineBreakLexerMixin,
):
    pass


class OcfMarkdownBlockLexer(
    mistune.BlockLexer,
    HtmlCommentsLexerMixin,
):
    pass


_renderer = OcfMarkdownRenderer(
    escape=True,