Python mistune.InlineLexer() Examples

The following are 2 code examples of mistune.InlineLexer(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module mistune , or try the search function .
Example #1
Source File: inline.py    From Spirit with MIT License 5 votes vote down vote up
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 
Example #2
Source File: md_helpers.py    From dockerfiles with MIT License 4 votes vote down vote up
def markdown_convert(markdown_string) -> str:
    def _get_contents(text):
        try:
            contents = json.loads(text).get('message', '')
        except json.decoder.JSONDecodeError:
            contents = text
        except AttributeError:
            contents = text

        return contents

    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)
    return md(markdown_string).strip()