diff options
| -rw-r--r-- | ishtar_common/ooo_replace.py | 44 | ||||
| -rw-r--r-- | ishtar_common/tests.py | 12 | ||||
| -rw-r--r-- | ishtar_common/tests/test-file.odt | bin | 9571 -> 10142 bytes | 
3 files changed, 38 insertions, 18 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': diff --git a/ishtar_common/tests.py b/ishtar_common/tests.py index 06d0ca3ec..b8369ad03 100644 --- a/ishtar_common/tests.py +++ b/ishtar_common/tests.py @@ -17,9 +17,10 @@  # See the file COPYING for details. -import tempfile +import tempfile, datetime  from zipfile import ZipFile, ZIP_DEFLATED +from django.conf import settings  from django.contrib.auth.models import User  from django.contrib.contenttypes.models import ContentType  from django.template.defaultfilters import slugify @@ -30,13 +31,20 @@ from ishtar_common.ooo_replace import ooo_replace  class OOOGenerationTest(TestCase):      def testGeneration(self): -        context = {'test-var':u"Testé", 'test-var2':u""} +        context = {'test_var':u"Testé", 'test_var2':u"", +                   "test_date":datetime.date(2015, 1, 1)}          tmp = tempfile.TemporaryFile()          ooo_replace("../ishtar_common/tests/test-file.odt", tmp, context)          inzip = ZipFile(tmp, 'r', ZIP_DEFLATED)          value = inzip.read('content.xml')          self.assertTrue("Testé" in value)          self.assertTrue("testé 2" not in value) +        self.assertTrue("2015" in value) +        lg, ct = settings.LANGUAGE_CODE.split('-') +        if lg == 'fr': +            self.assertTrue('janvier' in value) +        if lg == 'en': +            self.assertTrue('january' in value)  class MergeTest(TestCase):      def setUp(self): diff --git a/ishtar_common/tests/test-file.odt b/ishtar_common/tests/test-file.odtBinary files differ index 2e73fe0ee..2dcc18179 100644 --- a/ishtar_common/tests/test-file.odt +++ b/ishtar_common/tests/test-file.odt | 
