diff --git a/python/mdhtml/fill.py b/python/mdhtml/fill.py index 665e833..3746571 100644 --- a/python/mdhtml/fill.py +++ b/python/mdhtml/fill.py @@ -113,6 +113,7 @@ def _tokens(norm, tmpls): toks = sorted(blks + inl, key=lambda t: t["start"]) groups = _groups(norm, tmpls, len(toks)) if toks else [] for i, t in enumerate(toks): + t.setdefault("source", norm[t["start"]:t["end"]]) t["extent"] = _extent(norm, t) t["line"] = norm.count("\n", 0, t["start"]) + 1 t["group"] = None if groups is None else groups[i] diff --git a/tests/test_fill.py b/tests/test_fill.py index 8cc98e9..7433566 100644 --- a/tests/test_fill.py +++ b/tests/test_fill.py @@ -1,6 +1,6 @@ import pytest -from mdhtml.fill import instantiate, fill_md +from mdhtml.fill import instantiate, fill_md, tokens from helpers import mk_dlg, run_cli @@ -56,6 +56,15 @@ def test_list_and_tree_rules(): with pytest.raises(ValueError): fill_md('Bad {{!note}}.', {}) +def test_block_token_source_and_warnings(): + assert tokens("{{/early}}\n")[0]["source"].strip() == "{{/early}}" + orphan = fill_md("Café\n\n{{/early}}\n", {}, strict=False) + assert orphan == "Café\n\n{{/early}}\n" and any("unpaired close marker '{{/early}}'" in w for w in orphan.warnings) + with pytest.raises(ValueError, match="unpaired close marker"): fill_md("{{/early}}\n", {}) + unknown = fill_md("{{!note}}\n", {}, strict=False) + assert unknown == "{{!note}}\n" and any("unknown template token '{{!note}}'" in w for w in unknown.warnings) + + def test_substitution_recursion(): assert fill_md("{{a}}\n", dict(a="See {{b}}.", b="9")) == "See 9.\n" # values are re-rendered with pytest.raises(ValueError, match="loop"): fill_md("{{loop}}\n", dict(loop="{{loop}}"))