Skip to content

Commit

Permalink
minor performance tweaks (#12)
Browse files Browse the repository at this point in the history
* append method

* tweaks

* version bump
  • Loading branch information
keithasaurus authored Jan 2, 2024
1 parent 1aeb016 commit fef0ca9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "simple-html"
version = "1.2.1"
version = "1.2.2"
readme = "README.md"
description = "Template-less html rendering in Python"
authors = ["Keith Philpott <[email protected]>"]
Expand Down
28 changes: 14 additions & 14 deletions simple_html/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from html import escape
from types import GeneratorType
from typing import Tuple, Union, Dict, List, FrozenSet, Generator, Iterable, Any
from typing import Tuple, Union, Dict, List, FrozenSet, Generator, Iterable, Any, Callable


class SafeString:
Expand Down Expand Up @@ -128,9 +128,9 @@ def __call__(
# attributes values are always escaped (when they are `str`s)
if key not in _common_safe_attribute_names:
key = (
key.safe_str
if isinstance(key, SafeString)
else escape_attribute_key(key)
escape_attribute_key(key)
if isinstance(key, str)
else key.safe_str
)

if isinstance(val, str):
Expand Down Expand Up @@ -266,25 +266,25 @@ def __call__(
wbr = Tag("wbr")


def _render(nodes: Iterable[Node], strs: List[str]) -> None:
def _render(nodes: Iterable[Node], append_to_list: Callable[[str], None]) -> None:
"""
mutate a list instead of constantly rendering strings
"""
for node in nodes:
if type(node) is tuple:
strs.append(node[0])
_render(node[1], strs)
strs.append(node[2])
append_to_list(node[0])
_render(node[1], append_to_list)
append_to_list(node[2])
elif isinstance(node, SafeString):
strs.append(node.safe_str)
append_to_list(node.safe_str)
elif isinstance(node, str):
strs.append(escape(node))
append_to_list(escape(node))
elif isinstance(node, Tag):
strs.append(node.rendered)
append_to_list(node.rendered)
elif isinstance(node, list):
_render(node, strs)
_render(node, append_to_list)
elif isinstance(node, GeneratorType):
_render(node, strs)
_render(node, append_to_list)
else:
raise TypeError(f"Got unknown type: {type(node)}")

Expand Down Expand Up @@ -526,6 +526,6 @@ def render_styles(

def render(*nodes: Node) -> str:
results: List[str] = []
_render(nodes, results)
_render(nodes, results.append)

return "".join(results)

0 comments on commit fef0ca9

Please sign in to comment.