generated from executablebooks/mdformat-plugin
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support real world use cases (#1)
- Loading branch information
Showing
10 changed files
with
361 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
python 3.10.5 3.8.4 3.9.4 3.7.12 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,70 @@ | ||
from functools import partial | ||
from typing import Mapping | ||
import re | ||
from typing import Dict, Mapping | ||
|
||
from markdown_it import MarkdownIt | ||
from mdformat.renderer import RenderContext, RenderTreeNode | ||
from mdformat.renderer.typing import Render | ||
|
||
_MKDOCS_INDENT_COUNT = 4 | ||
"""Use 4-spaces for mkdocs.""" | ||
|
||
|
||
def update_mdit(mdit: MarkdownIt) -> None: | ||
"""No changes to markdown parsing are necessary.""" | ||
"""Ensure that 4-spaces are converted to HTML correctly.""" | ||
... | ||
|
||
|
||
def _render_list(node: RenderTreeNode, context: RenderContext, bullet: str) -> str: | ||
"""Render a `RenderTreeNode` consistent with `mkdocs`.""" | ||
_RE_INDENT = re.compile(r"(?P<indent>\s*)(?P<content>.*)") | ||
"""Match `indent` and `content` against line`.""" | ||
|
||
_RE_LIST_ITEM = re.compile(r"(?P<bullet>[\-\*\d\.]+)\s+(?P<item>.+)") | ||
"""Match `bullet` and `item` against `content`.""" | ||
|
||
|
||
def _normalize_list(text: str, node: RenderTreeNode, context: RenderContext) -> str: | ||
"""No changes to markdown parsing are necessary.""" | ||
eol = "\n" # PLANNED: What about carriage returns? | ||
indent = " " * _MKDOCS_INDENT_COUNT | ||
|
||
rendered = "" | ||
indent = " " * 4 | ||
with context.indented(len(indent)): # Modifies context.env['indent_width'] | ||
inner_indent = indent * (context.env["indent_width"] // len(indent) - 1) | ||
for child in node.children: | ||
content = child.render(context) | ||
rendered += f"{inner_indent}{bullet} {content}\n" | ||
return rendered | ||
|
||
|
||
# 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] = { | ||
"bullet_list": partial(_render_list, bullet="-"), | ||
"ordered_list": partial(_render_list, bullet="1."), | ||
last_indent = "" | ||
indent_counter = 0 | ||
|
||
indent_lookup: Dict[str, int] = {} | ||
for line in text.split(eol): | ||
match = _RE_INDENT.match(line) | ||
assert match is not None # for pylint | ||
list_match = _RE_LIST_ITEM.match(match["content"]) | ||
new_line = line | ||
if list_match: | ||
new_bullet = "-" if list_match["bullet"] in {"-", "*"} else "1." | ||
new_line = f'{new_bullet} {list_match["item"]}' | ||
|
||
this_indent = match["indent"] | ||
if this_indent: | ||
indent_diff = len(this_indent) - len(last_indent) | ||
if indent_diff == 0: | ||
... | ||
elif this_indent in indent_lookup: | ||
indent_counter = indent_lookup[this_indent] | ||
elif indent_diff > 0: | ||
indent_counter += 1 | ||
indent_lookup[this_indent] = indent_counter | ||
else: | ||
raise NotImplementedError(f"Error in indentation of: `{line}`") | ||
else: | ||
indent_counter = 0 | ||
last_indent = match["indent"] | ||
new_indent = indent * indent_counter | ||
rendered += f"{new_indent}{new_line.strip()}{eol}" | ||
return rendered.rstrip() | ||
|
||
|
||
# # 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] = {} | ||
POSTPROCESSORS = { | ||
"bullet_list": _normalize_list, | ||
"ordered_list": _normalize_list, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# Other Tests | ||
|
||
## Footnotes | ||
|
||
FYI: Requires `mdformat-footnote`: | ||
|
||
Here is a simple footnote[^1]. | ||
|
||
A footnote can also have multiple lines[^2]. | ||
|
||
You can also use words, to fit your writing style more closely[^note]. | ||
|
||
[^1]: My reference. | ||
[^2]: Every new line should be prefixed with 2 spaces.\ | ||
This allows you to have a footnote with multiple lines. | ||
[^note]: Named footnotes will still render with numbers instead of the text but allow easier identification and linking.\ | ||
This footnote also has been made with a different syntax using 4 spaces for new lines. |
Oops, something went wrong.