Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions marko/ast_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,13 @@ def render_children(self, element: Element) -> str:
children = getattr(element, "body", None) or getattr(element, "children", None)
if children:
self.indent += 2
if isinstance(children, str): # type: ignore
if isinstance(children, str):
lines.append(
" " * self.indent
+ HTMLRenderer.escape_html(json.dumps(children)[1:-1]) # type: ignore
+ HTMLRenderer.escape_html(json.dumps(children)[1:-1])
)
else:
lines.extend(self.render(child) for child in children) # type: ignore
lines.extend(self.render(child) for child in children)
self.indent -= 2
lines.append(" " * self.indent + f"</{element_name}>")
else:
Expand Down
8 changes: 4 additions & 4 deletions marko/block.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,7 @@ def break_paragraph(cls, source: Source, lazy: bool = False) -> bool:

@classmethod
def parse(cls, source: Source) -> list[str] | SetextHeading:
lines = [cast(str, source.next_line())]
lines = [source.next_line()]
source.consume()
end_parse = False
while not source.exhausted and not end_parse:
Expand Down Expand Up @@ -547,7 +547,7 @@ def match(cls, source: Source) -> bool:
return False
if not source.expect_re(cls.pattern):
return False
next_line = cast(str, source.next_line(False)).expandtabs(4)
next_line = source.next_line(False).expandtabs(4)
prefix_pos = 0
m = re.match(source.prefix, next_line)
if m is not None:
Expand All @@ -571,9 +571,9 @@ def parse(cls, source: Source) -> ListItem:
state = cls(source.context.list_item_info)
state.children = []
with source.under_state(state):
if not source.next_line().strip(): # type: ignore[union-attr]
if not source.next_line().strip():
source.consume()
if not source.next_line() or not source.next_line().strip(): # type: ignore[union-attr]
if not source.next_line() or not source.next_line().strip():
return state
state.children = source.parser.parse_source(source)
if isinstance(state.children[-1], BlankLine):
Expand Down
8 changes: 7 additions & 1 deletion marko/ext/codehilite.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
markdown.convert('```python filename="my_script.py"\nprint('hello world')\n```')
"""

from __future__ import annotations

import json
from typing import TYPE_CHECKING

from pygments import highlight
from pygments.formatters import html
Expand All @@ -27,6 +30,9 @@
from marko import HTMLRenderer
from marko.helpers import MarkoExtension, render_dispatch

if TYPE_CHECKING:
from typing import Any


def _parse_extras(line):
if not line:
Expand All @@ -44,7 +50,7 @@ def _parse_extras(line):


class CodeHiliteRendererMixin:
options = {} # type: dict
options: dict[str, Any] = {}

@render_dispatch(HTMLRenderer)
def render_fenced_code(self, element):
Expand Down
2 changes: 1 addition & 1 deletion marko/ext/toc.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

import re

from marko.helpers import MarkoExtension, render_dispatch # type: ignore
from marko.helpers import MarkoExtension, render_dispatch
from marko.html_renderer import HTMLRenderer

try:
Expand Down
4 changes: 2 additions & 2 deletions marko/html_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HTMLRenderer(Renderer):

def render_paragraph(self, element: block.Paragraph) -> str:
children = self.render_children(element)
if element._tight: # type: ignore
if element._tight:
return children
else:
return f"<p>{children}</p>\n"
Expand All @@ -38,7 +38,7 @@ def render_list(self, element: block.List) -> str:
)

def render_list_item(self, element: block.ListItem) -> str:
if len(element.children) == 1 and getattr(element.children[0], "_tight", False): # type: ignore
if len(element.children) == 1 and getattr(element.children[0], "_tight", False):
sep = ""
else:
sep = "\n"
Expand Down
2 changes: 1 addition & 1 deletion marko/inline.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class Literal(InlineElement):

@classmethod
def strip_backslash(cls, text: str) -> str:
return cls.pattern.sub(r"\1", text) # type: ignore[unio]
return cls.pattern.sub(r"\1", text)


class LineBreak(InlineElement):
Expand Down
2 changes: 1 addition & 1 deletion marko/md_renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def render_code_block(self, element: block.CodeBlock) -> str:
return "\n".join(lines) + "\n"

def render_html_block(self, element: block.HTMLBlock) -> str:
result = self._prefix + element.body + "\n" # type: ignore[attr-defined]
result = self._prefix + element.body + "\n"
self._prefix = self._second_prefix
return result

Expand Down
5 changes: 3 additions & 2 deletions marko/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,12 @@ def render_children(self, element: Any) -> Any:

:param element: a branch node who has children attribute.
"""
rendered = [self.render(child) for child in element.children] # type: ignore
rendered = [self.render(child) for child in element.children]
return "".join(rendered)


_F = TypeVar("_F", bound=Callable)
_FT = TypeVar("_FT")
_F = TypeVar("_F", bound=Callable[..., _FT])


def force_delegate(func: _F) -> _F:
Expand Down
4 changes: 2 additions & 2 deletions marko/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ def expect_re(self, regexp: Pattern[str] | str) -> Match[str] | None:
"""
prefix_len = self.match_prefix(
self.prefix,
self.next_line(require_prefix=False), # type: ignore
self.next_line(require_prefix=False),
)
if prefix_len >= 0:
match = self._expect_re(regexp, self.pos + prefix_len)
Expand Down Expand Up @@ -154,4 +154,4 @@ def reset(self) -> None:
def _update_prefix(self) -> None:
for s in self._states:
if hasattr(s, "_second_prefix"):
s._prefix = s._second_prefix # type: ignore
s._prefix = s._second_prefix
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ dev = [
"pytest",
"pytest-cov",
"mypy>=0.950",
"types-Pygments>=2.19.0.20251121",
]
doc = [
"marko[toc]",
Expand All @@ -75,3 +76,8 @@ without-hashes = true

[tool.isort]
profile = "black"

[[tool.mypy.overrides]]
# objprint is typed, just missing a py.typed marker
module = ["objprint.*"]
follow_untyped_imports = true
Loading