Reimplement _unescape_entities routine.

</?text:s.*?> entities are not longer escaped.
This functionallity was used when secretary handled plain text
jinja instructions. This now is no longer supported since the
official method to add instructions is through input fields.
This commit is contained in:
Christopher 2015-12-21 11:29:41 -06:00
parent 5daa1a2c2b
commit 3a2b4b9c3b
2 changed files with 33 additions and 22 deletions

View file

@ -218,6 +218,31 @@ class Renderer(object):
self.environment.block_end_string self.environment.block_end_string
)) ))
self._compile_escape_expressions()
def _compile_escape_expressions(self):
# Compiles escape expressions
self.escape_map = dict()
unescape_rules = {
r'&gt;' : r'>',
r'&lt;' : r'<',
r'&amp;' : r'&',
r'&quot;' : r'"',
}
for key, value in unescape_rules.items():
exp = r'(?is)(({0}|{1}).*?)({2})(.*?({3}|{4}))'
key = re.compile(exp.format(
self.environment.variable_start_string,
self.environment.block_start_string,
key,
self.environment.variable_end_string,
self.environment.block_end_string
))
self.escape_map[key] = r'\1{0}\4'.format(value)
def _is_jinja_tag(self, tag): def _is_jinja_tag(self, tag):
""" """
@ -366,24 +391,16 @@ class Renderer(object):
placeholder_parent.removeChild(placeholder) placeholder_parent.removeChild(placeholder)
@staticmethod def _unescape_entities(self, xml_text):
def _unescape_entities(xml_text):
""" """
Strips tags of the form <text:span ...> from inside Jinja elements Unescape '&amp;', '&lt;', '&quot;' and '&gt;' within jinja instructions.
and unescapes HTML codes for >, <, & and " The regexs rules used here are compiled in _compile_escape_expressions.
""" """
unescape_rules = { for regexp, replacement in self.escape_map.items():
r'(?is)({([{%])[^%}]*?)(</?text:s.*?>)(.*?[%}]})': r'\1 \4', while True:
r'(?is)({([{%])[^%}]*?)(&gt;)(.*?[%}]})' : r'\1>\4', xml_text, substitutions = regexp.subn(replacement, xml_text)
r'(?is)({([{%])[^%}]*?)(&lt;)(.*?[%}]})' : r'\1<\4', if not substitutions:
r'(?is)({([{%])[^%}]*?)(&amp;)(.*?[%}]})' : r'\1&\4', break
r'(?is)({([{%])[^%}]*?)(&quot;)(.*?[%}]})' : r'\1"\4',
}
for regexp, replacement in unescape_rules.items():
subs_made = True
while subs_made:
xml_text, subs_made = re.subn(regexp, replacement, xml_text)
return xml_text return xml_text

View file

@ -43,12 +43,6 @@ class RenderTestCase(TestCase):
'{{ a &lt; b }}' : '{{ a < b }}', '{{ a &lt; b }}' : '{{ a < b }}',
'{% a|filter &lt; b %}' : '{% a|filter < b %}', '{% a|filter &lt; b %}' : '{% a|filter < b %}',
'<node>{% a == b %}</node>{% else if a &lt; b %}': '<node>{% a == b %}</node>{% else if a < b %}', '<node>{% a == b %}</node>{% else if a &lt; b %}': '<node>{% a == b %}</node>{% else if a < b %}',
# test scapig of multiple spaces, even encoded as <text:space> nodes
'{{ if <text:s> multiple_spaces }}' : '{{ if multiple_spaces }}',
'{{ if </text:s> multiple_spaces }}' : '{{ if multiple_spaces }}',
'{{ if <text:s/> multiple_spaces }}' : '{{ if multiple_spaces }}',
'{{ if <text:span/>[1,2,3]<text:span>&lt;</text:span>2 }}': '{{ if [1,2,3] < 2 }}',
} }
for test, expect in test_samples.items(): for test, expect in test_samples.items():