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
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
Unreleased
----------

- Preserve tabs and line breaks between the characters of an escaped delimiter in Word templates.

0.21.0 (2026-11-15)
-------------------
First release as Docform. Rendering output matches docxtpl 0.20.2.
Expand Down
9 changes: 4 additions & 5 deletions docform/docx/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,10 @@ def patch_xml(self, src_xml):
strip all unnecessary xml tags, manage table cell background color and colspan,
unescape html entities, etc..."""

# Join escaped Jinja delimiters that Word has split across runs. Limit the
# removable XML to tags inside one paragraph so unrelated text is untouched.
split_run_gap = (
r"(?:</w:t>(?:(?!</?w:p(?:\s|>))<[^>]+>)*<w:t(?: [^>]*)?>)?"
)
# Word may insert proofing marks, bookmarks or run formatting between
# text nodes. Paragraph boundaries, tabs and line breaks end a delimiter.
boundary = r"</?w:(?:p|tab|br|cr)(?=[\s/>])"
split_run_gap = rf"(?:</w:t>(?:(?!{boundary})<[^>]+>)*<w:t(?: [^>]*)?>)?"
for escaped_delimiter in ("{_{", "}_}", "{_%", "%_}"):
pattern = split_run_gap.join(map(re.escape, escaped_delimiter))
src_xml = re.sub(pattern, escaped_delimiter, src_xml)
Expand Down
3 changes: 3 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ In order to display ``{%``, ``%}``, ``{{`` or ``}}``, one can use::

{_%, %_}, {_{ or }_}

The characters of an escaped delimiter can have different Word formatting.
A tab or line break between the characters interrupts the delimiter and is preserved.

Tables
......

Expand Down
39 changes: 38 additions & 1 deletion tests/unit/test_render.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import pytest
from docx import Document
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
from jinja2 import Environment
from jinja2.exceptions import TemplateSyntaxError, UndefinedError
from jinja2 import StrictUndefined
Expand All @@ -15,6 +18,41 @@ def test_variables(make_docx):
assert body_text(saved(tpl)) == ["Hello Ada", "Total: $12"]


@pytest.mark.parametrize(
("separator", "expected"), [("", "}}"), ("\t", "}\t_}"), ("\n", "}\n_}")]
)
def test_escaped_delimiter_across_runs(tmp_path, separator, expected):
doc = Document()
paragraph = doc.add_paragraph()
paragraph.add_run("left }")
paragraph.add_run(separator)
paragraph.add_run("_").bold = True
paragraph.add_run("} right")
path = tmp_path / "delimiter.docx"
doc.save(path)

tpl = DocxTemplate(path)
tpl.render({})
assert body_text(saved(tpl)) == ["left " + expected + " right"]


def test_escaped_delimiter_across_proofing_marks(tmp_path):
doc = Document()
paragraph = doc.add_paragraph()
paragraph.add_run("left }")
for mark, text in (("spellStart", "_"), ("spellEnd", "} right")):
marker = OxmlElement("w:proofErr")
marker.set(qn("w:type"), mark)
paragraph._p.append(marker)
paragraph.add_run(text)
path = tmp_path / "proofing.docx"
doc.save(path)

tpl = DocxTemplate(path)
tpl.render({})
assert body_text(saved(tpl)) == ["left }} right"]


def test_missing_variable_renders_empty(make_docx):
tpl = DocxTemplate(make_docx(["Hi {{ missing }}!"]))
tpl.render({})
Expand Down Expand Up @@ -142,7 +180,6 @@ def test_save_to_path(make_docx, tmp_path):
tpl.render({"a": "ok"})
out = tmp_path / "out.docx"
tpl.save(out)
from docx import Document

assert body_text(Document(str(out))) == ["ok"]

Expand Down
Loading