From 51904366dd5c46c8d8d37e333b538b7574360ff5 Mon Sep 17 00:00:00 2001 From: Asko Nõmm Date: Sat, 4 Jan 2025 22:24:42 +0200 Subject: Implement `When` and `WhenNot` parsers. --- README.md | 16 ++++++++-------- htmtl/htmtl.py | 4 ++++ htmtl/parsers/when.py | 14 ++++++++++++++ htmtl/parsers/when_not.py | 14 ++++++++++++++ 4 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 htmtl/parsers/when.py create mode 100644 htmtl/parsers/when_not.py diff --git a/README.md b/README.md index 0268c7c..31bc1a0 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ you to use any editor without needing any additional editor extensions. - **Conditionals**: You can show or hide blocks using expressions. - **Partials**: You can include other templates inside your templates. - **Loops**: You can loop over iterable data. -- **Extendable**: You can implement custom attribute parsers and expression modifiers. +- **Extendable**: You can implement custom parsers and modifiers. ## Example syntax @@ -24,7 +24,7 @@ you to use any editor without needing any additional editor extensions.

-
+

@@ -182,14 +182,14 @@ Results in:

``` -### `if` +### `when` Removes the element if the attribute is false-y. HTMTL template where `show` key is `False`: ```html -
Hello, World!
+
Hello, World!
``` Results in: @@ -198,14 +198,14 @@ Results in: ``` -### `unless` +### `when-not` Removes the element if the attribute is truthy. HTMTL template where `hide` key is `True`: ```html -
Hello, World!
+
Hello, World!
``` Results in: @@ -346,8 +346,8 @@ HTMTL is built upon the [Dompa](https://github.com/askonomm/dompa) HTML parser, #### List of built-in parsers - `htmtl.parsers.GenericValue` - Parser the `:*` attributes. -- `htmtl.parsers.If` - Parser the `if` attributes. (**soon**) -- `htmtl.parsers.Unless` - Parser the `unless` attributes. (**soon**) +- `htmtl.parsers.When` - Parser the `when` attributes. +- `htmtl.parsers.WhenNot` - Parser the `when-not` attributes. - `htmtl.parsers.InnerPartial` - Parser the `inner-partial` attributes. - `htmtl.parsers.InnerHtml` - Parser the `inner-html` attributes. - `htmtl.parsers.InnerText` - Parser the `inner-text` attributes. diff --git a/htmtl/htmtl.py b/htmtl/htmtl.py index 885fe94..7732f06 100644 --- a/htmtl/htmtl.py +++ b/htmtl/htmtl.py @@ -11,6 +11,8 @@ from .parsers.outer_text import OuterText from .modifier import Modifier from .modifiers.truncate import Truncate from .expression_parser import ExpressionParser +from .parsers.when import When +from .parsers.when_not import WhenNot class Htmtl: @@ -32,6 +34,8 @@ class Htmtl: OuterHtml, OuterPartial, GenericValue, + When, + WhenNot, ] # set default expression modifiers diff --git a/htmtl/parsers/when.py b/htmtl/parsers/when.py new file mode 100644 index 0000000..57a965a --- /dev/null +++ b/htmtl/parsers/when.py @@ -0,0 +1,14 @@ +from typing import Optional +from dompa.nodes import Node +from ..parser import Parser + + +class When(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "when" in node.attributes: + if not self.expression(node.attributes["when"]): + return None + + node.attributes.pop("when") + + return node \ No newline at end of file diff --git a/htmtl/parsers/when_not.py b/htmtl/parsers/when_not.py new file mode 100644 index 0000000..68e3d09 --- /dev/null +++ b/htmtl/parsers/when_not.py @@ -0,0 +1,14 @@ +from typing import Optional +from dompa.nodes import Node +from ..parser import Parser + + +class WhenNot(Parser): + def traverse(self, node: Node) -> Optional[Node]: + if "when-not" in node.attributes: + if self.expression(node.attributes["when-not"]): + return None + + node.attributes.pop("when-not") + + return node \ No newline at end of file -- cgit v1.2.3