Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions python/mdhtml/fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from fastcore.script import call_parse
from fastcore.xtras import strloader
from fastcore.nbio import nb_frontmatter, cell_frontmatter
from fastcore.nbio import nb_frontmatter, cell_frontmatter, deep_merge
from aihist.hist import dlg2md
from aidialog.ipynb import read_ipynb
from fast5ever import parse_fragment
Expand Down Expand Up @@ -314,7 +314,7 @@ def instantiate(
tmpls = _tmpl_args(templates)
meta, body = _frontmatter(src)
fd = meta.get("formdata")
merged = {**(fd if isinstance(fd, dict) else {}), **(data or {})}
merged = deep_merge(fd if isinstance(fd, dict) else {}, data or {})
norm, _ = _normalize_offsets(body.lstrip("\n"))
woven = _weave(norm, merged, tmpls)
res = fill_md(woven, merged, strict=strict, filled=filled, templates=templates)
Expand All @@ -333,7 +333,7 @@ def instantiate_nb(
"Instantiate a dialog: run its participating code cells (the `eval` cascade, opt-in by default), weave their outputs, fill tokens"
d = read_ipynb(fname)
fd = nb_frontmatter(d, strvals=True).get("formdata")
merged = {**(fd if isinstance(fd, dict) else {}), **(data or {})}
merged = deep_merge(fd if isinstance(fd, dict) else {}, data or {})
shell = _capture_shell()
shell.user_ns["__data__"] = merged
ran = d.execute(default_eval=False, shell=shell)
Expand Down
13 changes: 13 additions & 0 deletions tests/test_fill.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,16 @@ def test_instantiate_nb(tmp_path):
bad = Message('#| eval: true\n1/0')
with pytest.raises(ZeroDivisionError) as error: instantiate_nb(mk_dlg(tmp_path, [bad]))
assert any(bad.id in note for note in error.value.__notes__)


def test_nested_formdata_merge(tmp_path):
from aidialog.dialog import Message, snote, sraw
from mdhtml.fill import instantiate_nb
fm = ("---\nformdata:\n company:\n name: ''\n signatory:\n name: ''\n title: CEO\n"
" signature: '`{{signature, r1}}`'\n extra:\n note: keep\n drop: unused\n---\n")
body = "{{company.name}} / {{company.signatory.name}} ({{company.signatory.title}}) {{company.signature}}{{#extra}} {{note}}{{/extra}}\n"
data = dict(company=dict(name="Acme", signatory=dict(name="Rachel")), extra=False, drop=None)
assert instantiate(fm + "\n" + body, data) == "Acme / Rachel (CEO) `{{signature, r1}}`\n"
p = mk_dlg(tmp_path, [Message(fm.strip(), msg_type=sraw), Message(body, msg_type=snote)])
assert instantiate_nb(p, data).strip() == "Acme / Rachel (CEO) `{{signature, r1}}`"
assert data == dict(company=dict(name="Acme", signatory=dict(name="Rachel")), extra=False, drop=None)
Loading