summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAsko Nõmm <asko@nmm.ee>2025-01-02 01:30:23 +0200
committerAsko Nõmm <asko@nmm.ee>2025-01-02 01:30:23 +0200
commit689fe2613765de71101214a482c928cfdb1b2be6 (patch)
tree53ff6f9a092caee32e5c600bccf5ee546bd8bc20
parent5e08a8b2f9258ae1f0aca7e3ad044b6360e07458 (diff)
Clean up some naming usages and things
-rw-r--r--README.md64
-rw-r--r--htmtl/__init__.py4
-rw-r--r--htmtl/expression_parser.py6
-rw-r--r--htmtl/htmtl.py42
-rw-r--r--htmtl/modifier.py (renamed from htmtl/expression_modifier.py)6
-rw-r--r--htmtl/modifiers/__init__.py (renamed from htmtl/expression_modifiers/__init__.py)0
-rw-r--r--htmtl/modifiers/truncate.py (renamed from htmtl/expression_modifiers/truncate.py)6
-rw-r--r--htmtl/parser.py (renamed from htmtl/attribute_parser.py)5
-rw-r--r--htmtl/parsers/__init__.py (renamed from htmtl/attribute_parsers/__init__.py)0
-rw-r--r--htmtl/parsers/inner_text.py (renamed from htmtl/attribute_parsers/inner_text.py)4
-rw-r--r--htmtl/parsers/outer_text.py (renamed from htmtl/attribute_parsers/outer_text.py)4
11 files changed, 68 insertions, 73 deletions
diff --git a/README.md b/README.md
index 99097e7..216758e 100644
--- a/README.md
+++ b/README.md
@@ -299,31 +299,31 @@ This also works on collections, so you can use `truncate` to limit items in an a
## Extending
-### Attribute Parsers
+### Parsers
-You can add (or replace) attribute parsers in HTMTL when creating a new instance of the `Htmtl` class, like so:
+You can add (or replace) parsers in HTMTL when creating a new instance of the `Htmtl` class, like so:
```python
from htmtl import Htmtl
-from htmtl.attribute_parsers import InnerText
+from htmtl.parsers import InnerText
htmtl = Htmtl('<p inner-text="Hello {who}"></p>', {'who': 'World'})
-htmtl.set_attribute_parsers([
+htmtl.set_parsers([
InnerText,
])
html = htmtl.html() # returns: <p>Hello World</p>
```
-Attribute parsers must extend the `AttributeParser` class, like so:
+Prsers must extend the `Parser` class, like so:
```python
from typing import Optional
from dompa.nodes import Node, TextNode
-from htmtl import AttributeParser
+from htmtl import Parser
-class InnerText(AttributeParser):
+class InnerText(Parser):
def traverse(self, node: Node) -> Optional[Node]:
if "inner-text" in node.attributes:
node.children = [TextNode(value=self.expression(node.attributes["inner-text"]))]
@@ -332,49 +332,49 @@ class InnerText(AttributeParser):
return node
```
-All attribute parsers traverse the entire DOM tree to do whatever DOM manipulation they want. It's important to know that
-an attribute parser must have the `traverse` method, and it must return a `Node`, or `None` if you want to remove the `Node`.
+All parsers traverse the entire DOM tree to do whatever DOM manipulation they want. It's important to know that
+a parser must have the `traverse` method, and it must return a `Node`, or `None` if you want to remove the `Node`.
HTMTL is built upon the [Dompa](https://github.com/askonomm/dompa) HTML parser, so check that out for more granular info on things.
-#### List of built-in attribute parsers
+#### List of built-in parsers
-- `htmtl.attribute_parsers.GenericValue` - Parser the `:*` attributes. (**soon**)
-- `htmtl.attribute_parsers.If` - Parser the `if` attributes. (**soon**)
-- `htmtl.attribute_parsers.Unless` - Parser the `unless` attributes. (**soon**)
-- `htmtl.attribute_parsers.InnerPartial` - Parser the `inner-partial` attributes. (**soon**)
-- `htmtl.attribute_parsers.InnerHtml` - Parser the `inner-html` attributes. (**soon**)
-- `htmtl.attribute_parsers.InnerText` - Parser the `inner-text` attributes.
-- `htmtl.attribute_parsers.OuterPartial` - Parser the `outer-partial` attributes. (**soon**)
-- `htmtl.attribute_parsers.OuterHtml` - Parser the `outer-html` attributes. (**soon**)
-- `htmtl.attribute_parsers.OuterText` - Parser the `outer-text` attributes.
-- `htmtl.attribute_parsers.Foreach` - Parses the `foreach` attributes. (**soon**)
+- `htmtl.parsers.GenericValue` - Parser the `:*` attributes. (**soon**)
+- `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.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.OuterText` - Parser the `outer-text` attributes.
+- `htmtl.parsers.Foreach` - Parses the `foreach` attributes. (**soon**)
-### Expression Modifiers
+### Modifiers
-You can add (or replace) expression modifiers in HTMTL when creating a new instance of the `Htmtl` class, like so:
+You can add (or replace) modifiers in HTMTL when creating a new instance of the `Htmtl` class, like so:
```python
from htmtl import Htmtl
-from htmtl.expression_modifiers import Truncate
+from htmtl.modifiers import Truncate
htmtl = Htmtl('<p inner-text="Hello {who}"></p>', {'who': 'World'})
-htmtl.set_expression_modifiers([
+htmtl.set_modifiers([
Truncate,
])
html = htmtl.html() # returns: <p>Hello World</p>
```
-Expression modifiers must implement the `ExpressionModifier` class, like so:
+Mdifiers must extend the `Modifier` class, like so:
```python
from typing import Any
-from htmtl import ExpressionModifier, modifier
+from htmtl import Modifier, modifier_name
-@modifier("truncate")
-class Truncate(ExpressionModifier):
+@modifier_name("truncate")
+class Truncate(Modifier):
def modify(self, value: Any, opts: list[Any]) -> Any:
if isinstance(value, str) and len(opts) > 0:
if all([x in "1234567890" for x in opts[0]]):
@@ -386,9 +386,9 @@ class Truncate(ExpressionModifier):
return value
```
-All expression modifiers need to have a name (that you will use in your templates), and you can name your modifier
-with the `modifier` decorator.
+All modifiers need to have a name (that you will use in your templates), and you can name your modifier
+with the `modifier_name` decorator.
-#### List of built-in expression modifiers
+#### List of built-in modifiers
-- `htmtl.expression_modifiers.Truncate` - Truncates the value (both strings and collections).
+- `htmtl.modifiers.Truncate` - Truncates the value (both strings and collections).
diff --git a/htmtl/__init__.py b/htmtl/__init__.py
index 4286154..f5708d1 100644
--- a/htmtl/__init__.py
+++ b/htmtl/__init__.py
@@ -1,3 +1,3 @@
from .htmtl import Htmtl
-from .attribute_parser import AttributeParser
-from .expression_modifier import ExpressionModifier, modifier \ No newline at end of file
+from .parser import Parser
+from .modifier import Modifier, modifier_name \ No newline at end of file
diff --git a/htmtl/expression_parser.py b/htmtl/expression_parser.py
index 6d955d9..a99791b 100644
--- a/htmtl/expression_parser.py
+++ b/htmtl/expression_parser.py
@@ -1,12 +1,12 @@
from typing import Any
-from .expression_modifier import ExpressionModifier
+from .modifier import Modifier
class ExpressionParser:
__data: dict[str, Any]
- __expression_modifiers: list[type[ExpressionModifier]]
+ __expression_modifiers: list[type[Modifier]]
- def __init__(self, data: dict[str, Any], expression_modifiers: list[type[ExpressionModifier]]) -> None:
+ def __init__(self, data: dict[str, Any], expression_modifiers: list[type[Modifier]]) -> None:
self.__data = data
self.__expression_modifiers = expression_modifiers
diff --git a/htmtl/htmtl.py b/htmtl/htmtl.py
index a865eaf..8c9a7a7 100644
--- a/htmtl/htmtl.py
+++ b/htmtl/htmtl.py
@@ -1,51 +1,47 @@
from typing import Any
from dompa import Dompa
-from .attribute_parser import AttributeParser
-from .attribute_parsers.inner_text import InnerText
-from .attribute_parsers.outer_text import OuterText
-from .expression_modifier import ExpressionModifier
-from .expression_modifiers.truncate import Truncate
+from .parser import Parser
+from .parsers.inner_text import InnerText
+from .parsers.outer_text import OuterText
+from .modifier import Modifier
+from .modifiers.truncate import Truncate
from .expression_parser import ExpressionParser
class Htmtl:
__dom: Dompa
__data: dict[str, Any]
- __attribute_parsers: list[type[AttributeParser]]
- __expression_modifiers: list[type[ExpressionModifier]]
+ __parsers: list[type[Parser]]
+ __modifiers: list[type[Modifier]]
def __init__(self, template: str, data: dict[str, Any] = None):
self.__dom = Dompa(template)
self.__data = data or {}
- self.__attribute_parsers = self.__default_attribute_parsers()
- self.__expression_modifiers = self.__default_expression_modifiers()
- @staticmethod
- def __default_attribute_parsers() -> list[type[AttributeParser]]:
- return [
+ # set default attribute parsers
+ self.__attribute_parsers = [
InnerText,
OuterText,
]
- @staticmethod
- def __default_expression_modifiers() -> list[type[ExpressionModifier]]:
- return [
+ # set default expression modifiers
+ self.__expression_modifiers = [
Truncate,
]
- def set_attribute_parsers(self, parsers: list[type[AttributeParser]]):
+ def set_parsers(self, parsers: list[type[Parser]]):
for parser in parsers:
- if not isinstance(parser, AttributeParser):
- raise TypeError("Attribute parser must extend the AttributeParser class.")
+ if not isinstance(parser, Parser):
+ raise TypeError("Parser must extend the Parser class.")
- self.__attribute_parsers = parsers
+ self.__parsers = parsers
- def set_expression_modifiers(self, modifiers: list[type[ExpressionModifier]]):
+ def set_modifiers(self, modifiers: list[type[Modifier]]):
for modifier in modifiers:
- if not isinstance(modifier, ExpressionModifier):
- raise NotImplementedError("Modifier must extend the ExpressionModifier class.")
+ if not isinstance(modifier, Modifier):
+ raise NotImplementedError("Modifier must extend the Modifier class.")
- self.__expression_modifiers = modifiers
+ self.__modifiers = modifiers
def __parse(self) -> None:
expression_parser = ExpressionParser(self.__data, self.__expression_modifiers)
diff --git a/htmtl/expression_modifier.py b/htmtl/modifier.py
index 69d6a86..a771cca 100644
--- a/htmtl/expression_modifier.py
+++ b/htmtl/modifier.py
@@ -2,7 +2,7 @@ from abc import abstractmethod, ABC
from typing import Any
-class ExpressionModifier(ABC):
+class Modifier(ABC):
name: str
@abstractmethod
@@ -10,8 +10,8 @@ class ExpressionModifier(ABC):
pass
-def modifier(name: str):
- def wrapper(cls: type[ExpressionModifier]):
+def modifier_name(name: str):
+ def wrapper(cls: type[Modifier]):
cls.name = name
return cls
diff --git a/htmtl/expression_modifiers/__init__.py b/htmtl/modifiers/__init__.py
index f200bbd..f200bbd 100644
--- a/htmtl/expression_modifiers/__init__.py
+++ b/htmtl/modifiers/__init__.py
diff --git a/htmtl/expression_modifiers/truncate.py b/htmtl/modifiers/truncate.py
index 41a1bdb..bac2862 100644
--- a/htmtl/expression_modifiers/truncate.py
+++ b/htmtl/modifiers/truncate.py
@@ -1,10 +1,10 @@
from typing import Any
-from ..expression_modifier import ExpressionModifier, modifier
+from ..modifier import Modifier, modifier_name
-@modifier("truncate")
-class Truncate(ExpressionModifier):
+@modifier_name("truncate")
+class Truncate(Modifier):
def modify(self, value: Any, opts: list[Any]) -> Any:
if isinstance(value, str) and len(opts) > 0:
if all([x in "1234567890" for x in opts[0]]):
diff --git a/htmtl/attribute_parser.py b/htmtl/parser.py
index 7511c8e..a91de0a 100644
--- a/htmtl/attribute_parser.py
+++ b/htmtl/parser.py
@@ -1,11 +1,10 @@
from abc import ABC, abstractmethod
from typing import Any, Optional
from dompa.nodes import Node
+from .expression_parser import ExpressionParser
-from htmtl.expression_parser import ExpressionParser
-
-class AttributeParser(ABC):
+class Parser(ABC):
__data: dict[str, Any]
__expression_parser: ExpressionParser
diff --git a/htmtl/attribute_parsers/__init__.py b/htmtl/parsers/__init__.py
index 4ad8185..4ad8185 100644
--- a/htmtl/attribute_parsers/__init__.py
+++ b/htmtl/parsers/__init__.py
diff --git a/htmtl/attribute_parsers/inner_text.py b/htmtl/parsers/inner_text.py
index 75ad01e..52fc0b4 100644
--- a/htmtl/attribute_parsers/inner_text.py
+++ b/htmtl/parsers/inner_text.py
@@ -1,10 +1,10 @@
from typing import Optional
from dompa.nodes import Node, TextNode
-from ..attribute_parser import AttributeParser
+from ..parser import Parser
-class InnerText(AttributeParser):
+class InnerText(Parser):
def traverse(self, node: Node) -> Optional[Node]:
if "inner-text" in node.attributes:
node.children = [TextNode(value=self.expression(node.attributes["inner-text"]))]
diff --git a/htmtl/attribute_parsers/outer_text.py b/htmtl/parsers/outer_text.py
index 8e9bead..78b0fce 100644
--- a/htmtl/attribute_parsers/outer_text.py
+++ b/htmtl/parsers/outer_text.py
@@ -1,10 +1,10 @@
from typing import Optional
from dompa.nodes import Node, TextNode
-from ..attribute_parser import AttributeParser
+from ..parser import Parser
-class OuterText(AttributeParser):
+class OuterText(Parser):
def traverse(self, node: Node) -> Optional[Node]:
if "outer-text" in node.attributes:
return TextNode(value=self.expression(node.attributes["outer-text"]))