Skip to content

Commit

Permalink
fix: make a dummy document when rendering a non-document element
Browse files Browse the repository at this point in the history
Close #188

Signed-off-by: Frost Ming <me@frostming.com>
  • Loading branch information
frostming committed Feb 22, 2024
1 parent 625f8a4 commit d515c96
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## Unreleased

- Add pretty representation for the AST for debugging purpose. An extra group `repr` is added for more readable output.
- Make a dummy `Document` element if the element to render is not a `Document` instance.

## v2.0.2(2023-11-16)

Expand Down
1 change: 0 additions & 1 deletion marko/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ def render(self, parsed: Document) -> str:
Override this to handle parsed result.
"""
self._setup_extensions()
self.renderer.root_node = parsed
with self.renderer as r:
return r.render(parsed)

Expand Down
8 changes: 7 additions & 1 deletion marko/renderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ def __enter__(self: _T) -> _T:

def __exit__(self, *args: Any) -> None:
html._charref = _charref_bak # type: ignore[attr-defined]
self.root_node = None

def render(self, element: Element) -> Any:
"""Renders the given element to string.
Expand All @@ -60,7 +61,12 @@ def render(self, element: Element) -> Any:
"""
# Store the root node since it may be required by the render functions
if not self.root_node: # pragma: no cover
self.root_node = element # type: ignore
if isinstance(element, Document):
self.root_node = element
else:
# Make a dummy root node from it
self.root_node = Document()
self.root_node.children = [element]
if hasattr(element, "get_type"):
func_name = "render_" + element.get_type(snake_case=True)
render_func = getattr(self, func_name, None)
Expand Down

0 comments on commit d515c96

Please sign in to comment.