Skip to content

Commit 201383d

Browse files
authored
feat: initial admon plugin attempt (#1)
1 parent 8d85d75 commit 201383d

File tree

7 files changed

+268
-45
lines changed

7 files changed

+268
-45
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ repos:
2727
- flake8-bugbear>=21.3.2
2828
- flake8-builtins>=1.5.3
2929
- flake8-comprehensions>=3.4.0
30+
args: [
31+
--ignore=E501
32+
]

mdformat-admon/plugin.py

Lines changed: 0 additions & 24 deletions
This file was deleted.
File renamed without changes.

mdformat_admon/plugin.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import textwrap
2+
from typing import Mapping
3+
4+
from markdown_it import MarkdownIt
5+
from mdformat.renderer import RenderContext, RenderTreeNode
6+
from mdformat.renderer.typing import Render
7+
from mdit_py_plugins.admon import admon_plugin
8+
9+
10+
def update_mdit(mdit: MarkdownIt) -> None:
11+
"""Update the parser,"""
12+
mdit.use(admon_plugin)
13+
14+
15+
def _render_admon(node: RenderTreeNode, context: RenderContext) -> str:
16+
"""Render a `RenderTreeNode` of type `admonition`.
17+
18+
Primarily based on:
19+
20+
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py#LL24-L40C29
21+
22+
And based on:
23+
24+
- https://github.com/hukkin/mdformat-gfm/blob/cf316a121b6cf35cbff7b0ad6e171f287803f8cb/src/mdformat_gfm/plugin.py
25+
- https://github.com/hukkin/mdformat-toc/blob/42624b6f3468da4f793ec7425da872b030714774/mdformat_toc/plugin.py
26+
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py
27+
28+
"""
29+
separator = "\n\n" # TODO: Is this configurable?
30+
indent = " " * 4 # FIXME: Is this configurable?
31+
title = node.info.strip()
32+
body = f"{node.markup} {title}{separator}"
33+
with context.indented(len(indent)): # Modifies context.env['indent_width']
34+
elements = [child.render(context) for child in node.children[1:]]
35+
body += textwrap.indent(separator.join(elements), indent)
36+
return body
37+
38+
39+
# A mapping from syntax tree node type to a function that renders it.
40+
# This can be used to overwrite renderer functions of existing syntax
41+
# or add support for new syntax.
42+
RENDERERS: Mapping[str, Render] = {"admonition": _render_admon}

pyproject.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,16 @@ classifiers = [
1515
"Topic :: Software Development :: Libraries :: Python Modules",
1616
]
1717
keywords = ["mdformat", "markdown", "markdown-it"]
18-
requires-python = ">=3.7"
18+
requires-python = ">=3.7.2"
1919
dependencies = [
2020
"mdformat >=0.7.0,<0.8.0",
21+
"mdit-py-plugins >=0.3.2",
2122
]
2223
dynamic = ["version", "description"]
2324

2425
[project.optional-dependencies]
2526
test = [
26-
"pytest~=6.0",
27-
"coverage",
27+
"pytest>=6.0",
2828
"pytest-cov",
2929
]
3030
dev = ["pre-commit"]
@@ -33,7 +33,7 @@ dev = ["pre-commit"]
3333
Homepage = "https://github.com/executablebooks/mdformat-admon"
3434

3535
[project.entry-points."mdformat.parser_extension"]
36-
admon = "mdformat_admon"
36+
admonition = "mdformat_admon"
3737

3838
[tool.flit.sdist]
3939
include = []

tests/fixtures.md

Lines changed: 217 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,227 @@
1-
a test
1+
2+
Simple admonition
3+
.
4+
!!! note
5+
*content*
6+
.
7+
!!! note
8+
*content*
9+
.
10+
11+
12+
Could contain block elements too
13+
.
14+
!!! note
15+
### heading
16+
17+
-----------
18+
19+
.
20+
!!! note
21+
### heading
22+
23+
______________________________________________________________________
24+
25+
.
26+
27+
28+
Shows custom title
29+
.
30+
!!! note Custom title
31+
32+
Some text
33+
34+
.
35+
!!! note Custom title
36+
37+
Some text
38+
39+
.
40+
41+
42+
Shows no title
43+
.
44+
!!! note ""
45+
Some text
46+
247
.
3-
This is the input Markdown test,
4-
then below add the expected output.
48+
!!! note ""
49+
Some text
50+
51+
.
52+
53+
54+
Closes block after 2 empty lines
55+
.
56+
!!! note
57+
Some text
58+
59+
60+
A code block
61+
.
62+
!!! note
63+
Some text
64+
65+
66+
A code block
67+
.
68+
69+
70+
Nested blocks
571
.
6-
This is the input Markdown test,
7-
then below add the expected output.
72+
!!! note
73+
!!! note
74+
Some text
75+
76+
code block
877
.
78+
!!! note
79+
!!! note
80+
Some text
981

10-
another test
82+
code block
1183
.
12-
Some *markdown*
1384

14-
- a
15-
- b
16-
* c
85+
86+
Consecutive admonitions
1787
.
18-
Some *markdown*
88+
!!! note
1989

20-
- a
21-
- b
90+
!!! warning
91+
.
92+
!!! note
2293

23-
* c
94+
!!! warning
95+
.
96+
97+
98+
Marker may be indented up to 3 chars
99+
.
100+
!!! note
101+
content
102+
.
103+
!!! note
104+
content
105+
.
106+
107+
108+
But that's a code block
109+
.
110+
!!! note
111+
content
112+
.
113+
!!! note
114+
content
115+
.
116+
117+
118+
Some more indent checks
119+
.
120+
!!! note
121+
not a code block
122+
123+
code block
124+
.
125+
!!! note
126+
not a code block
127+
128+
code block
129+
.
130+
131+
132+
Type could be adjacent to marker
133+
.
134+
!!!note
135+
xxx
136+
137+
.
138+
!!!note
139+
xxx
140+
141+
.
142+
143+
144+
Type could be adjacent to marker and content may be shifted up to 3 chars
145+
.
146+
!!!note
147+
xxx
148+
149+
.
150+
!!!note
151+
xxx
152+
153+
.
154+
155+
156+
Or several spaces apart
157+
.
158+
!!! note
159+
xxx
160+
.
161+
!!! note
162+
xxx
163+
.
164+
165+
166+
Admonitions self-close at the end of the document
167+
.
168+
!!! note
169+
xxx
170+
.
171+
!!! note
172+
xxx
173+
.
174+
175+
176+
They could be nested in lists
177+
.
178+
- !!! note
179+
- a
180+
- b
181+
- !!! warning
182+
- c
183+
- d
184+
.
185+
- !!! note
186+
- a
187+
- b
188+
- !!! warning
189+
- c
190+
- d
191+
.
192+
193+
194+
Or in blockquotes
195+
.
196+
> !!! note
197+
> xxx
198+
> > yyy
199+
> zzz
200+
>
201+
.
202+
> !!! note
203+
> xxx
204+
> > yyy
205+
> zzz
206+
>
207+
.
208+
209+
210+
Renders unknown admonition type
211+
.
212+
!!! unknown title
213+
content
214+
.
215+
!!! unknown title
216+
content
217+
.
218+
219+
220+
Does not render
221+
.
222+
!!!
223+
content
224+
.
225+
!!!
226+
content
24227
.

tests/test_fixtures.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,5 @@
1212
"line,title,text,expected", fixtures, ids=[f[1] for f in fixtures]
1313
)
1414
def test_fixtures(line, title, text, expected):
15-
output = mdformat.text(text, extensions={"admon"})
16-
print(output)
17-
assert output.rstrip() == expected.rstrip(), output
15+
output = mdformat.text(text, extensions={"admonition"})
16+
assert output.rstrip() == expected.rstrip()

0 commit comments

Comments
 (0)