summaryrefslogtreecommitdiff
path: root/ishtar_common/ooo_replace.py
diff options
context:
space:
mode:
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
commit566a3c81552e72ec7db11eb6d264ae9f423893a5 (patch)
tree3c87ea9c84f4ce83f772ec7575261de6703a2bd7 /ishtar_common/ooo_replace.py
parent364d678f720679d55ae60c7cb4e3790f8b95d436 (diff)
downloadIshtar-566a3c81552e72ec7db11eb6d264ae9f423893a5.tar.bz2
Ishtar-566a3c81552e72ec7db11eb6d264ae9f423893a5.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.
Diffstat (limited to 'ishtar_common/ooo_replace.py')
-rw-r--r--ishtar_common/ooo_replace.py38
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=''):