Skip to content

Commit

Permalink
refactor: resolve ruff issues
Browse files Browse the repository at this point in the history
  • Loading branch information
KyleKing committed Jan 25, 2024
1 parent c341335 commit 0b04f92
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 23 deletions.
2 changes: 1 addition & 1 deletion mdformat_mkdocs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""An mdformat plugin for mkdocs."""

__version__ = "2.0.0"
__version__ = "2.0.0rc4"

# FYI see source code for available interfaces:
# https://github.com/executablebooks/mdformat/blob/5d9b573ce33bae219087984dd148894c774f41d4/src/mdformat/plugins.py
Expand Down
40 changes: 25 additions & 15 deletions mdformat_mkdocs/_copy_of_mdformat_gfm/plugin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
"""Copied from <https://github.com/hukkin/mdformat-gfm/blob/735781e3fcf95d92cd4537a9712893d00415cd63/src/mdformat_gfm/plugin.py>.
Will be removed when released (Workaround for https://github.com/hukkin/mdformat-gfm/issues/31)
"""

import re

import mdformat.plugins
Expand Down Expand Up @@ -29,7 +35,7 @@ def update_mdit(mdit: MarkdownIt) -> None:

def _strikethrough_renderer(node: RenderTreeNode, context: RenderContext) -> str:
content = "".join(child.render(context) for child in node.children)
return "~~" + content + "~~"
return f"~~{content}~~"


def _render_with_default_renderer(node: RenderTreeNode, context: RenderContext) -> str:
Expand All @@ -47,9 +53,9 @@ def _render_with_default_renderer(node: RenderTreeNode, context: RenderContext)


def _is_task_list_item(node: RenderTreeNode) -> bool:
assert node.type == "list_item"
assert node.type == "list_item" # noqa: S101
classes = node.attrs.get("class", "")
assert isinstance(classes, str)
assert isinstance(classes, str) # noqa: S101
return "task-list-item" in classes


Expand All @@ -61,10 +67,10 @@ def _list_item_renderer(node: RenderTreeNode, context: RenderContext) -> str:
# tasks are annotated by html. We need to remove the HTML.
paragraph_node = node.children[0]
inline_node = paragraph_node.children[0]
assert inline_node.type == "inline"
assert inline_node.children, "inline token must have children"
assert inline_node.type == "inline" # noqa: S101
assert inline_node.children, "inline token must have children" # noqa: S101
html_inline_node = inline_node.children[0]
assert 'class="task-list-item-checkbox"' in html_inline_node.content
assert 'class="task-list-item-checkbox"' in html_inline_node.content # noqa: S101

# This is naughty, shouldn't mutate and rely on `.remove` here
inline_node.children.remove(html_inline_node)
Expand Down Expand Up @@ -98,24 +104,23 @@ def _postprocess_inline(text: str, node: RenderTreeNode, context: RenderContext)
if not isinstance(wrap_mode, int):
return text
if (
node.parent
node.parent # noqa: PLR0916
and node.parent.type == "paragraph"
and not node.parent.previous_sibling
and node.parent.parent
and node.parent.parent.type == "list_item"
and _is_task_list_item(node.parent.parent)
):
text = text.lstrip("\x00")
text = text.lstrip()
text = "xxxx" + text
text = f"xxxx{text.lstrip()}"
return text


def _link_renderer(node: RenderTreeNode, context: RenderContext) -> str:
"""Extend the default link renderer to handle linkify links."""
if node.markup == "linkify":
autolink_url = node.attrs["href"]
assert isinstance(autolink_url, str)
assert isinstance(autolink_url, str) # noqa: S101
startswith_scheme = RE_COMMONMARK_URL_SCHEME.match(autolink_url)
if startswith_scheme and not node.children[0].content.startswith(
startswith_scheme.group(),
Expand All @@ -127,16 +132,21 @@ def _link_renderer(node: RenderTreeNode, context: RenderContext) -> str:
return _render_with_default_renderer(node, context)


def _escape_text(text: str, node: RenderTreeNode, context: RenderContext) -> str:
def _escape_text(
text: str,
node: RenderTreeNode, # noqa: ARG001
context: RenderContext, # noqa: ARG001
) -> str:
# Escape strikethroughs
text = text.replace("~~", "\\~~")

return text
return text.replace("~~", "\\~~")


RENDERERS = {
"s": _strikethrough_renderer,
"list_item": _list_item_renderer,
"link": _link_renderer,
}
POSTPROCESSORS = {"text": _escape_text, "inline": _postprocess_inline}
POSTPROCESSORS = {
"text": _escape_text,
"inline": _postprocess_inline,
}
6 changes: 3 additions & 3 deletions tests/fixtures-semantic-indent.md
Original file line number Diff line number Diff line change
Expand Up @@ -258,9 +258,9 @@ Table
|:---------------|---------:|:---------------------|
| Name | 2| <!-- Comment --> |
.
| Label | Rating | Comment |
|:---------------|---------:|:---------------------|
| Name | 2| <!-- Comment --> |
| Label | Rating | Comment |
| :---- | -----: | :--------------- |
| Name | 2 | <!-- Comment --> |
.

Floating Link
Expand Down
2 changes: 1 addition & 1 deletion tests/format/test_parsed_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest
from markdown_it.utils import read_fixture_file

from mdformat_mkdocs._normalize_list import parse_text
from mdformat_mkdocs._normalize_list import parse_text # noqa: PLC2701

FIXTURE_PATH = Path(__file__).parent / "fixtures/parsed_result.md"
fixtures = read_fixture_file(FIXTURE_PATH)
Expand Down
2 changes: 1 addition & 1 deletion tests/helpers.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mdformat_mkdocs._helpers import separate_indent
from mdformat_mkdocs._helpers import separate_indent # noqa: PLC2701

_SHOW_TEXT = True # PLANNED: Make configurable based on pytest CLI

Expand Down
4 changes: 2 additions & 2 deletions tests/pre-commit-test-numbered.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
list item 2
2. If you store artifacts in the
repository, you need to remember to
compile the application before
every commit.
compile the application before every
commit.
3. Compiled application.

0 comments on commit 0b04f92

Please sign in to comment.