diff options
| -rw-r--r-- | hello.py | 2 | ||||
| -rw-r--r-- | htmtl/attribute_parsers/inner_text.py | 4 | ||||
| -rw-r--r-- | htmtl/attribute_parsers/outer_text.py | 8 | ||||
| -rw-r--r-- | htmtl/expression_modifier.py | 5 | ||||
| -rw-r--r-- | htmtl/expression_parser.py | 70 |
5 files changed, 74 insertions, 15 deletions
@@ -2,7 +2,7 @@ from htmtl import Htmtl def main(): - print(Htmtl("<div inner-text=\"hello\">asdasd</div>").html()) + print(Htmtl("<div inner-text=\"hello {something}\">asdasd</div>", {'something': 'nothing'}).html()) if __name__ == "__main__": diff --git a/htmtl/attribute_parsers/inner_text.py b/htmtl/attribute_parsers/inner_text.py index bd98010..9195802 100644 --- a/htmtl/attribute_parsers/inner_text.py +++ b/htmtl/attribute_parsers/inner_text.py @@ -1,9 +1,9 @@ from dompa.nodes import Node, TextNode -from htmtl.attribute_parser import AttributeParser +from ..attribute_parser import AttributeParser class InnerText(AttributeParser): def traverse(self, node: Node): if "inner-text" in node.attributes: - node.children = [TextNode(self.expression(node.attributes["inner-text"]))] + node.children = [TextNode(value=self.expression(node.attributes["inner-text"]))] node.attributes.pop("inner-text")
\ No newline at end of file diff --git a/htmtl/attribute_parsers/outer_text.py b/htmtl/attribute_parsers/outer_text.py new file mode 100644 index 0000000..8d89b14 --- /dev/null +++ b/htmtl/attribute_parsers/outer_text.py @@ -0,0 +1,8 @@ +from dompa.nodes import Node, TextNode +from ..attribute_parser import AttributeParser + + +class OuterText(AttributeParser): + def traverse(self, node: Node): + if "outer-text" in node.attributes: + node.replace_with(TextNode(value=self.expression(node.attributes["outer-text"]))) diff --git a/htmtl/expression_modifier.py b/htmtl/expression_modifier.py index abb1619..1986d92 100644 --- a/htmtl/expression_modifier.py +++ b/htmtl/expression_modifier.py @@ -1,7 +1,10 @@ from abc import abstractmethod, ABC +from typing import Any class ExpressionModifier(ABC): + name: str + @abstractmethod - def modify(self, expression): + def modify(self, value: Any, opts: list[Any]) -> Any: pass
\ No newline at end of file diff --git a/htmtl/expression_parser.py b/htmtl/expression_parser.py index 6db16b1..9a91ebb 100644 --- a/htmtl/expression_parser.py +++ b/htmtl/expression_parser.py @@ -1,6 +1,6 @@ -from typing import Dict, Any, Optional +from typing import Dict, Any -from htmtl.expression_modifier import ExpressionModifier +from .expression_modifier import ExpressionModifier class ExpressionParser: @@ -12,16 +12,64 @@ class ExpressionParser: self.__expression_modifiers = expression_modifiers def parse(self, expression: str) -> Any: - pass + # no curly brackets means that the whole thing is an interpolation + if expression.count("{") != expression.count("}"): + parsed_interpolation = self.__parse_interpolation(expression) - def parse_interpolation(self, interpolation: str) -> Any: - pass + return parsed_interpolation - def use_modifier(self, value: Any, modifier_name: str, modifier_opts: list[Any]) -> Any: - pass + # otherwise only parts of it are + parsed_expression = "" + interpolation_start = None + interpolation_end = None - def find_modifier(self, name: str) -> Optional[ExpressionModifier]: - pass + for idx, char in enumerate(expression): + parsed_expression += char - def var_to_val(self, var: str) -> Any: - pass
\ No newline at end of file + if char == "{": + interpolation_start = idx + + if char == "}": + interpolation_end = idx + 1 + + if interpolation_start is not None and interpolation_end is not None: + interpolation = expression[interpolation_start:interpolation_end] + parsed_expression = parsed_expression.replace(interpolation, self.__parse_interpolation(interpolation[1:-1])) + interpolation_start = None + interpolation_end = None + + return parsed_expression + + def __parse_interpolation(self, interpolation: str) -> Any: + parts = interpolation.split("|") + value = self.__var_to_val(parts[0].strip()) + modifiers = [x.strip() for x in parts[1:]] if len(parts) > 1 else [] + + for modifier in modifiers: + modifier_parts = modifier.split(":") + modifier_name = modifier_parts[0].strip() + modifier_opts = [x.strip() for x in modifier_parts[1:]] if len(modifier_parts) > 1 else [] + value = self.__use_modifier(value, modifier_name, modifier_opts) + + return value + + def __use_modifier(self, value: Any, modifier_name: str, modifier_opts: list[Any]) -> Any: + for modifier in self.__expression_modifiers: + modifier_instance = modifier() + + if modifier_instance.name == modifier_name: + return modifier_instance.modify(value, modifier_opts) + + return None + + def __var_to_val(self, var: str) -> Any: + parts = var.split(".") + value = self.__data + + for part in parts: + if part in value: + value = value[part] + else: + return None + + return value |
