diff --git a/hamlpy/elements.py b/hamlpy/elements.py index d1cb1d6..1115eae 100644 --- a/hamlpy/elements.py +++ b/hamlpy/elements.py @@ -109,6 +109,31 @@ def _escape_attribute_quotes(self, v): return ''.join(escaped) + def _prepare_dynamic_attributes(self, attribute_dict_string): + try: + import ast + from codegen import to_source + node = ast.parse(attribute_dict_string, mode='eval') + s = [] + for k, v in zip(node.body.keys, node.body.values): + if isinstance(k, ast.Name): + #k = '"{{ %s }}"' % k.id # TODO: allow dynamic keys in settings + k = '"%s"' % k.id + else: + k = to_source(k) + if isinstance(v, ast.Str): # TODO: add allowed types + v = to_source(v) + else: + v = '"{{ %s }}"' % to_source(v) + s.append('%s: %s' % (k, v)) + attribute_dict_string = '{%s}' % ', '.join(s) + except Exception as e: + import traceback + traceback.print_exc() + print repr(e) + attribute_dict_string = 'error: "%s"' % repr(e).replace('"', "'") + return attribute_dict_string + def _parse_attribute_dictionary(self, attribute_dict_string): attributes_dict = {} if (attribute_dict_string): @@ -120,6 +145,8 @@ def _parse_attribute_dictionary(self, attribute_dict_string): attribute_dict_string = re.sub(self.RUBY_HAML_REGEX, '"\g":', attribute_dict_string) # Put double quotes around key attribute_dict_string = re.sub(self.ATTRIBUTE_REGEX, '\g
"\g":\g', attribute_dict_string)
+                # replace short notation for inline expressions
+                attribute_dict_string = self._prepare_dynamic_attributes(attribute_dict_string)
                 # Parse string as dictionary
                 attributes_dict = eval(attribute_dict_string)
                 for k, v in attributes_dict.items():
@@ -138,11 +165,12 @@ def _parse_attribute_dictionary(self, attribute_dict_string):
                                 
                             attributes_dict[k] = v
                             v = v.decode('utf-8')
-                            self.attributes += "%s=%s " % (k, self.attr_wrap(self._escape_attribute_quotes(v)))
+                            if not v.startswith('{{'):
+                                v = self._escape_attribute_quotes(v)
+                            self.attributes += "%s=%s " % (k, self.attr_wrap(v))
                 self.attributes = self.attributes.strip()
             except Exception, e:
-                raise Exception('failed to decode: %s' % attribute_dict_string)
-                #raise Exception('failed to decode: %s. Details: %s'%(attribute_dict_string, e))
+                raise Exception('failed to decode: %s. Error: %r' % (attribute_dict_string, e))
 
         return attributes_dict
 
diff --git a/setup.py b/setup.py
index a9fe101..a162fa9 100644
--- a/setup.py
+++ b/setup.py
@@ -12,6 +12,7 @@
       url = 'http://github.com/jessemiller/HamlPy',
       license = 'MIT',
       install_requires = [
+          'codegen',
           'django',
           'pygments',
           'markdown'