Skip to content

Commit

Permalink
RFC: html: add parse_html
Browse files Browse the repository at this point in the history
Signed-off-by: Florian Scherf <[email protected]>
  • Loading branch information
fscherf committed Jul 7, 2023
1 parent d0b50b9 commit b8ddd67
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
16 changes: 13 additions & 3 deletions lona/html/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,12 @@ def handle_endtag(self, tag):
self.set_current_node(self._node.parent)


def html_string_to_node_list(html_string, use_high_level_nodes=True,
node_classes=None):
def parse_html(
html_string: str,
use_high_level_nodes: bool = True,
node_classes: Dict[str, AbstractNode] | None = None,
flat: bool = True,
) -> AbstractNode | List[AbstractNode]:

root_node = Node()
nodes = []
Expand All @@ -221,6 +225,9 @@ def html_string_to_node_list(html_string, use_high_level_nodes=True,
node.remove()
nodes.append(node)

if flat and len(nodes) == 1:
return nodes[0]

return nodes


Expand All @@ -230,6 +237,8 @@ def HTML(
node_classes: Dict[str, AbstractNode] | None = None,
) -> AbstractNode:

# TODO: remove HTML parsing in 2.0

_nodes: List[AbstractNode] = []

for node in nodes:
Expand All @@ -243,10 +252,11 @@ def HTML(

# html string
elif '<' in node or '>' in node:
parsed_nodes = html_string_to_node_list(
parsed_nodes = parse_html(
html_string=node,
use_high_level_nodes=use_high_level_nodes,
node_classes=node_classes or {},
flat=False,
)

if len(nodes) > 1:
Expand Down
5 changes: 3 additions & 2 deletions lona/html/widgets.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from lona.html.parsing import html_string_to_node_list
from lona.html.parsing import parse_html
from lona.html.text_node import TextNode
from lona.html.widget import Widget

Expand All @@ -22,10 +22,11 @@ def __init__(self, *nodes, use_high_level_nodes=True, node_classes=None):
self.nodes.append(HTML(node))

else:
self.nodes = html_string_to_node_list(
self.nodes = parse_html(
html_string=node,
use_high_level_nodes=use_high_level_nodes,
node_classes=node_classes or {},
flat=False,
)

else:
Expand Down

0 comments on commit b8ddd67

Please sign in to comment.