htmy 0.3.5__py3-none-any.whl → 0.3.6__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- htmy/core.py +17 -3
- htmy/md/core.py +6 -1
- htmy/typing.py +6 -0
- {htmy-0.3.5.dist-info → htmy-0.3.6.dist-info}/METADATA +1 -1
- {htmy-0.3.5.dist-info → htmy-0.3.6.dist-info}/RECORD +7 -7
- {htmy-0.3.5.dist-info → htmy-0.3.6.dist-info}/LICENSE +0 -0
- {htmy-0.3.5.dist-info → htmy-0.3.6.dist-info}/WHEEL +0 -0
htmy/core.py
CHANGED
|
@@ -3,7 +3,7 @@ from __future__ import annotations
|
|
|
3
3
|
import abc
|
|
4
4
|
import asyncio
|
|
5
5
|
import enum
|
|
6
|
-
from collections.abc import Callable, Container
|
|
6
|
+
from collections.abc import Awaitable, Callable, Container
|
|
7
7
|
from pathlib import Path
|
|
8
8
|
from typing import TYPE_CHECKING, Any, ClassVar, Generic, TypedDict, cast, overload
|
|
9
9
|
from xml.sax.saxutils import escape as xml_escape
|
|
@@ -21,6 +21,7 @@ from .typing import (
|
|
|
21
21
|
PropertyValue,
|
|
22
22
|
SyncFunctionComponent,
|
|
23
23
|
T,
|
|
24
|
+
TextProcessor,
|
|
24
25
|
is_component_sequence,
|
|
25
26
|
)
|
|
26
27
|
from .utils import join_components
|
|
@@ -130,21 +131,34 @@ class Snippet:
|
|
|
130
131
|
Base component that can load its content from a file.
|
|
131
132
|
"""
|
|
132
133
|
|
|
133
|
-
__slots__ = ("_path_or_text",)
|
|
134
|
+
__slots__ = ("_path_or_text", "_text_processor")
|
|
134
135
|
|
|
135
|
-
def __init__(
|
|
136
|
+
def __init__(
|
|
137
|
+
self,
|
|
138
|
+
path_or_text: Text | str | Path,
|
|
139
|
+
*,
|
|
140
|
+
text_processor: TextProcessor | None = None,
|
|
141
|
+
) -> None:
|
|
136
142
|
"""
|
|
137
143
|
Initialization.
|
|
138
144
|
|
|
139
145
|
Arguments:
|
|
140
146
|
path_or_text: The path from where the content should be loaded or a `Text`
|
|
141
147
|
instance if this value should be rendered directly.
|
|
148
|
+
text_processor: An optional text processors that can be used to process the text
|
|
149
|
+
content before rendering. It can be used for example for token replacement or
|
|
150
|
+
string formatting.
|
|
142
151
|
"""
|
|
143
152
|
self._path_or_text = path_or_text
|
|
153
|
+
self._text_processor = text_processor
|
|
144
154
|
|
|
145
155
|
async def htmy(self, context: Context) -> Component:
|
|
146
156
|
"""Renders the component."""
|
|
147
157
|
text = await self._get_text_content()
|
|
158
|
+
if self._text_processor is not None:
|
|
159
|
+
processed = self._text_processor(text, context)
|
|
160
|
+
text = (await processed) if isinstance(processed, Awaitable) else processed
|
|
161
|
+
|
|
148
162
|
return self._render_text(text, context)
|
|
149
163
|
|
|
150
164
|
async def _get_text_content(self) -> str:
|
htmy/md/core.py
CHANGED
|
@@ -5,6 +5,7 @@ from typing import TYPE_CHECKING, ClassVar
|
|
|
5
5
|
from markdown import Markdown
|
|
6
6
|
|
|
7
7
|
from htmy.core import ContextAware, SafeStr, Snippet, Text
|
|
8
|
+
from htmy.typing import TextProcessor
|
|
8
9
|
|
|
9
10
|
if TYPE_CHECKING:
|
|
10
11
|
from collections.abc import Callable
|
|
@@ -90,6 +91,7 @@ class MD(Snippet):
|
|
|
90
91
|
*,
|
|
91
92
|
converter: Callable[[str], Component] | None = None,
|
|
92
93
|
renderer: MarkdownRenderFunction | None = None,
|
|
94
|
+
text_processor: TextProcessor | None = None,
|
|
93
95
|
) -> None:
|
|
94
96
|
"""
|
|
95
97
|
Initialization.
|
|
@@ -100,8 +102,11 @@ class MD(Snippet):
|
|
|
100
102
|
into an HTMY component.
|
|
101
103
|
renderer: Function that get the parsed and converted content and the metadata (if it exists)
|
|
102
104
|
and turns them into an HTMY component.
|
|
105
|
+
text_processor: An optional text processors that can be used to process the text
|
|
106
|
+
content before rendering. It can be used for example for token replacement or
|
|
107
|
+
string formatting.
|
|
103
108
|
"""
|
|
104
|
-
super().__init__(path_or_text)
|
|
109
|
+
super().__init__(path_or_text, text_processor=text_processor)
|
|
105
110
|
self._converter: Callable[[str], Component] = SafeStr if converter is None else converter
|
|
106
111
|
self._renderer = renderer
|
|
107
112
|
|
htmy/typing.py
CHANGED
|
@@ -103,3 +103,9 @@ class AsyncContextProvider(Protocol):
|
|
|
103
103
|
|
|
104
104
|
ContextProvider: TypeAlias = SyncContextProvider | AsyncContextProvider
|
|
105
105
|
"""Context provider type."""
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
# -- Text processors
|
|
109
|
+
|
|
110
|
+
TextProcessor: TypeAlias = Callable[[str, Context], str | Coroutine[Any, Any, str]]
|
|
111
|
+
"""Callable type that expects a string and a context, and returns a processed string."""
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
htmy/__init__.py,sha256=My_Dmh9JNQpQekTO6CEbdbV7Ux7MD9Pz-L2M3KMZjGQ,1835
|
|
2
|
-
htmy/core.py,sha256=
|
|
2
|
+
htmy/core.py,sha256=kdIE9s7mE-mphPraZvl5h7RL9NBVOYaRWsWsA0PpphM,19332
|
|
3
3
|
htmy/etree.py,sha256=zZkKY82t5fX85unS9oHuG6KEBsJY_iz6E7SJto8lSVQ,3097
|
|
4
4
|
htmy/html.py,sha256=pxmE-KU5OgwNp6MyxOfdS0Ohpzu2RNYCeGGFlHLDGUM,20940
|
|
5
5
|
htmy/i18n.py,sha256=brNazQjObBFfbnViZCpcnxa0qgxQbJfX7xJAH-MqTW8,5124
|
|
6
6
|
htmy/io.py,sha256=iebJOZp7L0kZ9SWdqMatKtW5VGRIkEd-eD0_vTAldH8,41
|
|
7
7
|
htmy/md/__init__.py,sha256=lxBJnYplkDuxYuiese6My9KYp1DeGdzo70iUdYTvMnE,334
|
|
8
|
-
htmy/md/core.py,sha256
|
|
8
|
+
htmy/md/core.py,sha256=5fLEihxjBivknZUwiR13gc924K2_jnthv9xnSDsTfow,3686
|
|
9
9
|
htmy/md/typing.py,sha256=LF-AEvo7FCW2KumyR5l55rsXizV2E4AHVLKFf6lApgM,762
|
|
10
10
|
htmy/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
11
11
|
htmy/renderer.py,sha256=vCYqq83RaEtlXKDGWB6LgKjoE1TSfMfB5TW1lJWHE5c,3829
|
|
12
|
-
htmy/typing.py,sha256
|
|
12
|
+
htmy/typing.py,sha256=-_N9xE9bh0OvP2LRedh3OeuOGlJcipxm0Aul73IKN7E,3134
|
|
13
13
|
htmy/utils.py,sha256=7_CyA39l2m6jzDqparPKkKgRB2wiGuBZXbiPgiZOXKA,1093
|
|
14
|
-
htmy-0.3.
|
|
15
|
-
htmy-0.3.
|
|
16
|
-
htmy-0.3.
|
|
17
|
-
htmy-0.3.
|
|
14
|
+
htmy-0.3.6.dist-info/LICENSE,sha256=rFtoGU_3c_rlacXgOZapTHfMErN-JFPT5Bq_col4bqI,1067
|
|
15
|
+
htmy-0.3.6.dist-info/METADATA,sha256=M0UqpgexbJ0963aacW3pVvd_puJu99S72yrmIrL4Pus,16347
|
|
16
|
+
htmy-0.3.6.dist-info/WHEEL,sha256=Nq82e9rUAnEjt98J6MlVmMCZb-t9cYE2Ir1kpBmnWfs,88
|
|
17
|
+
htmy-0.3.6.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|