Skip to content

Commit ebc770a

Browse files
committed
Updated RichText Jinja tags from rr/rp to r/q and updated test template
1 parent aa7b7a7 commit ebc770a

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

docxtpl/template.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ def cellbg(m):
160160
flags=re.DOTALL,
161161
)
162162
src_xml = re.sub(
163-
r"({{r.\s.*?}}|{%r.\s.*?%})",
163+
r"({{[rq]\s.*?}}|{%[rq].\s.*?%})",
164164
r'</w:t></w:r><w:r><w:t xml:space="preserve">\1</w:t></w:r><w:r><w:t xml:space="preserve">',
165165
src_xml,
166166
flags=re.DOTALL,
@@ -173,7 +173,7 @@ def cellbg(m):
173173
r"-%}(?:(?!<w:t[ >]|{%|{{).)*?<w:t[^>]*?>", "%}", src_xml, flags=re.DOTALL
174174
)
175175

176-
for y in ["tr", "tc", "p"]:
176+
for y in ["tr", "tc", "p", "r"]:
177177
# replace into xml code the row/paragraph/run containing
178178
# {%y xxx %} or {{y xxx}} template tag
179179
# by {% xxx %} or {{ xx }} without any surrounding <w:y> tags :
@@ -184,17 +184,17 @@ def cellbg(m):
184184
)
185185
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
186186

187-
for y in ["p", "r"]:
188-
# replace into xml paragraph or run containing
189-
# {%rp xxx %} or {{rp xxx}} template tag
190-
# by {% xxx %} or {{ xx }} without any surrounding <w:p> tags
191-
# This allow for inline {rr <var> }} and paragraph {rp <var> }) styling
192-
# This is mandatory to have jinja2 generating correct xml code
193-
pat = (
194-
r"<w:%(y)s[ >](?:(?!<w:%(y)s[ >]).)*({%%|{{)r%(y)s ([^}%%]*(?:%%}|}})).*?</w:%(y)s>"
195-
% {"y": y}
196-
)
197-
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
187+
# For paragraph level richtext
188+
# replace into xml paragraph containing
189+
# {%q xxx %} or {{q xxx}} template tag
190+
# by {% xxx %} or {{ xx }} without any surrounding <w:p> tags
191+
# This allow for inline {r <var> }} and paragraph {q <var> }) styling
192+
# This is mandatory to have jinja2 generating correct xml code
193+
pat = (
194+
r"<w:p[ >](?:(?!<w:p[ >]).)*({%%|{{)q ([^}%%]*(?:%%}|}})).*?</w:p>"
195+
196+
)
197+
src_xml = re.sub(pat, r"\1 \2", src_xml, flags=re.DOTALL)
198198

199199
for y in ["tr", "tc", "p"]:
200200
# same thing, but for {#y xxx #} (but not where y == 'r', since that

tests/richtextparagraph.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,20 @@
1212
rt = RichText()
1313

1414
rtp.add("The rich text paragraph function allows paragraph styles to be added to text",parastyle="myrichparastyle")
15+
rtp.add("Any built in paragraph style can be used", parastyle="IntenseQuote")
16+
rtp.add("or you can add your own, unlocking all style options", parastyle="createdStyle")
17+
rtp.add("To use, just create a style in your template word doc with the formatting you want and call it in the code.", parastyle="normal")
1518

1619
rtp.add("This allows for the use of")
17-
rtp.add("bullet\apoints.", parastyle="SquareBullet")
20+
rtp.add("custom bullet\apoints", parastyle="SquareBullet")
21+
rtp.add("Numbered Bullet Points", parastyle="BasicNumbered")
22+
rtp.add("and Alpha Bullet Points.", parastyle="alphaBracketNumbering")
23+
rtp.add("You can", parastyle="normal")
24+
rtp.add("set the", parastyle="centerAlign")
25+
rtp.add("text alignment", parastyle="rightAlign")
26+
rtp.add("as well as the spacing between lines of text. Like this for example, this text has very tight spacing between the lines.\aIt also has no space between paragraphs of the same style.", parastyle="TightLineSpacing")
27+
rtp.add("Unlike this one, which has extra large spacing between lines for when you want to space things out a bit or just write a little less.", parastyle="WideLineSpacing")
28+
rtp.add("You can also set the background colour of a line.", parastyle="LineShadingGreen")
1829

1930
rt.add("This works with ")
2031
rt.add("Rich ", bold=True)
2.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)