diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-02-12 18:36:57 +0100 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-02-12 18:36:57 +0100 |
commit | 33edfdd319aa4f417de6bd75de07b43ff1dee6b9 (patch) | |
tree | 3c87ea9c84f4ce83f772ec7575261de6703a2bd7 | |
parent | 02b0e348e2e3986ee78b3221873a3d3de233576f (diff) | |
download | Ishtar-33edfdd319aa4f417de6bd75de07b43ff1dee6b9.tar.bz2 Ishtar-33edfdd319aa4f417de6bd75de07b43ff1dee6b9.zip |
Fix regexp matching on ooo_replace
Manage interfering marker like:
###VAR <text:span text:style-name="T7">adminact_signature_date###
Filter and put the markers after the replacement in order to have a
consistent file generation.
-rw-r--r-- | ishtar_common/ooo_replace.py | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py index 08ee17b63..7baad2439 100644 --- a/ishtar_common/ooo_replace.py +++ b/ishtar_common/ooo_replace.py @@ -81,9 +81,22 @@ def _format_value(value, default_value): return value VAR_EXPR = u"###VAR %s###" -RE_VAR = re.compile(u"###VAR %s###" % u"([-a-zA-Z0-9_]+)") +WHOLE_KEY_FILTER = u"((?: )*(?:<[^#>]*>)*(?: )*(?:[-a-zA-Z0-9_])+(?: )*)" +RE_VAR = re.compile(VAR_EXPR % WHOLE_KEY_FILTER) IF_EXPR = u"###IF %s###(.*)###ENDIF###" -RE_IF = re.compile(IF_EXPR % u"([-a-zA-Z0-9_]+)") +RE_IF = re.compile(IF_EXPR % WHOLE_KEY_FILTER) +KEY_FILTER = u"(?: )*(<.*>)*(?: )*([-a-zA-Z0-9_]*)(?: )*" +KEY_FILTER = re.compile(KEY_FILTER) + +def _filter_key(base_key): + # return (key, extra_marker) + key = base_key[:] + key = key.strip() + m = KEY_FILTER.match(key) + if not m: + return '', key + groups = m.groups() + return groups[1], groups[0] def _ishtar_parsing(context, value): """ @@ -91,16 +104,17 @@ def _ishtar_parsing(context, value): ###IF nom_var### ###ENDIF### for conditionnal display Be carreful nested condition are not yet managed! """ - for key, val in RE_IF.findall(value): - v = "" - if key in context and context[key]: - v = _ishtar_parsing(context, val) - value = re.sub(IF_EXPR % key, v, value) - for key in RE_VAR.findall(value): - v = "" - if key in context and context[key]: - v = unicode(context[key]) - value = re.sub(VAR_EXPR % key, v, value) + for regexp, sub_exp in ((RE_IF, IF_EXPR), + (RE_VAR, VAR_EXPR)): + for base_key in regexp.findall(value): + v = "" + key, extra_markers = _filter_key(base_key) + if key in context and context[key]: + v = unicode(context[key]) + # to preserve a consistent OOO file put extra_markers + if extra_markers: + v += extra_markers + value = re.sub(sub_exp % base_key, v, value) return value def ooo_replace(infile, outfile, context, default_value=''): |