Skip to content

Commit 867a77a

Browse files
KyleKingpre-commit-ci[bot]chrisjsewell
authored
👌 Expand support for Python-Markdown in the admon plugin (#94)
- Strip redundant quotes to improve support for [Python Markdown](https://python-markdown.github.io/extensions/admonition/) - Follow Python-Markdown's syntax to parse the title in double quotes and use all other tokens as classes Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Chris Sewell <[email protected]>
1 parent 3829c84 commit 867a77a

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

‎.coveragerc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
[report]
2+
exclude_lines =
3+
pragma: no cover
4+
if TYPE_CHECKING:

‎mdit_py_plugins/admon/index.py

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
# Process admonitions and pass to cb.
2+
23
from __future__ import annotations
34

4-
from typing import TYPE_CHECKING, Callable, Sequence
5+
from contextlib import suppress
6+
import re
7+
from typing import TYPE_CHECKING, Callable, List, Sequence, Tuple
58

69
from markdown_it import MarkdownIt
710
from markdown_it.rules_block import StateBlock
@@ -14,20 +17,34 @@
1417
from markdown_it.utils import EnvType, OptionsDict
1518

1619

17-
def _get_tag(params: str) -> tuple[str, str]:
20+
def _get_multiple_tags(params: str) -> Tuple[List[str], str]:
21+
"""Check for multiple tags when the title is double quoted."""
22+
re_tags = re.compile(r'^\s*(?P<tokens>[^"]+)\s+"(?P<title>.*)"\S*$')
23+
match = re_tags.match(params)
24+
if match:
25+
tags = match["tokens"].strip().split(" ")
26+
return [tag.lower() for tag in tags], match["title"]
27+
raise ValueError("No match found for parameters")
28+
29+
30+
def _get_tag(_params: str) -> Tuple[List[str], str]:
1831
"""Separate the tag name from the admonition title."""
19-
if not params.strip():
20-
return "", ""
32+
params = _params.strip()
33+
if not params:
34+
return [""], ""
35+
36+
with suppress(ValueError):
37+
return _get_multiple_tags(params)
2138

22-
tag, *_title = params.strip().split(" ")
39+
tag, *_title = params.split(" ")
2340
joined = " ".join(_title)
2441

2542
title = ""
2643
if not joined:
2744
title = tag.title()
28-
elif joined != '""':
45+
elif joined != '""': # Specifically check for no title
2946
title = joined
30-
return tag.lower(), title
47+
return [tag.lower()], title
3148

3249

3350
def _validate(params: str) -> bool:
@@ -125,12 +142,13 @@ def admonition(state: StateBlock, startLine: int, endLine: int, silent: bool) ->
125142
# this will prevent lazy continuations from ever going past our end marker
126143
state.lineMax = next_line
127144

128-
tag, title = _get_tag(params)
145+
tags, title = _get_tag(params)
146+
tag = tags[0]
129147

130148
token = state.push("admonition_open", "div", 1)
131149
token.markup = markup
132150
token.block = True
133-
token.attrs = {"class": " ".join(["admonition", tag, *_extra_classes(markup)])}
151+
token.attrs = {"class": " ".join(["admonition", *tags, *_extra_classes(markup)])}
134152
token.meta = {"tag": tag}
135153
token.content = title
136154
token.info = params

‎tests/fixtures/admon.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,32 @@ Shows no title
5353
.
5454

5555

56+
Removes extra quotes from the title
57+
.
58+
!!! danger "Don't try this at home"
59+
...
60+
61+
.
62+
<div class="admonition danger">
63+
<p class="admonition-title">Don't try this at home</p>
64+
<p>...</p>
65+
</div>
66+
.
67+
68+
69+
Parse additional classes to support Python markdown (https://github.com/executablebooks/mdit-py-plugins/issues/93#issuecomment-1601822723)
70+
.
71+
!!! a b c d inline-classes "Note: note about "foo""
72+
...
73+
74+
.
75+
<div class="admonition a b c d inline-classes">
76+
<p class="admonition-title">Note: note about &quot;foo&quot;</p>
77+
<p>...</p>
78+
</div>
79+
.
80+
81+
5682
Closes block after 2 empty lines
5783
.
5884
!!! note

0 commit comments

Comments
 (0)