Layer Changes / T-String All The Things #74
Replies: 7 comments 3 replies
-
|
I like the simplicity of the current key public API: I could definitely see adding a second I also agree we want a better cached intermediate representation in the processor. Holding on to |
Beta Was this translation helpful? Give feedback.
-
|
The placeholder idea is genius in a way but there is something about having them "floating around" in the Nodes that can be confusing. I had a hard time trying to figure out what is happening and what should be happening. Also there are some book-keeping issues about where things were in the original template that can only be determined while parsing (mainly the I was able to make some progress on trying to get one of my prototypes shoe-horned into the current codebase and a new pattern did emerge. If a new "kind" of nodes existed that were "Template Nodes",
So there would be So conceptually something like this: doc_t: Annotated[Template, "html"] = get_doc_t()
t_node: TNode = parse_html(doc_t)
node: Node = html(t_node, doc_t.interpolations) # could do both at once like now but just for clarity
html_string: Annotated[str, "html"] = str(node) |
Beta Was this translation helpful? Give feedback.
-
|
I've tried out a few things regarding the post parsing / pre processing format. This one kind of came out of the ashes and is very simple:
It might be "too" tricky/fragile but we just flatten each attribute type into a tuple that uses indexes to point to the original interpolation, this is just meant to be iterated, def _substitute_interpolated_attrs(
attrs_seq: tuple[tuple[int|str, ...], ...], interpolations: tuple[Interpolation, ...]
) -> dict[str, object|None]:
new_attrs: dict[str, object | None] = LastUpdatedOrderedDict()
for attr in attrs_seq: #tuple[tuple[int|str, ...], ...]
match attr: #tuple[int|str, ...]
case [int()]: # spread
interpolation = interpolations[attr[0]]
spread_value = format_interpolation(interpolation)
for sub_k, sub_v in _substitute_spread_attrs(spread_value):
new_attrs[sub_k] = sub_v
case [str()]: # bare attr
new_attrs[attr[0]] = True
case [str(), str()]: # static attr
new_attrs[attr[0]] = attr[1]
case [str(), int()]: # interpolated attr
new_value = format_interpolation(interpolations[attr[1]])
for sub_k, sub_v in _process_attr(attr[0], new_value):
new_attrs[sub_k] = sub_v
case [str(), str()|int(), _, *rest]: # templated attr
new_attrs[attr[0]] = "".join(
part
if isinstance(part, str)
else str(format_interpolation(interpolations[part]))
for part in attr[1:]
)
case _:
raise ValueError(f"Unknown attr format {attr}")
return new_attrsWe could make a more structured format that tries to keep all these things separate with a |
Beta Was this translation helpful? Give feedback.
-
|
Well I put a draft version of a structured format design in a PR to reference maybe: #76 |
Beta Was this translation helpful? Give feedback.
-
|
Just checking about this thread, has the discussion moved elsewhere (to PRs) and we should close this thread? |
Beta Was this translation helpful? Give feedback.
-
|
I confess, I've long had a deep, almost daily interest in a crazy idea: keeping intermediate representations around as JSON to cache in...SQLite. I'll spare you the explanation. In a nutshell: what's the least amount of work to remake a site from a change? |
Beta Was this translation helpful? Give feedback.
-
|
Sounds good about letting you wrap up your work and start a new discussion, that's the right idea. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
After experimenting with the parser a lot I have been thinking it might be better to fully embrace t-strings by re-packing the Template back into a more "aware" Template. I have a "not very well integrated" prototype so I thought I'd start talking about it here and then maybe I could slowly make small pull requests and keep the tests up to date without sending a bunch of PRs out of no where. This mainly builds on the placeholder idea to use an integer index to lookup the original interpolations in the "values"/source template.
From a high level without completely drowning in details the long version could look something like this (psuedo code):
The cached version would look like this but would use
values_template.stringsto lookup thestructure_templatefrom the cache:In the simplest possible scenario we'd do something like this (psuedo code):
Parser Changes
Remove placeholders and put in Interpolations.
Node Changes
Nodes become more like an easier to manage 1-to-1 mapping of the structure of the
values_templatebut in a hierarchy and with all the metadata established. We don't ever interpolate the actual values here we just pre-extract the placeholders and replace them with actual objects that still use the integer indices.Processor Changes
Instead of interpolating into the node actually coerce the node tree back into a structured template. Almost the reverse of the parser where static content is flattened back down but with the information we gleaned from parsing maintained in
Interpolations (or something similar).Renderer Changes
Instead of using
Node.__str__()to render the html we act directly on the structure template and descend through interpolations into nested/embedded templates.Examples
These are just psuedo code right now but I could flesh out the details. The EXACT implementation might need to be adjusted because I'm not sure we'd want interpolations in interpolations or not. Maybe using custom tuples / slotted dataclasses or something would be better.
Beta Was this translation helpful? Give feedback.
All reactions