Esempio n. 1
0
 def test_dictionaries_define_attributes(self):
     haml = "%html{'xmlns':'http://www.w3.org/1999/xhtml', 'xml:lang':'en', 'lang':'en'}"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     self.assertTrue("<html" in result)
     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>") or result.endswith("></html>\n"))
Esempio n. 2
0
 def test_haml_comment_nodes_dont_post_render_children(self):
     haml = '''
     -# My comment
         #my_div
             my text
     test
     '''
     html = "test"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.strip())
Esempio n. 3
0
        def load_template_source(self, template_name, *args, **kwargs):
            if not template_name.endswith('.haml'):
                raise TemplateDoesNotExist(template_name)

            haml_source, template_path = super(Loader,
                                               self).load_template_source(
                                                   template_name, *args,
                                                   **kwargs)
            hamlParser = hamlpy.Compiler()
            html = hamlParser.process(haml_source)

            return html, template_path
Esempio n. 4
0
    def test_xml_namespaces(self):
        haml = "%fb:tag\n  content"
        html = "<fb:tag>\n  content\n</fb:tag>\n"
        hamlParser = hamlpy.Compiler()
        result = hamlParser.process(haml)
        eq_(html, result)

        def test_pygments_filter(self):
            haml = ":highlight\nprint \"Hello, HamlPy World!"
            html = "<div class=\"highlight\"><pre><span class=\"k\">print</span> <span class=\"err\">&quot;</span><span class=\"n\">Hello</span><span class=\"p\">,</span> <span class=\"n\">HamlPy</span> <span class=\"n\">World</span><span class=\"o\">!</span></pre></div>"

            hamlParser = hamlpy.Compiler()

        result = hamlParser.process(haml)
        eq_(html, result)
Esempio n. 5
0
 def test_dictionaries_allow_conditionals(self):
     for (haml, html) in (
         ("%img{'src': 'hello' if coming}",
          "<img {% if coming %} src='hello' {% endif %} />"),
         ("%img{'src': 'hello' if coming else 'goodbye' }",
          "<img {% if coming %} src='hello' {% else %} src='goodbye' {% endif %} />"),
         ("%item{'a': 'one' if b == 1 else 'two' if b == [1, 2] else None}",
          "<item {% if b == 1 %} a='one' {% elif b == [1, 2] %} a='two' {% else %} a {% endif %}></item>"),
         # For id and class attributes, conditions work on individual parts
         # of the value (more parts can be added from HAML tag).
         ("%div{'id': 'No1' if tree is TheLarch, 'class': 'quite-a-long-way-away'}",
          "<div id='{% if tree is TheLarch %}No1{% endif %}' class='quite-a-long-way-away'></div>"),
          ("%div{'id': 'dog_kennel' if assisant.name == 'Mr Lambert' else 'mattress'}",
           "<div id='{% if assisant.name == 'Mr Lambert' %}dog_kennel{% else %}mattress{% endif %}'></div>"),
     ):
         hamlParser = hamlpy.Compiler()
         result = hamlParser.process(haml)
         self.assertEqual(html, result.replace('\n', ''))
Esempio n. 6
0
        def load_template_source(self, template_name, *args, **kwargs):
            _name, _extension = os.path.splitext(template_name)

            for extension in hamlpy.VALID_EXTENSIONS:
                try:
                    haml_source, template_path = super(
                        Loader, self).load_template_source(
                            self._generate_template_name(_name, extension),
                            *args, **kwargs)
                except TemplateDoesNotExist:
                    pass
                else:
                    hamlParser = hamlpy.Compiler()
                    html = hamlParser.process(haml_source)

                    return html, template_path

            raise TemplateDoesNotExist(template_name)
Esempio n. 7
0
        def load_template_source(self, template_name, *args, **kwargs):
            name, _extension = os.path.splitext(template_name)
            # os.path.splitext always returns a period at the start of extension
            extension = _extension.lstrip('.')

            if extension in hamlpy.VALID_EXTENSIONS:
                try:
                    haml_source, template_path = super(Loader, self).load_template_source(
                        self._generate_template_name(name, extension), *args, **kwargs
                    )
                except TemplateDoesNotExist:
                    pass
                else:
                    hamlParser = hamlpy.Compiler(options_dict=options_dict)
                    html = hamlParser.process(haml_source)

                    return html, template_path

            raise TemplateDoesNotExist(template_name)
Esempio n. 8
0
    def _compare_test_files(self, name):
        haml_lines = codecs.open('templates/' + name + '.hamlpy',
                                 encoding='utf-8').readlines()
        html = open('templates/' + name + '.html').read()

        haml_compiler = hamlpy.Compiler()
        parsed = haml_compiler.process_lines(haml_lines)

        # Ignore line ending differences
        parsed = parsed.replace('\r', '')
        html = html.replace('\r', '')

        if parsed != html:
            print('\nHTML (actual): ')
            print('\n'.join([
                "%d. %s" % (i + 1, l) for i, l in enumerate(parsed.split('\n'))
            ]))
            self._print_diff(parsed, html)
        eq_(parsed, html)
Esempio n. 9
0
    def test_pygments_filter(self):
        haml = '''
            :highlight
              print "hi"

              if x:
                  print "y":
              else:
                  print "z":
        '''
        html = '\n<div class="highlight"><pre><span class="n">print</span> &quot;<span class="n">hi</span>&quot;' \
                + '\n\n<span class="k">if</span> <span class="n">x</span><span class="p">:</span>' \
                + '\n    <span class="n">print</span> &quot;<span class="n">y</span>&quot;<span class="p">:</span>' \
                + '\n<span class="k">else</span><span class="p">:</span>' \
                + '\n    <span class="n">print</span> &quot;<span class="n">z</span>&quot;<span class="p">:</span>' \
                + '\n</pre></div>\n'

        hamlParser = hamlpy.Compiler()
        result = hamlParser.process(haml)
        eq_(html, result)
Esempio n. 10
0
        def get_contents(self, origin):
            """
            Used by Django 1.9+
            """
            name, _extension = os.path.splitext(origin.name)
            template_name, _extension = os.path.splitext(origin.template_name)

            for extension in hamlpy.VALID_EXTENSIONS:
                try_name = self._generate_template_name(name, extension)
                try_template_name = self._generate_template_name(template_name, extension)
                try_origin = Origin(try_name, try_template_name, origin.loader)
                try:
                    haml_source = super(Loader, self).get_contents(try_origin)
                except TemplateDoesNotExist:
                    pass
                else:
                    haml_parser = hamlpy.Compiler()
                    return haml_parser.process(haml_source)

            raise TemplateDoesNotExist(origin.template_name)
Esempio n. 11
0
    def _compare_test_files(self, name):
        haml_lines = codecs.open('templates/' + name + '.hamlpy',
                                 encoding='utf-8').readlines()
        html = open('templates/' + name + '.html').read()

        haml_compiler = hamlpy.Compiler()
        parsed = haml_compiler.process_lines(haml_lines)

        # Ignore line ending differences
        parsed = parsed.replace('\r', '')
        html = html.replace('\r', '')

        line, col, c1, c2 = self._find_diff(parsed, html)
        if line != -1:
            print 'HAML generated: '
            print '\n'.join(
                ["%d. %s" % (i, l) for i, l in enumerate(parsed.split('\n'))])
            print 'Difference begins at line', line, 'column', col
            print 'Character code in parsed:', ord(c1)
            print 'Character code in HTML file:', ord(c2)
        eq_(parsed, html)
Esempio n. 12
0
    def test_attr_wrapper(self):
        haml = """
%html{'xmlns':'http://www.w3.org/1999/xhtml', 'xml:lang':'en', 'lang':'en'}
  %body#main
    %div.wrap
      %a{:href => '/'}
:javascript"""
        hamlParser = hamlpy.Compiler(options_dict={'attr_wrapper': '"'})
        result = hamlParser.process(haml)
        self.assertEqual(result,
                         '''<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  <body id="main">
    <div class="wrap">
      <a href="/"></a>
    </div>
  </body>
</html>
<script type="text/javascript">
// <![CDATA[
// ]]>
</script>
''')
Esempio n. 13
0
 def test_single_line_conditional_comments_rendered_properly(self):
     haml = "/[if IE] You use a shitty browser"
     html = "<!--[if IE]> You use a shitty browser<![endif]-->\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 14
0
 def test_html_comments_rendered_properly(self):
     haml = '/ some comment'
     html = "<!-- some comment -->"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.replace('\n', ''))
Esempio n. 15
0
 def test_dictionaries_can_by_pythonic(self):
     haml = "%div{'id':['Article','1'], 'class':['article','entry','visible']} Booyaka"
     html = "<div id='Article_1' class='article entry visible'>Booyaka</div>"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     self.assertEqual(html, result.replace('\n', ''))
Esempio n. 16
0
 def test_dictionaries_support_arrays_for_id(self):
     haml = "%div{'id':('itemType', '5')}"
     html = "<div id='itemType_5'></div>"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     self.assertEqual(html, result.replace('\n', ''))
Esempio n. 17
0
 def test_utf8_with_regular_text(self):
     haml = u"%a{'href':'', 'title':'링크(Korean)'} Some Link"
     html = u"<a href='' title='\ub9c1\ud06c(Korean)'>Some Link</a>\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 18
0
 def test_stand_alone_django_variables_render(self):
     haml = '= story.tease'
     html = '{{ story.tease }}'
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.replace('\n', ''))
Esempio n. 19
0
 def test_if_else_django_tags_render(self):
     haml = '- if something\n   %p hello\n- else\n   %p goodbye'
     html = '{% if something %}\n   <p>hello</p>\n{% else %}\n   <p>goodbye</p>\n{% endif %}\n'
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 20
0
 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.replace('\n', ''))
Esempio n. 21
0
 def test_doctype_xml_encoding(self):
     haml = '!!! XML iso-8859-1'
     html = "<?xml version='1.0' encoding='iso-8859-1' ?>"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.replace('\n', ''))
Esempio n. 22
0
 def test_doctype_xhtml(self):
     haml = '!!!'
     html = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">'
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.replace('\n', ''))
Esempio n. 23
0
 def test_doctype_html5(self):
     haml = '!!! 5'
     html = '<!DOCTYPE html>'
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result.replace('\n', ''))
Esempio n. 24
0
 def test_python_filter(self):
     haml = ":python\n   for i in range(0, 5): print \"<p>item \%s</p>\" % i"
     html = '<p>item \\0</p>\n<p>item \\1</p>\n<p>item \\2</p>\n<p>item \\3</p>\n<p>item \\4</p>\n'
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 25
0
 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.replace('\n', ''))
Esempio n. 26
0
 def test_plain_filter_with_no_children(self):
     haml = ":plain\nNothing"
     html = "Nothing\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 27
0
 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.replace('\n', ''))
Esempio n. 28
0
 def test_filters_render_escaped_backslash(self):
     haml = ":plain\n  \\Something"
     html = "\\Something\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 29
0
 def test_plain_filter_with_indentation(self):
     haml = ":plain\n    -This should be plain text\n    .This should be more\n      This should be indented"
     html = "-This should be plain text\n.This should be more\n  This should be indented\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)
Esempio n. 30
0
 def test_xml_namespaces(self):
     haml = "%fb:tag\n  content"
     html = "<fb:tag>\n  content\n</fb:tag>\n"
     hamlParser = hamlpy.Compiler()
     result = hamlParser.process(haml)
     eq_(html, result)