Permalink
Browse files

Added proper output formating. Fixed all the tests.

Thanks for the initial idea from:
Merge http://github.com/fitoria/HamlPy
  • Loading branch information...
2 parents 4ecd2e4 + 0922235 commit 624c9da971cad5a5e8d59354854526f8fb4a4190 @jessemiller committed Sep 2, 2010
View
@@ -1,7 +1,4 @@
-.project
-.pydevproject
*.pyc
dist
build
hamlpy.egg-info
-
View
@@ -61,25 +61,27 @@ def render(self):
def render_internal_nodes(self):
result = ''
for node in self.internal_nodes:
- result += node.render()
+ result += ('%s\n') % node.render()
return result
def has_internal_nodes(self):
return len(self.internal_nodes) > 0
def should_contain(self, node):
return False
-
+
class HamlNode(RootNode):
def __init__(self, haml):
RootNode.__init__(self)
self.haml = haml.strip()
self.raw_haml = haml
self.indentation = (len(haml) - len(haml.lstrip()))
+ self.spaces = ''.join(' ' for i in range(self.indentation))
+
def render(self):
- return self.haml
+ return "%s%s" % (self.spaces, self.haml)
class ElementNode(HamlNode):
@@ -96,7 +98,11 @@ def _render_tag(self):
return self._generate_html(element)
def _generate_html(self, element):
- result = "<%s" % element.tag
+ if self.indentation > 0:
+ result = "%s<%s" % (self.spaces, element.tag)
+ else:
+ result = "<%s" % element.tag
+
if element.id:
result += " id='%s'" % element.id
if element.classes:
@@ -115,7 +121,7 @@ def _generate_html(self, element):
def _render_tag_content(self, current_tag_content):
if self.has_internal_nodes():
- current_tag_content = self.render_internal_nodes()
+ current_tag_content = '\n' + self.render_internal_nodes() + self.spaces
if current_tag_content == None:
current_tag_content = ''
if self.django_variable:
@@ -131,11 +137,11 @@ def __init__(self, haml):
def render(self):
content = ''
if self.has_internal_nodes():
- content = self.render_internal_nodes()
+ content = '\n' + self.render_internal_nodes()
else:
- content = self.haml
+ content = self.haml + ' '
- return "<!-- %s -->" % content
+ return "<!-- %s-->" % content
class HamlCommentNode(HamlNode):
def __init__(self, haml):
@@ -151,7 +157,7 @@ def __init__(self, haml):
def render(self):
tag_content = self.haml.lstrip(VARIABLE)
- return self._render_tag_content(tag_content)
+ return "%s%s" % (self.spaces, self._render_tag_content(tag_content))
class TagNode(HamlNode):
self_closing = {'for':'endfor', 'if':'endif', 'block':'endblock'}
@@ -166,11 +172,11 @@ def __init__(self, haml):
def render(self):
internal = self.render_internal_nodes()
- output = "{%% %s %%}%s" % (self.tag_statement, internal)
+ output = "%s{%% %s %%}\n%s" % (self.spaces, self.tag_statement, internal)
if (self.tag_name in self.self_closing.keys()):
- output += '{%% %s %%}' % self.self_closing[self.tag_name]
+ output += '%s{%% %s %%}' % (self.spaces, self.self_closing[self.tag_name])
return output
def should_contain(self, node):
return (isinstance(node,TagNode) and node.tag_name == 'else')
-
+
@@ -44,4 +44,10 @@ def test_adds_multiple_nodes_to_one(self):
start.add_node(two)
start.add_node(three)
- self.assertEqual(3, len(start.internal_nodes))
+ self.assertEqual(3, len(start.internal_nodes))
+
+ def test_html_indentation_vs_haml_indentation(self):
+ pass
+
+if __name__ == "__main__":
+ unittest.main()
@@ -3,37 +3,27 @@
from hamlpy import hamlpy
class HamlPyTest(unittest.TestCase):
- htmlValues = ( ('<h1></h1>', '%h1'),
- ('<div></div>', '%div'),
- ('<one><two><three>Hey there</three></two></one>', '%one\n %two\n %three Hey there'),
- ('<gee><whiz>Wow this is cool!</whiz></gee>', '%gee\n %whiz\n Wow this is cool!'))
-
- def test_outputs_simply_html_properly(self):
- hamlParser = hamlpy.Compiler()
- for html, haml in self.htmlValues:
- result = hamlParser.process(haml)
- assert html == result
def test_applies_id_properly(self):
haml = '%div#someId Some text'
html = "<div id='someId'>Some text</div>"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- self.assertEqual(html, result)
+ self.assertEqual(html, result.replace('\n', ''))
def test_applies_class_properly(self):
haml = '%div.someClass Some text'
html = "<div class='someClass'>Some text</div>"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- self.assertEqual(html, result)
+ self.assertEqual(html, result.replace('\n', ''))
def test_applies_multiple_classes_properly(self):
haml = '%div.someClass.anotherClass Some text'
html = "<div class='someClass anotherClass'>Some text</div>"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- self.assertEqual(html, result)
+ self.assertEqual(html, result.replace('\n', ''))
def test_dictionaries_define_attributes(self):
haml = "%html{'xmlns':'http://www.w3.org/1999/xhtml', 'xml:lang':'en', 'lang':'en'}"
@@ -43,46 +33,46 @@ def test_dictionaries_define_attributes(self):
self.assertTrue("xmlns='http://www.w3.org/1999/xhtml'" in result)
self.assertTrue("xml:lang='en'" in result)
self.assertTrue("lang='en'" in result)
- self.assertTrue(result.endswith("></html>"))
+ self.assertTrue(result.endswith("></html>") or result.endswith("></html>\n"))
def testDictionariesSupportArraysForId(self):
haml = "%div{'id':('itemType', '5')}"
html = "<div id='itemType_5'></div>"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- self.assertEqual(html, result)
+ self.assertEqual(html, result.replace('\n', ''))
def test_html_comments_rendered_properly(self):
haml = '/ some comment'
html = "<!-- some comment -->"
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- eq_(html, result)
+ eq_(html, result.replace('\n', ''))
def test_django_variables_on_tag_render_properly(self):
haml = '%div= story.tease'
html = '<div>{{ story.tease }}</div>'
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- eq_(html, result)
+ eq_(html, result.replace('\n', ''))
def test_stand_alone_django_variables_render(self):
haml = '= story.tease'
html = '{{ story.tease }}'
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- eq_(html, result)
+ eq_(html, result.replace('\n', ''))
def test_stand_alone_django_tags_render(self):
haml = '- extends "something.html"'
html = '{% extends "something.html" %}'
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
- eq_(html, result)
+ eq_(html, result.replace('\n', ''))
def test_if_else_django_tags_render(self):
haml = '- if something\n %p hello\n- else\n %p goodbye'
- html = '{% if something %}<p>hello</p>{% else %}<p>goodbye</p>{% endif %}'
+ html = '{% if something %}\n <p>hello</p>\n{% else %}\n <p>goodbye</p>\n\n{% endif %}\n'
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
eq_(html, result)
@@ -93,4 +83,4 @@ def test_throws_exception_when_trying_to_close_django(self):
hamlParser = hamlpy.Compiler()
result = hamlParser.process(haml)
-
+
@@ -48,8 +48,3 @@ def test_equals_symbol_creates_variable_node(self):
def test_dash_symbol_creates_tag_node(self):
node = nodes.create_node('- for something in somethings')
assert isinstance(node, nodes.TagNode)
-
-
-
-
-
@@ -35,4 +35,4 @@ def _compare_test_files(self, name):
haml_compiler = hamlpy.Compiler()
parsed = haml_compiler.process_lines(haml_lines)
eq_(parsed, html)
-
+
@@ -1 +1,3 @@
-<div id='Article_123' class='article entry true'>Now this is interesting</div>
+<div id='Article_123' class='article entry true'>
+ Now this is interesting
+</div>
@@ -1,13 +1,13 @@
- extends "base_generic.html"
- block title
- = section.title
+ = section.title
- block content
- %h1= section.title
+ %h1= section.title
- - for story in story_list
- %h2
- %a{'href':'{{ story.get_absolute_url }}'}
- = story.headline|upper
- %p= story.tease|truncatewords:"100"
+ - for story in story_list
+ %h2
+ %a{'href':'{{ story.get_absolute_url }}'}
+ = story.headline|upper
+ %p= story.tease|truncatewords:"100"
@@ -1 +1,16 @@
-{% extends "base_generic.html" %}{% block title %}{{ section.title }}{% endblock %}{% block content %}<h1>{{ section.title }}</h1>{% for story in story_list %}<h2><a href='{{ story.get_absolute_url }}'>{{ story.headline|upper }}</a></h2><p>{{ story.tease|truncatewords:"100" }}</p>{% endfor %}{% endblock %}
+{% extends "base_generic.html" %}
+
+{% block title %}
+ {{ section.title }}
+{% endblock %}
+{% block content %}
+ <h1>{{ section.title }}</h1>
+ {% for story in story_list %}
+ <h2>
+ <a href='{{ story.get_absolute_url }}'>
+ {{ story.headline|upper }}
+ </a>
+ </h2>
+ <p>{{ story.tease|truncatewords:"100" }}</p>
+ {% endfor %}
+{% endblock %}
@@ -1,12 +1,11 @@
%div
- %div
- -# These comments won't show up
- This will
- -#
- None of this
- Inside of here will show
- up.
- Yikes!
- %div More
+ %div
+ -# These comments won't show up
+ This will
+ -#
+ None of this
+ Inside of here will show
+ up.
+ Yikes!
+ %div More
%div Hello
-
@@ -1 +1,9 @@
-<div><div>This will</div><div>More</div></div><div>Hello</div>
+<div>
+ <div>
+
+ This will
+
+ </div>
+ <div>More</div>
+</div>
+<div>Hello</div>
@@ -1,6 +1,5 @@
.articles
- #article_1
- .title.bold So happy
- #content_1.content
- Finally, I can use HAML again!
-
+ #article_1
+ .title.bold So happy
+ #content_1.content
+ Finally, I can use HAML again!
@@ -1 +1,8 @@
-<div class='articles'><div id='article_1'><div class='title bold'>So happy</div><div id='content_1' class='content'>Finally, I can use HAML again!</div></div></div>
+<div class='articles'>
+ <div id='article_1'>
+ <div class='title bold'>So happy</div>
+ <div id='content_1' class='content'>
+ Finally, I can use HAML again!
+ </div>
+ </div>
+</div>
@@ -1,4 +1,4 @@
/
%div.someClass
- %div
- None of this matters
+ %div
+ None of this matters
@@ -1 +1,7 @@
-<!-- <div class='someClass'><div>None of this matters</div></div> -->
+<!--
+ <div class='someClass'>
+ <div>
+ None of this matters
+ </div>
+ </div>
+-->
@@ -1,3 +1,3 @@
%ul
- - for story in story_list
- %li= story.text
+ - for story in story_list
+ %li= story.text
@@ -1 +1,5 @@
-<ul>{% for story in story_list %}<li>{{ story.text }}</li>{% endfor %}</ul>
+<ul>
+ {% for story in story_list %}
+ <li>{{ story.text }}</li>
+ {% endfor %}
+</ul>
@@ -6,4 +6,4 @@
%link
%script
%br
-%hr
+%hr
@@ -1 +1,9 @@
-<div /><br /><br /><meta content='text/html' /><img /><link /><script /><br /><hr />
+<div />
+<br />
+<br />
+<meta content='text/html' />
+<img />
+<link />
+<script />
+<br />
+<hr />
Oops, something went wrong.

0 comments on commit 624c9da

Please sign in to comment.