Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change regexp search method #158

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 20 additions & 13 deletions hamlpy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', '')
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions hamlpy/test/hamlpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<div id='someId' class='someClass'></div>"
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 = "<div class='someClass'>Some text</div>"
Expand Down