From 61c0b0d134d05c1647fa3356486d209bac87d8f6 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Thu, 2 Jan 2025 22:12:39 +0200 Subject: Implement `GenericValue`, `InnerHtml` and `OuterHtml` parsers. --- README.md | 6 +++--- htmtl/htmtl.py | 6 ++++++ htmtl/parsers/generic_value.py | 18 ++++++++++++++++++ htmtl/parsers/inner_html.py | 16 ++++++++++++++++ htmtl/parsers/outer_html.py | 14 ++++++++++++++ 5 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 htmtl/parsers/generic_value.py create mode 100644 htmtl/parsers/inner_html.py create mode 100644 htmtl/parsers/outer_html.py diff --git a/README.md b/README.md index a980e2e..7578e80 100644 --- a/README.md +++ b/README.md @@ -339,14 +339,14 @@ HTMTL is built upon the [Dompa](https://github.com/askonomm/dompa) HTML parser, #### List of built-in parsers -- `htmtl.parsers.GenericValue` - Parser the `:*` attributes. (**soon**) +- `htmtl.parsers.GenericValue` - Parser the `:*` attributes. - `htmtl.parsers.If` - Parser the `if` attributes. (**soon**) - `htmtl.parsers.Unless` - Parser the `unless` attributes. (**soon**) - `htmtl.parsers.InnerPartial` - Parser the `inner-partial` attributes. (**soon**) -- `htmtl.parsers.InnerHtml` - Parser the `inner-html` attributes. (**soon**) +- `htmtl.parsers.InnerHtml` - Parser the `inner-html` attributes. - `htmtl.parsers.InnerText` - Parser the `inner-text` attributes. - `htmtl.parsers.OuterPartial` - Parser the `outer-partial` attributes. (**soon**) -- `htmtl.parsers.OuterHtml` - Parser the `outer-html` attributes. (**soon**) +- `htmtl.parsers.OuterHtml` - Parser the `outer-html` attributes. - `htmtl.parsers.OuterText` - Parser the `outer-text` attributes. - `htmtl.parsers.Foreach` - Parses the `foreach` attributes. (**soon**) diff --git a/htmtl/htmtl.py b/htmtl/htmtl.py index 8c9a7a7..e5fa4f9 100644 --- a/htmtl/htmtl.py +++ b/htmtl/htmtl.py @@ -1,7 +1,10 @@ from typing import Any from dompa import Dompa from .parser import Parser +from .parsers.generic_value import GenericValue +from .parsers.inner_html import InnerHtml from .parsers.inner_text import InnerText +from .parsers.outer_html import OuterHtml from .parsers.outer_text import OuterText from .modifier import Modifier from .modifiers.truncate import Truncate @@ -21,7 +24,10 @@ class Htmtl: # set default attribute parsers self.__attribute_parsers = [ InnerText, + InnerHtml, OuterText, + OuterHtml, + GenericValue, ] # set default expression modifiers diff --git a/htmtl/parsers/generic_value.py b/htmtl/parsers/generic_value.py new file mode 100644 index 0000000..87b0d63 --- /dev/null +++ b/htmtl/parsers/generic_value.py @@ -0,0 +1,18 @@ +from typing import Optional +from dompa.nodes import Node +from ..parser import Parser + + +class GenericValue(Parser): + def traverse(self, node: Node) -> Optional[Node]: + new_attrs = {} + + for key, val in node.attributes.items(): + if key.startswith(":"): + new_attrs[key[1:]] = self.expression(val) + else: + new_attrs[key] = val + + node.attributes = new_attrs + + return node \ No newline at end of file diff --git a/htmtl/parsers/inner_html.py b/htmtl/parsers/inner_html.py new file mode 100644 index 0000000..60e90ca --- /dev/null +++ b/htmtl/parsers/inner_html.py @@ -0,0 +1,16 @@ +from typing import Optional + +from dompa import Dompa +from dompa.nodes import Node + +from ..parser import Parser + + +class InnerHtml(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "inner-html" in node.attributes: + child_nodes = Dompa(self.expression(node.attributes["inner-html"])).nodes() + node.children = child_nodes + node.attributes.pop("inner-html") + + return node \ No newline at end of file diff --git a/htmtl/parsers/outer_html.py b/htmtl/parsers/outer_html.py new file mode 100644 index 0000000..72df638 --- /dev/null +++ b/htmtl/parsers/outer_html.py @@ -0,0 +1,14 @@ +from typing import Optional + +from dompa import Dompa +from dompa.nodes import Node, FragmentNode + +from ..parser import Parser + + +class OuterHtml(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "outer-html" in node.attributes: + return FragmentNode(children=Dompa(self.expression(node.attributes["outer-html"])).nodes()) + + return node \ No newline at end of file -- cgit v1.2.3