Skip to content

Commit

Permalink
Change indentation to be added within rendering process, rather than …
Browse files Browse the repository at this point in the history
…at end
  • Loading branch information
MaddyGuthridge committed May 2, 2024
1 parent be4332b commit d6cb68b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
20 changes: 12 additions & 8 deletions pyhtml/__tag_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def _escape_children(self) -> bool:
"""
return True

def _render(self) -> list[str]:
def _render(self, indent: int) -> list[str]:
"""
Renders tag and its children to a list of strings where each string is
a single line of output
Expand All @@ -105,7 +105,7 @@ def _render(self) -> list[str]:
))

# Tag and attributes
opening = f"<{self._get_tag_name()}"
opening = f"{' ' * indent}<{self._get_tag_name()}"

# Add pre-content
if (pre := self._get_tag_pre_content()) is not None:
Expand All @@ -123,18 +123,22 @@ def _render(self) -> list[str]:
out = [opening]
# Children
out.extend(
util.render_children(self.children, self._escape_children())
util.render_children(
self.children,
self._escape_children(),
indent + 2,
)
)
# Closing tag
out.append(f"</{self._get_tag_name()}>")
out.append(f"{' ' * indent}</{self._get_tag_name()}>")

return out

def render(self) -> str:
"""
Render this tag and its contents to a string
"""
return '\n'.join(self._render())
return '\n'.join(self._render(0))

def __str__(self) -> str:
return self.render()
Expand All @@ -151,7 +155,7 @@ def __init__(self, **attributes: AttributeType) -> None:
# Self-closing tags don't allow children
super().__init__(**attributes)

def _render(self) -> list[str]:
def _render(self, indent: int) -> list[str]:
"""
Renders tag and its children to a list of strings where each string is
a single line of output
Expand All @@ -162,8 +166,8 @@ def _render(self) -> list[str]:
))
if len(attributes):
return [
f"<{self._get_tag_name()} "
f"{' ' * indent}<{self._get_tag_name()} "
f"{util.render_tag_attributes(attributes)}/>"
]
else:
return [f"<{self._get_tag_name()}/>"]
return [f"{' ' * indent}<{self._get_tag_name()}/>"]
18 changes: 9 additions & 9 deletions pyhtml/__tags/comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,17 @@ def _get_tag_name(self) -> str:
# and is never used since we override _render
return '!--' # pragma: no cover

def _render(self) -> list[str]:
def _render(self, indent: int) -> list[str]:
"""
Override of render, to render comments
"""

return [
'<!--',
*util.increase_indent(
return util.increase_indent(
['<!--']
+ util.increase_indent(
util.escape_string(self.comment_data).splitlines(),
# FIXME: Yucky magic number
2,
),
'-->'
]
indent+2,
)
+ ['-->'],
indent,
)
5 changes: 3 additions & 2 deletions pyhtml/__tags/dangerous_raw_html.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Definition for the DangerousRawHtml tag.
"""
from ..__tag_base import Tag
from ..__util import increase_indent


class DangerousRawHtml(Tag):
Expand Down Expand Up @@ -44,5 +45,5 @@ def _get_tag_name(self) -> str:
# and is never used since we override _render
return '!!!DANGEROUS RAW HTML!!!' # pragma: no cover

def _render(self) -> list[str]:
return self.html_data.splitlines()
def _render(self, indent: int) -> list[str]:
return increase_indent(self.html_data.splitlines(), indent)
17 changes: 11 additions & 6 deletions pyhtml/__util.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,26 +99,29 @@ def filter_attributes(attributes: dict[str, Any]) -> dict[str, Any]:
def render_inline_element(
ele: ChildElementType,
escape_strings: bool,
indent: int,
) -> list[str]:
"""
Render an element inline
"""
from .__tag_base import Tag
if isinstance(ele, Tag):
return ele._render()
return ele._render(indent)
elif isinstance(ele, type) and issubclass(ele, Tag):
return ele()._render()
e = ele()
return e._render(indent)
else:
# Remove newlines from strings when inline rendering
if escape_strings:
return [escape_string(str(ele))]
return increase_indent([escape_string(str(ele))], indent)
else:
return [str(ele)]
return increase_indent([str(ele)], indent)


def render_children(
children: list[ChildElementType],
escape_strings: bool,
indent: int,
) -> list[str]:
"""
Render child elements of tags.
Expand All @@ -127,15 +130,17 @@ def render_children(
"""
rendered = []
for ele in children:
rendered.extend(render_inline_element(ele, escape_strings))
return increase_indent(rendered, 2)
rendered.extend(render_inline_element(ele, escape_strings, indent))
return rendered


def flatten_list(the_list: list[ChildrenType]) -> list[ChildElementType]:
"""
Flatten a list by taking any list elements and inserting their items
individually. Note that other iterables (such as str and tuple) are not
flattened.
FIXME: Currently doesn't support lists of lists
"""
result: list[ChildElementType] = []
for item in the_list:
Expand Down

0 comments on commit d6cb68b

Please sign in to comment.