Skip to content

Commit

Permalink
Refactor admonition directive. Remove special process for title.
Browse files Browse the repository at this point in the history
  • Loading branch information
lepture committed Nov 27, 2022
1 parent c50d06f commit 325116b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 26 deletions.
46 changes: 33 additions & 13 deletions mistune/directives/admonition.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,29 @@ class Admonition(DirectivePlugin):
}

def parse(self, block, m, state):
options = self.parse_options(m)
name = self.parse_name(m)
attrs = {'name': name}
options = dict(self.parse_options(m))
if 'class' in options:
attrs['class'] = options['class']

title = self.parse_title(m)
attrs = {'name': name, 'options': options}
if not title:
title = name.capitalize()

content = self.parse_content(m)
children = self.parse_tokens(block, content, state)
children = [
{
'type': 'admonition_title',
'text': title,
},
{
'type': 'admonition_content',
'children': self.parse_tokens(block, content, state),
}
]
return {
'type': 'admonition',
'title': title or '',
'children': children,
'attrs': attrs,
}
Expand All @@ -28,14 +41,21 @@ def __call__(self, directive, md):

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(self, text, name, title="", options=None):
html = '<section class="admonition ' + name + '">\n'
if not title:
title = name.capitalize()
if title:
html += '<p class="admonition-title">' + title + '</p>\n'
if text:
html += text
return html + '</section>\n'
def render_admonition_content(self, text):
return text
3 changes: 1 addition & 2 deletions mistune/directives/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def parse(self, block, m, state):
'max_level': max_level,
'collapse': collapse,
}
return {'type': 'toc', 'title': title or '', 'attrs': attrs}
return {'type': 'toc', 'text': title or '', 'attrs': attrs}

def toc_hook(self, md, state):
sections = []
Expand Down Expand Up @@ -85,7 +85,6 @@ def __call__(self, directive, md):
def render_html_toc(renderer, title, collapse=False, **attrs):
if not title:
title = 'Table of Contents'

toc = attrs['toc']
content = render_toc_ul(attrs['toc'])

Expand Down
2 changes: 0 additions & 2 deletions mistune/markdown.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,6 @@ def _iter_render(self, tokens, state):
text = tok.pop('text')
# process inline text
tok['children'] = self.inline(text.strip(), state.env)
if 'title' in tok:
tok['title'] = self.inline(tok['title'], state.env)
yield tok

def parse(self, s: str, state: Optional[BlockState]=None):
Expand Down
9 changes: 0 additions & 9 deletions mistune/renderers/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ def render_token(self, token, state):
func = self._get_method(token['type'])
attrs = token.get('attrs')

if 'title' in token:
title = self.render_tokens(token['title'], state)
if attrs:
_attrs = {'title': title}
_attrs.update(attrs)
attrs = _attrs
else:
attrs = {'title': title}

if 'raw' in token:
text = token['raw']
elif 'children' in token:
Expand Down

0 comments on commit 325116b

Please sign in to comment.