Skip to content

Commit d6cb68b

Browse files
Change indentation to be added within rendering process, rather than at end
1 parent be4332b commit d6cb68b

File tree

4 files changed

+35
-25
lines changed

4 files changed

+35
-25
lines changed

pyhtml/__tag_base.py

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def _escape_children(self) -> bool:
9494
"""
9595
return True
9696

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

107107
# Tag and attributes
108-
opening = f"<{self._get_tag_name()}"
108+
opening = f"{' ' * indent}<{self._get_tag_name()}"
109109

110110
# Add pre-content
111111
if (pre := self._get_tag_pre_content()) is not None:
@@ -123,18 +123,22 @@ def _render(self) -> list[str]:
123123
out = [opening]
124124
# Children
125125
out.extend(
126-
util.render_children(self.children, self._escape_children())
126+
util.render_children(
127+
self.children,
128+
self._escape_children(),
129+
indent + 2,
130+
)
127131
)
128132
# Closing tag
129-
out.append(f"</{self._get_tag_name()}>")
133+
out.append(f"{' ' * indent}</{self._get_tag_name()}>")
130134

131135
return out
132136

133137
def render(self) -> str:
134138
"""
135139
Render this tag and its contents to a string
136140
"""
137-
return '\n'.join(self._render())
141+
return '\n'.join(self._render(0))
138142

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

154-
def _render(self) -> list[str]:
158+
def _render(self, indent: int) -> list[str]:
155159
"""
156160
Renders tag and its children to a list of strings where each string is
157161
a single line of output
@@ -162,8 +166,8 @@ def _render(self) -> list[str]:
162166
))
163167
if len(attributes):
164168
return [
165-
f"<{self._get_tag_name()} "
169+
f"{' ' * indent}<{self._get_tag_name()} "
166170
f"{util.render_tag_attributes(attributes)}/>"
167171
]
168172
else:
169-
return [f"<{self._get_tag_name()}/>"]
173+
return [f"{' ' * indent}<{self._get_tag_name()}/>"]

pyhtml/__tags/comment.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,17 @@ def _get_tag_name(self) -> str:
4242
# and is never used since we override _render
4343
return '!--' # pragma: no cover
4444

45-
def _render(self) -> list[str]:
45+
def _render(self, indent: int) -> list[str]:
4646
"""
4747
Override of render, to render comments
4848
"""
4949

50-
return [
51-
'<!--',
52-
*util.increase_indent(
50+
return util.increase_indent(
51+
['<!--']
52+
+ util.increase_indent(
5353
util.escape_string(self.comment_data).splitlines(),
54-
# FIXME: Yucky magic number
55-
2,
56-
),
57-
'-->'
58-
]
54+
indent+2,
55+
)
56+
+ ['-->'],
57+
indent,
58+
)

pyhtml/__tags/dangerous_raw_html.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
Definition for the DangerousRawHtml tag.
55
"""
66
from ..__tag_base import Tag
7+
from ..__util import increase_indent
78

89

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

47-
def _render(self) -> list[str]:
48-
return self.html_data.splitlines()
48+
def _render(self, indent: int) -> list[str]:
49+
return increase_indent(self.html_data.splitlines(), indent)

pyhtml/__util.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,26 +99,29 @@ def filter_attributes(attributes: dict[str, Any]) -> dict[str, Any]:
9999
def render_inline_element(
100100
ele: ChildElementType,
101101
escape_strings: bool,
102+
indent: int,
102103
) -> list[str]:
103104
"""
104105
Render an element inline
105106
"""
106107
from .__tag_base import Tag
107108
if isinstance(ele, Tag):
108-
return ele._render()
109+
return ele._render(indent)
109110
elif isinstance(ele, type) and issubclass(ele, Tag):
110-
return ele()._render()
111+
e = ele()
112+
return e._render(indent)
111113
else:
112114
# Remove newlines from strings when inline rendering
113115
if escape_strings:
114-
return [escape_string(str(ele))]
116+
return increase_indent([escape_string(str(ele))], indent)
115117
else:
116-
return [str(ele)]
118+
return increase_indent([str(ele)], indent)
117119

118120

119121
def render_children(
120122
children: list[ChildElementType],
121123
escape_strings: bool,
124+
indent: int,
122125
) -> list[str]:
123126
"""
124127
Render child elements of tags.
@@ -127,15 +130,17 @@ def render_children(
127130
"""
128131
rendered = []
129132
for ele in children:
130-
rendered.extend(render_inline_element(ele, escape_strings))
131-
return increase_indent(rendered, 2)
133+
rendered.extend(render_inline_element(ele, escape_strings, indent))
134+
return rendered
132135

133136

134137
def flatten_list(the_list: list[ChildrenType]) -> list[ChildElementType]:
135138
"""
136139
Flatten a list by taking any list elements and inserting their items
137140
individually. Note that other iterables (such as str and tuple) are not
138141
flattened.
142+
143+
FIXME: Currently doesn't support lists of lists
139144
"""
140145
result: list[ChildElementType] = []
141146
for item in the_list:

0 commit comments

Comments
 (0)