Skip to content
This repository has been archived by the owner on Aug 17, 2023. It is now read-only.

Commit

Permalink
[mod] simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
Psycojoker committed Sep 24, 2016
1 parent 7afb4dc commit 9224b44
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 14 deletions.
17 changes: 5 additions & 12 deletions hamlpy/elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,25 +112,20 @@ def _escape_attribute_quotes(self, v):
return ''.join(escaped)

def _parse_attribute_dictionary(self, attribute_dict_string):
class SingleVariableMarker(object):
pass

attributes_dict = {}

if (attribute_dict_string):
attribute_dict_string = attribute_dict_string.replace('\n', ' ')
try:
# converting all allowed attributes to python dictionary style

# When encountering single variables (like "required" or
# "visible") use an empty class instance as a trick to have
# both a unique and a non coliding marker because all of this
# will be converted as a python dictionary
attribute_dict_string = re.sub(self.SINGLE_VARIABLE_REGEX, '\g<before>SingleVariableMarker(): "\g<key>"\g<after>', attribute_dict_string)
print attribute_dict_string
# "visible") set its value to None
attribute_dict_string = re.sub(self.SINGLE_VARIABLE_REGEX, '\g<before>"\g<key>": None\g<after>', attribute_dict_string)

# do it both because the regex is too greedy and doesn't do it
# with single variable in between
attribute_dict_string = re.sub(self.SINGLE_VARIABLE_REGEX, '\g<before>SingleVariableMarker(): "\g<key>"\g<after>', attribute_dict_string)
attribute_dict_string = re.sub(self.SINGLE_VARIABLE_REGEX, '\g<before>"\g<key>": None\g<after>', attribute_dict_string)

# Replace Ruby-style HAML with Python style
attribute_dict_string = re.sub(self.RUBY_HAML_REGEX, '"\g<key>":', attribute_dict_string)
Expand All @@ -139,15 +134,13 @@ class SingleVariableMarker(object):
attribute_dict_string = re.sub(self.ATTRIBUTE_REGEX, '\g<pre>"\g<key>":\g<val>', attribute_dict_string)

# Parse string as dictionary
attributes_dict = eval(attribute_dict_string, {"SingleVariableMarker": SingleVariableMarker})
attributes_dict = eval(attribute_dict_string)
for k, v in attributes_dict.items():
if k != 'id' and k != 'class':
if isinstance(v, NoneType):
self.attributes += "%s " % (k,)
elif isinstance(v, int) or isinstance(v, float):
self.attributes += "%s=%s " % (k, self.attr_wrap(v))
elif isinstance(k, SingleVariableMarker):
self.attributes += "%s " % (v)
else:
# DEPRECATED: Replace variable in attributes (e.g. "= somevar") with Django version ("{{somevar}}")
v = re.sub(self.DJANGO_VARIABLE_REGEX, '{{\g<variable>}}', attributes_dict[k])
Expand Down
4 changes: 2 additions & 2 deletions hamlpy/test/hamlpy_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,11 @@ def test_dictionaries_define_no_value_attribute_several(self):
self.assertTrue(result.rstrip().endswith("/>"))

def test_dictionaries_define_no_value_attribute_several_a_lot(self):
haml = "%%input{a: 'b', %s}" % ", ".join(string.ascii_letters)
haml = "%%input{baba: 'b', %s}" % ", ".join(string.ascii_letters)
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
self.assertTrue("<input" in result)
self.assertTrue("a='b'" in result)
self.assertTrue("baba='b'" in result)
for i in string.ascii_letters:
self.assertTrue((" %s " % i) in result)
self.assertTrue(result.rstrip().endswith("/>"))
Expand Down

0 comments on commit 9224b44

Please sign in to comment.