How to use the mistune.InlineGrammar 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 lepture / mistune / tests / test_subclassing.py View on Github external
def test_simple_math():
    assert_data("$$\na = 1 *3* 5\n$$")
    assert_data("$ a = 1 *3* 5 $")


def test_markdown2html_math():
    # Mathematical expressions should be passed through unaltered
    assert_data('math.md')


def test_math_paragraph():
    # https://github.com/ipython/ipython/issues/6724
    assert_data('math-paragraph.md')


class WikiInlineGrammar(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'\]\](?!\])'             # ]]
    )


class WikiInlineLexer(mistune.InlineLexer):
    default_rules = copy.copy(mistune.InlineLexer.default_rules)
    default_rules.insert(3, 'wiki_link')

    def __init__(self, renderer, rules=None, **kwargs):
        if rules is None:
            rules = WikiInlineGrammar()
github lepture / mistune / tests / test_subclassing.py View on Github external
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]+?(?=[\\
github canonical-web-and-design / snapcraft.io / webapp / markdown.py View on Github external
"text",
        "newline",
    ]

    list_rules = ("block_code", "list_block", "text", "newline")

    # Need to extend this function since I need to modify this
    # https://github.com/lepture/mistune/blob/v0.8.4/mistune.py#L29
    def parse_block_code(self, m):
        # clean leading whitespace
        block_code_leading_pattern = re.compile(r"^ {3}", re.M)
        code = block_code_leading_pattern.sub("", m.group(0))
        self.tokens.append({"type": "code", "lang": None, "text": code})


class DescriptionInlineGrammar(InlineGrammar):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Rewrite to respect this convention:
        # https://github.com/CanonicalLtd/snap-squad/issues/936
        self.code = re.compile(r"^(`)([ \S]*?[^`])\1(?!`)")

        # Rewrite to support parentheses inside URLs:
        # https://github.com/canonical-web-and-design/snapcraft.io/issues/2424
        self.url = re.compile(
            r"""^([(])?(https?:\/\/[^\s<]+[^<.,:;"'\]\s])(?(1)([)]))"""
        )
        self.text = re.compile(
            r"^[\s\S]+?(?=[\\
github xalanq / ITree / imarkdown.py View on Github external
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"^\$(.+?)\$", re.DOTALL)
	block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL)
	text = re.compile(r'^[\s\S]+?(?=[\\
github cuthbertLab / music21 / music21 / ext / nbconvert / filters / markdown_mistune.py View on Github external
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"^\$(.+?)\$", re.DOTALL)
    block_math = re.compile(r"^\$\$(.+?)\$\$", re.DOTALL)
    text = re.compile(r'^[\s\S]+?(?=[\\
github tensorforce / tensorforce / docs / m2r.py View on Github external
})

    def parse_oneline_directive(self, m):
        # reuse directive output
        self.tokens.append({
            'type': 'directive',
            'text': m.group(1),
        })

    def parse_rest_code_block(self, m):
        self.tokens.append({
            'type': 'rest_code_block',
        })


class RestInlineGrammar(mistune.InlineGrammar):
    image_link = re.compile(
        r'\[!\[(?P.*?)\]\((?P.*?)\).*?\]\((?P.*?)\)'
    )
    rest_role = re.compile(r':.*?:`.*?`|`[^`]+`:.*?:')
    rest_link = re.compile(r'`[^`]*?`_')
    inline_math = re.compile(r'`\$(.*)?\$`')
    eol_literal_marker = re.compile(r'(\s+)?::\s*$')
    # add colon and space as special text
    text = re.compile(r'^[\s\S]+?(?=[\\[\s\S]+?)\1{2}(?!\1)'
    )
    # _word_ or *word*
    emphasis = re.compile(
        r'^\b_((?:__|[^_])+?)_\b'  # _word_
github nitely / Spirit / spirit / core / utils / markdown / inline.py View on Github external
from django.contrib.auth import get_user_model

import mistune

from ...conf import settings
from .utils.emoji import emojis

User = get_user_model()
_linebreak = re.compile(r'^ *\n(?!\s*$)')
_text = re.compile(
    r'^[\s\S]+?(?=[\\[A-Za-z0-9_\-\+]+?):'
    )

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

    # Override
    def hard_wrap(self):
github miyakogi / m2r / m2r.py View on Github external
})

    def parse_oneline_directive(self, m):
        # reuse directive output
        self.tokens.append({
            'type': 'directive',
            'text': m.group(1),
        })

    def parse_rest_code_block(self, m):
        self.tokens.append({
            'type': 'rest_code_block',
        })


class RestInlineGrammar(mistune.InlineGrammar):
    image_link = re.compile(
        r'\[!\[(?P.*?)\]\((?P.*?)\).*?\]\((?P.*?)\)'
    )
    rest_role = re.compile(r':.*?:`.*?`|`[^`]+`:.*?:')
    rest_link = re.compile(r'`[^`]*?`_')
    inline_math = re.compile(r'`\$(.*)?\$`')
    eol_literal_marker = re.compile(r'(\s+)?::\s*$')
    # add colon and space as special text
    text = re.compile(r'^[\s\S]+?(?=[\\[\s\S]+?)\1{2}(?!\1)'
    )
    # _word_ or *word*
    emphasis = re.compile(
        r'^\b_((?:__|[^_])+?)_\b'  # _word_
github huntzhan / zhlint / zhlint / mistune_patch.py View on Github external
key, m = manipulate(text)
            if m is not False:
                if self.loc_manager.level - self._block_level == 1:
                    inject_loc_and_block_type(key, m)

                text = text[len(m.group(0)):]
                continue

            if text:  # pragma: no cover
                raise RuntimeError('Infinite loop at: %s' % text)

        self.loc_manager.pop()
        return self.tokens


class HackedInlineGrammar(InlineGrammar):

    code = re.compile(r'^\s*(`+)\s*([\s\S]*?[^`])\s*\1(?!`)')

    text = re.compile(
        r'^[\s\S]+?'
        r'(?=[\\