wichert / lingua

Translation toolkit for Python
Other
46 stars 32 forks source link

Parse error in chameleon template with python expression that contains nested curly braces #29

Closed alveraan closed 10 years ago

alveraan commented 10 years ago

Using a python2.7 virtualenv with lingua 2.1 installed, I'm trying to do the following on a chameleon template:

$ $ENV/bin/pot-create -c lingua.cfg -d test test.pt

Where lingua.cfg contains:

[extension:.pt]
plugin = xml

[extension:.py]
plugin = python

and test.pt contains:

<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  i18n:domain="test">
  <head><title>tetst</title></head>
  <body>
    <a href="${request.route_url('set_locale', _query={'somevar': 'somevalue'})}">Test</a>
  </body>
</html>

This fails with the following error:

Aborting due to parse error in ./test.pt[9]: request.route_url('set_locale', _query={'somevar': 'somevalue'

I think it has to do with the nested curly braces, because running the same command on

<html xmlns:i18n="http://xml.zope.org/namespaces/i18n"
  i18n:domain="test">
  <head><title>tetst</title></head>
  <body>
    <a href="${request.route_url('set_locale')}">Test</a>
  </body>
</html>

works without error.

wichert commented 10 years ago

Here is a failing testcase:

@pytest.mark.usefixtures('fake_source')
def test_curly_brace_in_python_attribute_expression():
    global source
    source = b'''\
            <html>
              <a href="${request.route_url('set_locale', _query={'locale': 'de'})}"></a>
            </html>
            '''
    list(extract_xml('filename', _options()))

The underlying problem is that the utility function to extract python expressions from an attribute value is somewhat naive and will stop at the first closing curly brace.