Skip to content

Add for/endfor iteration and scoped variable resolution to fill_tokens - #33

Open
ncoop57 wants to merge 4 commits into
mainfrom
iter-scope
Open

Add for/endfor iteration and scoped variable resolution to fill_tokens#33
ncoop57 wants to merge 4 commits into
mainfrom
iter-scope

Conversation

@ncoop57

@ncoop57 ncoop57 commented Aug 5, 2026

Copy link
Copy Markdown
Contributor

What

Extends the template engine in mdhtml.md with two capabilities:

  1. Iteration{% for x in y %}…{% endfor %} (jinja) and list-valued {{#name}}…{{/name}} (mustache) now repeat their span once per item, binding each item to a frame pushed onto a scope stack.
  2. Scoped name resolution — names are now dotted paths resolved innermost-frame-first through the section stack, so variables inside an iterated section see the current item's fields while still falling back to outer (root) frames. Mustache pushes the item's value itself; jinja if pushes nothing (names stay lexical), for pushes {bind: item}.

Why

These are the core missing pieces for real-world templates that repeat over lists (tables, grant tables, contingency lists) while keeping the byte-identical-preservation guarantee of fill_tokens.

Changes

  • mdhtml/md.py: rewrites the fill loop to build a node tree, then render via build/walk; adds _resolve for dotted-path scope lookup and standalone for whole-line marker removal.
  • mdhtml/jinja.py: _classify now returns a 4-tuple with bind for for statements; docstring updated.
  • mdhtml/mustache.py: _classify passes '.' as the bind so list sections iterate; docstring updated.
  • tests/test_export.py: replaces the for-unsupported assertion with a while one; adds test_fill_iteration_and_scopes and test_jinja_for covering list iteration, dotted paths, innermost-frame precedence, staged fills, and table contexts.
  • updated examples to showcase iteration

@ncoop57 ncoop57 added the enhancement New feature or request label Aug 5, 2026
@ncoop57
ncoop57 marked this pull request as draft August 5, 2026 15:31
@ncoop57
ncoop57 marked this pull request as ready for review August 5, 2026 19:12
@ncoop57
ncoop57 requested a review from jph00 August 5, 2026 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant