From a4e2a18d939fcd3f0be200f52d598713276f467f Mon Sep 17 00:00:00 2001 From: Arkadiusz Adamski Date: Thu, 6 Mar 2014 22:18:19 +0100 Subject: [PATCH] Change regexp search method --- hamlpy/elements.py | 33 ++++++++++++++++++++------------- hamlpy/test/hamlpy_test.py | 7 +++++++ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/hamlpy/elements.py b/hamlpy/elements.py index 3bb5d3d..03b231f 100644 --- a/hamlpy/elements.py +++ b/hamlpy/elements.py @@ -52,17 +52,17 @@ def attr_wrap(self, value): return '%s%s%s' % (self.attr_wrapper, value, self.attr_wrapper) def _parse_haml(self): - split_tags = self.HAML_REGEX.search(self.haml).groupdict('') - - self.attributes_dict = self._parse_attribute_dictionary(split_tags.get('attributes')) - self.tag = split_tags.get('tag').strip(self.ELEMENT) or 'div' - self.id = self._parse_id(split_tags.get('id')) - self.classes = ('%s %s' % (split_tags.get('class').lstrip(self.CLASS).replace('.', ' '), self._parse_class_from_attributes_dict())).strip() - self.self_close = split_tags.get('selfclose') or self.tag in self.self_closing_tags - self.nuke_inner_whitespace = split_tags.get('nuke_inner_whitespace') != '' - self.nuke_outer_whitespace = split_tags.get('nuke_outer_whitespace') != '' - self.django_variable = split_tags.get('django') != '' - self.inline_content = split_tags.get('inline').strip() + split_tags = self._split_haml(self.haml) + + self.attributes_dict = self._parse_attribute_dictionary(split_tags.get('attributes', '')) + self.tag = split_tags.get('tag', '').strip(self.ELEMENT) or 'div' + self.id = self._parse_id(split_tags.get('id', '')) + self.classes = ('%s %s' % (split_tags.get('class', '').lstrip(self.CLASS).replace('.', ' '), self._parse_class_from_attributes_dict())).strip() + self.self_close = split_tags.get('selfclose', '') or self.tag in self.self_closing_tags + self.nuke_inner_whitespace = split_tags.get('nuke_inner_whitespace', '') != '' + self.nuke_outer_whitespace = split_tags.get('nuke_outer_whitespace', '') != '' + self.django_variable = split_tags.get('django', '') != '' + self.inline_content = split_tags.get('inline', '').strip() def _parse_class_from_attributes_dict(self): clazz = self.attributes_dict.get('class', '') @@ -146,6 +146,13 @@ def _parse_attribute_dictionary(self, attribute_dict_string): return attributes_dict + def _split_haml(self, txt): + result = {} + def merge(data): + kwargs = {key: value for key, value in data.iteritems() if value} + if bool(kwargs): + result.update(kwargs) - - + groups = [m.groupdict() for m in self.HAML_REGEX.finditer(txt)] + map(merge, groups) + return result diff --git a/hamlpy/test/hamlpy_test.py b/hamlpy/test/hamlpy_test.py index 5601c96..e590738 100755 --- a/hamlpy/test/hamlpy_test.py +++ b/hamlpy/test/hamlpy_test.py @@ -19,6 +19,13 @@ def test_non_ascii_id_allowed(self): result = hamlParser.process(haml) self.assertEqual(html, result.replace('\n', '')) + def test_applies_class_and_id_properly(self): + haml = '%div.someClass#someId' + html = "
" + hamlParser = hamlpy.Compiler() + result = hamlParser.process(haml) + self.assertEqual(html, result.replace('\n', '')) + def test_applies_class_properly(self): haml = '%div.someClass Some text' html = "
Some text
"