diff options
-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=''): |