diff options
author | Arturs Artamonovs <dos21h@gmail.com> | 2023-01-29 10:30:54 +0000 |
---|---|---|
committer | Arturs Artamonovs <dos21h@gmail.com> | 2023-01-29 10:30:54 +0000 |
commit | 66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8 (patch) | |
tree | 7aed7f385826a3bd88c76a373e28c6cfae4f396e /src/mistune/directives/admonition.py | |
parent | 129c1201ea5c4418f0f89ad932633c7cea2439b7 (diff) | |
download | md-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.tar.gz md-site-66fa71a8f11b6ce5e8471b533f67cc3a1fdb85a8.zip |
Update to new mistune, removed old mistune, rewrite to python3
Diffstat (limited to 'src/mistune/directives/admonition.py')
-rw-r--r-- | src/mistune/directives/admonition.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/mistune/directives/admonition.py b/src/mistune/directives/admonition.py new file mode 100644 index 0000000..b380611 --- /dev/null +++ b/src/mistune/directives/admonition.py @@ -0,0 +1,61 @@ +from ._base import DirectivePlugin + + +class Admonition(DirectivePlugin): + SUPPORTED_NAMES = { + "attention", "caution", "danger", "error", "hint", + "important", "note", "tip", "warning", + } + + def parse(self, block, m, state): + name = self.parse_type(m) + attrs = {'name': name} + options = dict(self.parse_options(m)) + if 'class' in options: + attrs['class'] = options['class'] + + title = self.parse_title(m) + if not title: + title = name.capitalize() + + content = self.parse_content(m) + children = [ + { + 'type': 'admonition_title', + 'text': title, + }, + { + 'type': 'admonition_content', + 'children': self.parse_tokens(block, content, state), + } + ] + return { + 'type': 'admonition', + 'children': children, + 'attrs': attrs, + } + + def __call__(self, directive, md): + for name in self.SUPPORTED_NAMES: + directive.register(name, self.parse) + + if md.renderer.NAME == 'html': + md.renderer.register('admonition', render_admonition) + md.renderer.register('admonition_title', render_admonition_title) + md.renderer.register('admonition_content', render_admonition_content) + + +def render_admonition(self, text, name, **attrs): + html = '<section class="admonition ' + name + _cls = attrs.get('class') + if _cls: + html += ' ' + _cls + return html + '">\n' + text + '</section>\n' + + +def render_admonition_title(self, text): + return '<p class="admonition-title">' + text + '</p>\n' + + +def render_admonition_content(self, text): + return text |