Skip to content

Regular expression in patch_xml problem - removing Jinja tags #81

Description

@naro

I have a DOCX template like this:

Available answers:
    {% for answer in answers %}{{r answer.text }}
    {% endfor %}

which produces a XML snippet

<w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr><w:b w:val="false"/><w:b w:val="false"/><w:bCs w:val="false"/></w:rPr></w:pPr><w:r><w:rPr><w:b w:val="false"/><w:bCs w:val="false"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>Available answers:</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:ind w:left="709" w:hanging="0"/><w:rPr><w:rFonts w:ascii="Verdana" w:hAnsi="Verdana"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr></w:pPr><w:r><w:rPr><w:b w:val="false"/><w:bCs w:val="false"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>{% for answer in answers %}{{r answer.text }}</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:ind w:left="709" w:hanging="0"/><w:rPr/></w:pPr><w:r><w:rPr><w:b w:val="false"/><w:bCs w:val="false"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>{% endfor %}</w:t></w:r></w:p>

And the regular expression in patch_xml causing problems:

<w:r[ >](?:(?!<w:r[ >]).)*({%|{{)r ([^}%]*(?:%}|}})).*?</w:r>

When this code is passed through patch_xml method, the {% for %} tag is accidentally removed and rendering fails on Encountered unknown tag 'endfor' message
Output:

<w:p><w:pPr><w:pStyle w:val="Normal"/><w:rPr><w:b w:val="false"/><w:b w:val="false"/><w:bCs w:val="false"/></w:rPr></w:pPr><w:r><w:rPr><w:b w:val="false"/><w:bCs w:val="false"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>Available answers:</w:t></w:r></w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:ind w:left="709" w:hanging="0"/><w:rPr><w:rFonts w:ascii="Verdana" w:hAnsi="Verdana"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr></w:pPr>{{ answer.text }}</w:p><w:p><w:pPr><w:pStyle w:val="Normal"/><w:ind w:left="709" w:hanging="0"/><w:rPr/></w:pPr><w:r><w:rPr><w:b w:val="false"/><w:bCs w:val="false"/><w:sz w:val="16"/><w:szCs w:val="16"/></w:rPr><w:t>{% endfor %}</w:t></w:r></w:p>

The correct behaviour would be to leave all Jinja markup in the output:
{% for answer in answers %}{{r answer.text }}

Regex101 snippet: https://regex101.com/r/3I5Otf/2

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions