diff options
Diffstat (limited to 'ishtar_common/ooo_replace.py')
| -rw-r--r-- | ishtar_common/ooo_replace.py | 44 | 
1 files changed, 28 insertions, 16 deletions
diff --git a/ishtar_common/ooo_replace.py b/ishtar_common/ooo_replace.py index 7baad2439..76ff81e73 100644 --- a/ishtar_common/ooo_replace.py +++ b/ishtar_common/ooo_replace.py @@ -1,6 +1,6 @@  #!/usr/bin/python  # -*- coding: utf-8 -*- -# Copyright (C) 2013 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet> +# Copyright (C) 2013-2015 Étienne Loks  <etienne.loks_AT_peacefrogsDOTnet>  # This program is free software: you can redistribute it and/or modify  # it under the terms of the GNU Affero General Public License as @@ -81,36 +81,48 @@ def _format_value(value, default_value):      return value  VAR_EXPR = u"###VAR %s###" -WHOLE_KEY_FILTER = 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 % WHOLE_KEY_FILTER) -KEY_FILTER = u"(?: )*(<.*>)*(?: )*([-a-zA-Z0-9_]*)(?: )*" -KEY_FILTER = re.compile(KEY_FILTER) +TAG_FILTER = re.compile(u"(<[^<^>]*>)") +KEY_FILTER = re.compile(u"([-a-zA-Z0-9_]*)")  def _filter_key(base_key):      # return (key, extra_marker) +    # manage strange key such as: +    # test_<text:span text:style-name="T1">date</text:span>      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): +    tags, new_key = '', key[:] +    for tag in TAG_FILTER.findall(key): +        tags += tag +        new_key = new_key.replace(tag, '') +    full_key = '' +    for k in KEY_FILTER.findall(new_key): +        if not k: +            continue +        full_key += k +    return full_key, tags + +def _ishtar_parsing(context, value, default_value=''):      """      ###VAR nom_var### for displaying a variable name      ###IF nom_var### ###ENDIF### for conditionnal display      Be carreful nested condition are not yet managed!      """ -    for regexp, sub_exp in ((RE_IF, IF_EXPR), -                            (RE_VAR, VAR_EXPR)): +    for regexp, sub_exp, if_cond in ((RE_IF, IF_EXPR, True), +                                     (RE_VAR, VAR_EXPR, False)):          for base_key in regexp.findall(value): -            v = "" +            v, val = "", None +            if if_cond: # the value inside the if is parsed +                base_key, val = base_key              key, extra_markers = _filter_key(base_key)              if key in context and context[key]: -                v = unicode(context[key]) +                if if_cond: +                    v = _ishtar_parsing(context, val, default_value) +                else: +                    v = _format_value(context[key], default_value)              # to preserve a consistent OOO file put extra_markers              if extra_markers:                  v += extra_markers @@ -148,7 +160,7 @@ def ooo_replace(infile, outfile, context, default_value=''):      str_io = StringIO()      content.write(str_io)      value = str_io.getvalue() -    value = _ishtar_parsing(context, value).encode('utf-8') +    value = _ishtar_parsing(context, value, default_value).encode('utf-8')      for f in inzip.infolist():          if f.filename == 'content.xml':  | 
