Exemplo n.º 1
0
    def _test_error(self, haml, expected_message, expected_cause=None, compiler_options=None):
        compiler = Compiler(compiler_options)

        try:
            compiler.process(haml)
            self.fail("Expected exception to be raised")
        except Exception as e:
            self.assertIsInstance(e, ParseException)
            assert str(e) == expected_message

            if expected_cause:
                assert type(e.__cause__) == expected_cause
Exemplo n.º 2
0
    def _test_error(self, haml, expected_message, expected_cause=None, compiler_options=None):
        compiler = Compiler(compiler_options)

        try:
            compiler.process(haml)
            self.fail("Expected exception to be raised")
        except Exception as e:
            self.assertIsInstance(e, ParseException)
            assert str(e) == expected_message

            if expected_cause:
                assert type(e.__cause__) == expected_cause
Exemplo n.º 3
0
    def _test(self, haml, expected_html, compiler_options=None):
        compiler = Compiler(compiler_options)
        result = compiler.process(haml)

        result = result.rstrip('\n')  # ignore trailing new lines

        assert result == expected_html
Exemplo n.º 4
0
    def templatize(src, origin=None):
        # if the template has no origin then don't attempt to convert it because we don't know if it's Haml
        if origin:
            extension = os.path.splitext(origin.name)[1][1:].lower()

            if extension in HAML_EXTENSIONS:
                compiler = Compiler()
                src = compiler.process(src)

        return func(src, origin)
Exemplo n.º 5
0
    def templatize(src, origin=None):
        # if the template has no origin then don't attempt to convert it because we don't know if it's Haml
        if origin:
            extension = os.path.splitext(origin)[1][1:].lower()

            if extension in HAML_EXTENSIONS:
                compiler = Compiler()
                src = compiler.process(src)

        return func(src, origin=origin)
Exemplo n.º 6
0
    def _test(self, haml, expected_html, options=None):
        if not options:
            options = {'escape_attrs': True}

        compiler = Compiler(options)
        result = compiler.process(haml)

        result = result.rstrip('\n')  # ignore trailing new lines

        assert result == expected_html
Exemplo n.º 7
0
    def preprocess(self, source, name, filename=None):
        extension = os.path.splitext(name)[1][1:]

        if extension in HAML_EXTENSIONS:
            compiler = Compiler()
            try:
                return compiler.process(source)
            except ParseException as e:
                raise jinja2.TemplateSyntaxError(six.text_type(e), 1, name=name, filename=filename)
        else:
            return source
Exemplo n.º 8
0
        def get_contents(self, origin):
            # Django>=1.9
            contents = super(Loader, self).get_contents(origin)
            name, _extension = os.path.splitext(origin.template_name)
            # os.path.splitext always returns a period at the start of extension
            extension = _extension.lstrip('.')

            if extension in HAML_EXTENSIONS:
                compiler = Compiler(options=options)
                return compiler.process(contents)

            return contents
Exemplo n.º 9
0
        def get_contents(self, origin):
            # Django>=1.9
            contents = super(Loader, self).get_contents(origin)
            name, _extension = os.path.splitext(origin.template_name)
            # os.path.splitext always returns a period at the start of extension
            extension = _extension.lstrip(".")

            if extension in HAML_EXTENSIONS:
                compiler = Compiler(options=options)
                return compiler.process(contents)

            return contents
Exemplo n.º 10
0
    def preprocess(self, source, name, filename=None):
        extension = os.path.splitext(name)[1][1:]

        if extension in HAML_EXTENSIONS:
            compiler = Compiler()
            try:
                return compiler.process(source)
            except ParseException as e:
                raise jinja2.TemplateSyntaxError(str(e),
                                                 1,
                                                 name=name,
                                                 filename=filename)
        else:
            return source
Exemplo n.º 11
0
        def get_contents(self, origin):
            # Django>=1.9
            contents = super(Loader, self).get_contents(origin)

            print 'start load template: ' + origin.template_name
            name, _extension = os.path.splitext(
                origin.template_name
            )  # os.path.splitext always returns a period at the start of extension
            extension = _extension.lstrip('.')

            if extension in HAML_EXTENSIONS:
                compiler = Compiler(options=options)

                t = clock()

                if HAML_UNIT.ENABLE:

                    dtaml = HamlComponent(origin, contents)
                    print 'HamlComponent create: ' + str(clock() - t)
                    res_keeper = dtaml.package_ress()
                    print 'package_ress: ' + str(clock() - t)
                    contents = dtaml.embed_components()
                    print 'embed_components: ' + str(clock() - t)

                    print '++++++++++++++++++++++++++++++++++++++++++++++++ hamlpy components completed'


##              now contents is full. Prepare it:
                contents = clean_sugar(contents)

                ## save contents before compiler
                haml_file = str(origin).rsplit('.', 1)[0] + '__log' + '.haml'
                with open(haml_file, 'w') as pen:
                    pen.write(contents)

                ##                contents = contents.decode('utf-8') if type(contents) is str else contents
                html_content = compiler.process(contents)

                ##  save result after compiling
                html_file = str(origin).rsplit('.', 1)[0] + '.html'
                with open(html_file, 'w') as html:
                    html.write(html_content)

                return html_content

            return contents
Exemplo n.º 12
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 HAML_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 = Compiler()
                    return haml_parser.process(haml_source)

            raise TemplateDoesNotExist(origin.template_name)
Exemplo n.º 13
0
        def load_template_source(self, template_name, *args, **kwargs):
            # Django<1.9
            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 HAML_EXTENSIONS:
                try:
                    haml_source, template_path = super(Loader, self).load_template_source(
                        self._generate_template_name(name, extension), *args, **kwargs
                    )
                except TemplateDoesNotExist:  # pragma: no cover
                    pass
                else:
                    compiler = Compiler(options=options)
                    html = compiler.process(haml_source)

                    return html, template_path

            raise TemplateDoesNotExist(template_name)
Exemplo n.º 14
0
        def load_template_source(self, template_name, *args, **kwargs):
            # Django<1.9
            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 HAML_EXTENSIONS:
                try:
                    haml_source, template_path = super(Loader, self).load_template_source(
                        self._generate_template_name(name, extension), *args, **kwargs
                    )
                except TemplateDoesNotExist:  # pragma: no cover
                    pass
                else:
                    compiler = Compiler(options=options)
                    html = compiler.process(haml_source)

                    return html, template_path

            raise TemplateDoesNotExist(template_name)
Exemplo n.º 15
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 HAML_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().get_contents(try_origin)
                except TemplateDoesNotExist:
                    pass
                else:
                    haml_parser = Compiler()
                    return haml_parser.process(haml_source)

            raise TemplateDoesNotExist(origin.template_name)
Exemplo n.º 16
0
def compile_file(fullpath, outfile_name, compiler_args):
    """
    Calls HamlPy compiler. Returns True if the file was compiled and written successfully.
    """
    if Options.VERBOSE:
        print('%s %s -> %s' % (strftime("%H:%M:%S"), fullpath, outfile_name))
    try:
        if Options.DEBUG:  # pragma: no cover
            print("Compiling %s -> %s" % (fullpath, outfile_name))
        haml = codecs.open(fullpath, 'r', encoding='utf-8').read()
        compiler = Compiler(compiler_args)
        output = compiler.process(haml)
        outfile = codecs.open(outfile_name, 'w', encoding='utf-8')
        outfile.write(output)

        return True
    except Exception as e:
        # import traceback
        print("Failed to compile %s -> %s\nReason:\n%s" % (fullpath, outfile_name, e))
        # print traceback.print_exc()

    return False
Exemplo n.º 17
0
def compile_file(fullpath, outfile_name, compiler_args):
    """
    Calls HamlPy compiler. Returns True if the file was compiled and written successfully.
    """
    if Options.VERBOSE:
        print('%s %s -> %s' % (strftime("%H:%M:%S"), fullpath, outfile_name))
    try:
        if Options.DEBUG:  # pragma: no cover
            print("Compiling %s -> %s" % (fullpath, outfile_name))
        haml = codecs.open(fullpath, 'r', encoding='utf-8').read()
        compiler = Compiler(compiler_args)
        output = compiler.process(haml)
        outfile = codecs.open(outfile_name, 'w', encoding='utf-8')
        outfile.write(output)

        return True
    except Exception as e:
        # import traceback
        print("Failed to compile %s -> %s\nReason:\n%s" %
              (fullpath, outfile_name, e))
        # print traceback.print_exc()

    return False
Exemplo n.º 18
0
    def run(self):
        compiler = Compiler()
        parsed = compiler.process(self.haml)

        # ignore line ending differences
        self.actual_html = parsed.replace('\r', '')
Exemplo n.º 19
0
#! /usr/bin/env python3

from __future__ import print_function

import sys
from hamlpy.compiler import Compiler

with open(sys.argv[1]) as f:
    haml = f.read()

compiler = Compiler()
html = compiler.process(haml)

print(html)