Repository: edgewall/genshi

Issue:
Translator filter merge sub-directives
I have wrote a test that shows the problem: https://github.com/cedk/genshi/commit/53f60233156b87190f03b7dca3f59cd6acaba56b

Discussion:

Patch:
diff --git a/genshi/template/markup.py b/genshi/template/markup.py
index 22f0602..dd441b9 100644
--- a/genshi/template/markup.py
+++ b/genshi/template/markup.py
@@ -172,11 +172,14 @@ class MarkupTemplate(Template):
                     ]
 
             elif kind is SUB:
-                directives, substream = data
-                substream = self._extract_directives(substream, namespace,
+                directives, prev_substream = data
+                substream = self._extract_directives(prev_substream, namespace,
                                                      factory)
 
-                if len(substream) == 1 and substream[0][0] is SUB:
+                if (len(substream) == 1 and substream[0][0] is SUB
+                        # merge only if the direct substream has changed
+                        and (prev_substream[0][0] is not SUB
+                            or prev_substream[0][1][0] != substream[0][1][0])):
                     added_directives, substream = substream[0][1]
                     directives += added_directives
 


Test patch:
diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml
deleted file mode 100644
index 936554a..0000000
--- a/.github/workflows/tests.yml
+++ /dev/null
@@ -1,22 +0,0 @@
-name: run
-
-on: [push]
-
-jobs:
-  tests:
-    runs-on: ubuntu-latest
-    strategy:
-      matrix:
-        python-version: [2.7, 3.6, 3.7, 3.8, 3.9, '3.10-dev', pypy2, pypy3]
-
-    steps:
-    - uses: actions/checkout@v2
-    - name: Set up Python ${{ matrix.python-version }}
-      uses: actions/setup-python@v2
-      with:
-        python-version: ${{ matrix.python-version }}
-
-    - name: Run test suite
-      run: |
-        python setup.py test
-
diff --git a/genshi/filters/tests/i18n.py b/genshi/filters/tests/i18n.py
index 80d0924..18548f2 100644
--- a/genshi/filters/tests/i18n.py
+++ b/genshi/filters/tests/i18n.py
@@ -301,6 +301,19 @@ class TranslatorTestCase(unittest.TestCase):
           <span title="Voh">...</span>
         </html>""", tmpl.generate().render())
 
+    def test_translate_nested_directives(self):
+        html = """<html xmlns:py="http://genshi.edgewall.org/">
+            <py:with vars="x = 'test'"><py:if test="x">
+            </py:if></py:with>
+        </html>"""
+        tmpl = MarkupTemplate(html)
+        raw = tmpl.generate().render()
+        tmpl = MarkupTemplate(html)
+        translator = Translator(DummyTranslations())
+        translator.setup(tmpl)
+        translated = tmpl.generate().render()
+        self.assertEqual(raw, translated)
+
 
 class MsgDirectiveTestCase(unittest.TestCase):