Skip to content

Commit

Permalink
feat: initial admon plugin attempt (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing authored Dec 14, 2022
1 parent 8d85d75 commit 201383d
Show file tree
Hide file tree
Showing 7 changed files with 268 additions and 45 deletions.
3 changes: 3 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ repos:
- flake8-bugbear>=21.3.2
- flake8-builtins>=1.5.3
- flake8-comprehensions>=3.4.0
args: [
--ignore=E501
]
24 changes: 0 additions & 24 deletions mdformat-admon/plugin.py

This file was deleted.

File renamed without changes.
42 changes: 42 additions & 0 deletions mdformat_admon/plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import textwrap
from typing import Mapping

from markdown_it import MarkdownIt
from mdformat.renderer import RenderContext, RenderTreeNode
from mdformat.renderer.typing import Render
from mdit_py_plugins.admon import admon_plugin


def update_mdit(mdit: MarkdownIt) -> None:
"""Update the parser,"""
mdit.use(admon_plugin)


def _render_admon(node: RenderTreeNode, context: RenderContext) -> str:
"""Render a `RenderTreeNode` of type `admonition`.
Primarily based on:
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py#LL24-L40C29
And based on:
- https://github.com/hukkin/mdformat-gfm/blob/cf316a121b6cf35cbff7b0ad6e171f287803f8cb/src/mdformat_gfm/plugin.py
- https://github.com/hukkin/mdformat-toc/blob/42624b6f3468da4f793ec7425da872b030714774/mdformat_toc/plugin.py
- https://github.com/executablebooks/mdformat-footnote/blob/80852fc20cfba7fd0330b9ac7a1a4df983542942/mdformat_footnote/plugin.py
"""
separator = "\n\n" # TODO: Is this configurable?
indent = " " * 4 # FIXME: Is this configurable?
title = node.info.strip()
body = f"{node.markup} {title}{separator}"
with context.indented(len(indent)): # Modifies context.env['indent_width']
elements = [child.render(context) for child in node.children[1:]]
body += textwrap.indent(separator.join(elements), indent)
return body


# A mapping from syntax tree node type to a function that renders it.
# This can be used to overwrite renderer functions of existing syntax
# or add support for new syntax.
RENDERERS: Mapping[str, Render] = {"admonition": _render_admon}
8 changes: 4 additions & 4 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ classifiers = [
"Topic :: Software Development :: Libraries :: Python Modules",
]
keywords = ["mdformat", "markdown", "markdown-it"]
requires-python = ">=3.7"
requires-python = ">=3.7.2"
dependencies = [
"mdformat >=0.7.0,<0.8.0",
"mdit-py-plugins >=0.3.2",
]
dynamic = ["version", "description"]

[project.optional-dependencies]
test = [
"pytest~=6.0",
"coverage",
"pytest>=6.0",
"pytest-cov",
]
dev = ["pre-commit"]
Expand All @@ -33,7 +33,7 @@ dev = ["pre-commit"]
Homepage = "https://github.com/executablebooks/mdformat-admon"

[project.entry-points."mdformat.parser_extension"]
admon = "mdformat_admon"
admonition = "mdformat_admon"

[tool.flit.sdist]
include = []
Expand Down
231 changes: 217 additions & 14 deletions tests/fixtures.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,227 @@
a test

Simple admonition
.
!!! note
*content*
.
!!! note
*content*
.


Could contain block elements too
.
!!! note
### heading

-----------

.
!!! note
### heading

______________________________________________________________________

.


Shows custom title
.
!!! note Custom title

Some text

.
!!! note Custom title

Some text

.


Shows no title
.
!!! note ""
Some text

.
This is the input Markdown test,
then below add the expected output.
!!! note ""
Some text

.


Closes block after 2 empty lines
.
!!! note
Some text


A code block
.
!!! note
Some text


A code block
.


Nested blocks
.
This is the input Markdown test,
then below add the expected output.
!!! note
!!! note
Some text

code block
.
!!! note
!!! note
Some text

another test
code block
.
Some *markdown*

- a
- b
* c

Consecutive admonitions
.
Some *markdown*
!!! note

- a
- b
!!! warning
.
!!! note

* c
!!! warning
.


Marker may be indented up to 3 chars
.
!!! note
content
.
!!! note
content
.


But that's a code block
.
!!! note
content
.
!!! note
content
.


Some more indent checks
.
!!! note
not a code block

code block
.
!!! note
not a code block

code block
.


Type could be adjacent to marker
.
!!!note
xxx

.
!!!note
xxx

.


Type could be adjacent to marker and content may be shifted up to 3 chars
.
!!!note
xxx

.
!!!note
xxx

.


Or several spaces apart
.
!!! note
xxx
.
!!! note
xxx
.


Admonitions self-close at the end of the document
.
!!! note
xxx
.
!!! note
xxx
.


They could be nested in lists
.
- !!! note
- a
- b
- !!! warning
- c
- d
.
- !!! note
- a
- b
- !!! warning
- c
- d
.


Or in blockquotes
.
> !!! note
> xxx
> > yyy
> zzz
>
.
> !!! note
> xxx
> > yyy
> zzz
>
.


Renders unknown admonition type
.
!!! unknown title
content
.
!!! unknown title
content
.


Does not render
.
!!!
content
.
!!!
content
.
5 changes: 2 additions & 3 deletions tests/test_fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,5 @@
"line,title,text,expected", fixtures, ids=[f[1] for f in fixtures]
)
def test_fixtures(line, title, text, expected):
output = mdformat.text(text, extensions={"admon"})
print(output)
assert output.rstrip() == expected.rstrip(), output
output = mdformat.text(text, extensions={"admonition"})
assert output.rstrip() == expected.rstrip()

0 comments on commit 201383d

Please sign in to comment.