How to use mistune - 10 common examples

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_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()

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

    def output_wiki_link(self, m):
        text = m.group(1)
        alt, link = text.split('|')
        return '<a href="%s">%s</a>' % (link, alt)


def test_custom_lexer():
    markdown = mistune.Markdown(inline=WikiInlineLexer)
github buddylindsey / dj-blog / tests / test_models.py View on Github external
def test_published_body(self):
        article = mommy.make('djblog.Article', body='hello')

        self.mock.StubOutWithMock(Markdown, 'render')
        Markdown.render('hello').AndReturn('converted text')

        self.mock.ReplayAll()
        a = article.published_body()
        self.mock.VerifyAll()

        self.assertEqual(a, 'converted text')
github lepture / mistune / tests / test_extra.py View on Github external
def test_use_xhtml():
    ret = mistune.markdown('foo\n\n----\n\nbar')
    assert '<hr>' in ret
    ret = mistune.markdown('foo\n\n----\n\nbar', use_xhtml=True)
    assert '<hr>' in ret

    ret = mistune.markdown('foo  \nbar', use_xhtml=True)
    assert '<br>' in ret

    ret = mistune.markdown('![foo](bar "title")', use_xhtml=True)
    assert '<img title="title" alt="foo" src="bar">' in ret
github miyuchina / mistletoe / test / profiler / mistune_profiler.py View on Github external
def run():
    with open('test/profiler/syntax.md', 'r') as fin:
        mistune.markdown(fin.read())
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 lepture / mistune / tests / test_misc.py View on Github external
def test_escape_html(self):
        md = mistune.create_markdown(escape=True)
        result = md('<div>1</div>')
        expected = '<p>&lt;div&gt;1&lt;/div&gt;</p>'
        self.assertEqual(result.strip(), expected)

        result = md('<em>1</em>')
        expected = '<p>&lt;em&gt;1&lt;/em&gt;</p>'
        self.assertEqual(result.strip(), expected)
github lepture / mistune / tests / test_toc.py View on Github external
def parse(text):
        md = create_markdown(
            escape=False,
            plugins=[DirectiveToc()]
        )
        html = md(text)
        return html
github lepture / mistune / tests / test_toc.py View on Github external
def test_extract_toc_items(self):
        md = create_markdown(
            renderer='ast',
            plugins=[DirectiveToc()]
        )
        self.assertEqual(extract_toc_items(md, ''), [])
        s = '# H1\n## H2\n# H1'
        result = extract_toc_items(md, s)
        expected = [
            ('toc_1', 'H1', 1),
            ('toc_2', 'H2', 2),
            ('toc_3', 'H1', 1),
        ]
        self.assertEqual(result, expected)