Skip to content

Commit

Permalink
Moving to HTML generator pattern with iter_html
Browse files Browse the repository at this point in the history
  • Loading branch information
Zohaib Sibte Hassan committed Dec 31, 2023
1 parent d01ae61 commit d3398fa
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions htmxido/htmx.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from collections.abc import Collection
from html import escape
from types import GeneratorType
from typing import Any, Sequence, Callable, Tuple, Dict, List, Iterable
from typing import Any, Generator, Sequence, Callable, Tuple, Dict, List, Iterable

TagPreProcessor = Callable[[Sequence[Any], Dict[str, Any]], Tuple[Sequence[Any], Dict[str, Any]]]

Expand Down Expand Up @@ -94,33 +94,39 @@ def __call__(self, *args: [Any], **kwds: Dict[str, Any]) -> Any:
if len(args) > 0:
self._content = _flatten(args)
return self

def iter_html(self) -> Generator[str, None, None]:
yield f"<{self._tag}"

def __str__(self) -> str:
ret = [f"<{self._tag}"]
for key, value in self._attributes.items():
if value is not None:
ret.append(f" {key}=\"{escape(value)}\"")
yield f" {key}=\"{escape(value)}\""
else:
ret.append(f" {key}")
yield f" {key}"

if self._tag in single_tags:
ret.append(" />")
yield " />"
elif self._content is not None:
ret.append(">")
yield ">"
for c in self._content:
if isinstance(c, DOMElement) or isinstance(c, RawContent):
ret.append(str(c))
if isinstance(c, DOMElement):
for chunk in c.iter_html():
yield chunk
elif isinstance(c, RawContent):
yield str(c)
elif isinstance(c, str) and self._tag in default_raw:
ret.append(c)
yield c
elif isinstance(c, str):
ret.append(escape(c))
yield escape(c)
else:
raise HTMXError("Unknown type for variable", c)
ret.append(f"</{self._tag}>")
yield f"</{self._tag}>"
else:
ret.append(f"></{self._tag}>")
yield f"></{self._tag}>"

return ''.join(ret)
def __str__(self) -> str:
chunks = [chunk for chunk in self.iter_html()]
return ''.join(chunks)


class DOM:
Expand Down

0 comments on commit d3398fa

Please sign in to comment.