Exemplo n.º 1
0
    def decodesto(self, input, expectation=None):
        if expectation == None:
            expectation = input

        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options),
                                  expectation)

        # if the expected is different from input, run it again
        # expected output should be unchanged when run twice.
        if not expectation != input:
            self.assertMultiLineEqual(
                cssbeautifier.beautify(expectation, self.options), expectation)

        # Everywhere we do newlines, they should be replaced with opts.eol
        self.options.eol = '\r\\n'
        expectation = expectation.replace('\n', '\r\n')
        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options),
                                  expectation)
        if input.find('\n') != -1:
            input = input.replace('\n', '\r\n')
            self.assertMultiLineEqual(
                cssbeautifier.beautify(input, self.options), expectation)
            # Ensure support for auto eol detection
            self.options.eol = 'auto'
            self.assertMultiLineEqual(
                cssbeautifier.beautify(input, self.options), expectation)
        self.options.eol = '\n'
Exemplo n.º 2
0
    def decodesto(self, input, expectation=None):
        if expectation == None:
            expectation = input

        self.assertMultiLineEqual(
            cssbeautifier.beautify(input, self.options), expectation)

        # if the expected is different from input, run it again
        # expected output should be unchanged when run twice.
        if not expectation != input:
            self.assertMultiLineEqual(
                cssbeautifier.beautify(expectation, self.options), expectation)

        # Everywhere we do newlines, they should be replaced with opts.eol
        self.options.eol = '\r\\n';
        expectation = expectation.replace('\n', '\r\n')
        self.assertMultiLineEqual(
            cssbeautifier.beautify(input, self.options), expectation)
        if input.find('\n') != -1:
            input = input.replace('\n', '\r\n')
            self.assertMultiLineEqual(
                cssbeautifier.beautify(input, self.options), expectation)
            # Ensure support for auto eol detection
            self.options.eol = 'auto'
            self.assertMultiLineEqual(
                cssbeautifier.beautify(input, self.options), expectation)
        self.options.eol = '\n'
Exemplo n.º 3
0
    def decodesto(self, input, expectation=None):
        if expectation == None:
            expectation = input

        self.assertMultiLineEqual(
            cssbeautifier.beautify(input, self.options), expectation)

        # if the expected is different from input, run it again
        # expected output should be unchanged when run twice.
        if not expectation == None:
            self.assertMultiLineEqual(
                cssbeautifier.beautify(expectation, self.options), expectation)
Exemplo n.º 4
0
    def read_testcase(self, testcase_path):
        self.testcase_path = testcase_path
        self._ext = testcase_path.rsplit(".", 1)[-1]

        if self.should_skip():
            return

        # Beautify testcase
        opts = (
            ('end_with_newline', False),
            ('indent_size', 2),
            ('newline_between_rules', False),
            ('preserve_newlines', False),
        )
        with open(testcase_path) as testcase_fp:
            self.original_testcase = testcase_fp.read()
        if self._ext == "css":
            # DDBEGIN and DDEND are ignored here atm
            LOG.info("Attempting to cssbeautify %s", testcase_path)
            with open(testcase_path, "w") as testcase_fp:
                testcase_fp.write(
                    cssbeautifier.beautify(self.original_testcase, opts))
        else:
            # handle html files
            begin = max(self.original_testcase.find("DDBEGIN"), 0)
            end = self.original_testcase.find("DDEND")
            if end == -1:
                end = len(self.original_testcase)
            re_tag = re.compile(r"(<style.*?>)(.*?)(</style>)",
                                flags=re.DOTALL | re.IGNORECASE)
            if not re_tag.search(self.original_testcase, begin, end):
                LOG.debug("<style> tags not found in %r", testcase_path)
                self._force_skip = True
                self.original_testcase = None
                return
            pos = 0
            with open(testcase_path, "w") as testcase_fp:
                for match in re_tag.finditer(self.original_testcase, begin,
                                             end):
                    testcase_fp.write(
                        self.original_testcase[pos:match.start(2)])
                    css = cssbeautifier.beautify(match.group(2), opts)
                    if css:
                        testcase_fp.write("\n")
                        testcase_fp.write(css)
                        testcase_fp.write("\n")
                    pos = match.end(2)
                testcase_fp.write(self.original_testcase[pos:])
            LOG.info("Ran cssbeautify on %s", testcase_path)

        self.lithium.strategy = self.strategy_type()  # pylint: disable=not-callable
        self.lithium.testcase = self.testcase_type()  # pylint: disable=not-callable
        self.lithium.testcase.readTestcase(testcase_path)
Exemplo n.º 5
0
    def decodesto(self, input, expectation=None):
        if expectation == None:
            expectation = input

        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options),
                                  expectation)

        # if the expected is different from input, run it again
        # expected output should be unchanged when run twice.
        if not expectation == None:
            self.assertMultiLineEqual(
                cssbeautifier.beautify(expectation, self.options), expectation)
Exemplo n.º 6
0
    def filter(self, data, subfilter):
        from bs4 import BeautifulSoup as bs
        soup = bs(data, features="lxml")

        try:
            import jsbeautifier
        except ImportError:
            logger.info(
                '"jsbeautifier" is not installed, will not beautify <script> tags'
            )
            jsbeautifier = None

        if jsbeautifier is not None:
            scripts = soup.find_all('script')
            for script in scripts:
                if script.string is not None:
                    beautified_js = jsbeautifier.beautify(script.string)
                    script.string = beautified_js

        try:
            import cssbeautifier
        except ImportError:
            logger.info(
                '"cssbeautifier" is not installed, will not beautify <style> tags'
            )
            cssbeautifier = None

        if cssbeautifier is not None:
            styles = soup.find_all('style')
            for style in styles:
                if style.string is not None:
                    beautified_css = cssbeautifier.beautify(style.string)
                    style.string = beautified_css

        return soup.prettify()
Exemplo n.º 7
0
    def filter(self, data, subfilter):
        if BeautifulSoup is None:
            raise ImportError('Please install BeautifulSoup')

        soup = BeautifulSoup(data, features="lxml")

        if jsbeautifier is not None:
            scripts = soup.find_all('script')
            for script in scripts:
                if script.string is not None:
                    beautified_js = jsbeautifier.beautify(script.string)
                    script.string = beautified_js
        else:
            logger.info(
                '"jsbeautifier" is not installed, will not beautify <script> tags'
            )

        if cssbeautifier is not None:
            styles = soup.find_all('style')
            for style in styles:
                if style.string is not None:
                    beautified_css = cssbeautifier.beautify(style.string)
                    style.string = beautified_css
        else:
            logger.info(
                '"cssbeautifier" is not installed, will not beautify <style> tags'
            )

        return soup.prettify()
Exemplo n.º 8
0
    def format(self, text):

        text = text.decode('utf-8')
        stderr = ''
        stdout = ''
        options = cssbeautifier.default_options()

        if ('indent_size' in self.opts and self.opts['indent_size']):
            options.indent_size = self.opts['indent_size']
        else:
            options.indent_size = 4

        if ('indent_char' in self.opts and self.opts['indent_char']):
            options.indent_char = self.opts['indent_char']
        else:
            options.indent_char = ' '

        if ('indent_with_tabs' in self.opts and self.opts['indent_with_tabs']):
            options.indent_with_tabs = True
        else:
            options.indent_with_tabs = False

        if ('selector_separator_newline' in self.opts
                and self.opts['selector_separator_newline']):
            options.selector_separator_newline = True
        else:
            options.selector_separator_newline = False

        if ('end_with_newline' in self.opts and self.opts['end_with_newline']):
            options.end_with_newline = True
        else:
            options.end_with_newline = False

        if ('eol' in self.opts and self.opts['eol']):
            options.eol = self.opts['eol']
        else:
            options.eol = '\n'

        if ('space_around_combinator' in self.opts
                and self.opts['space_around_combinator']):
            options.space_around_combinator = True
        else:
            options.space_around_combinator = False

        if ('newline_between_rules' in self.opts
                and self.opts['newline_between_rules']):
            options.newline_between_rules = True
        else:
            options.newline_between_rules = False

        try:
            stdout = cssbeautifier.beautify(text, options)
        except Exception as e:
            stderr = str(e)

        if (not stderr and not stdout):
            stderr = 'Formatting error!'

        return stdout, stderr
Exemplo n.º 9
0
Arquivo: test.py Projeto: hobinjk/gaia
    def decodesto(self, input, expectation=None):
        if expectation == None:
            expectation = input

        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options), expectation)

        # if the expected is different from input, run it again
        # expected output should be unchanged when run twice.
        if not expectation != input:
            self.assertMultiLineEqual(cssbeautifier.beautify(expectation, self.options), expectation)

        # Everywhere we do newlines, they should be replaced with opts.eol
        self.options.eol = "\r\\n"
        expectation = expectation.replace("\n", "\r\n")
        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options), expectation)
        input = input.replace("\n", "\r\n")
        self.assertMultiLineEqual(cssbeautifier.beautify(input, self.options), expectation)
        self.options.eol = "\n"
Exemplo n.º 10
0
    def beautify_bytes(cls, data):
        """Perform CSS beautification on a code buffer.

        Arguments:
            data (bytes): The code data to be beautified.

        Returns:
            bytes: The beautified result.
        """
        assert cls.import_available
        data = data.decode("utf-8", errors="surrogateescape")
        return cssbeautifier.beautify(data, cls.opts).encode(
            "utf-8", errors="surrogateescape")
    def format(self, text):
        text = text.decode("utf-8")
        opts = self.formatter.settings.get('codeformatter_css_options')


        stderr = ""
        stdout = ""
        options = cssbeautifier.default_options()

        if ("indent_size" in opts and opts["indent_size"]):
            options.indent_size = opts["indent_size"]
        else:
            options.indent_size = 4

        if ("indent_char" in opts and opts["indent_char"]):
            options.indent_char = opts["indent_char"]
        else:
            options.indent_char = ' '

        if ("indent_with_tabs" in opts and opts["indent_with_tabs"]):
            options.indent_with_tabs = True
        else:
            options.indent_with_tabs = False


        if ("selector_separator_newline" in opts and opts["selector_separator_newline"]):
            options.selector_separator_newline = True
        else:
            options.selector_separator_newline = False

        if ("end_with_newline" in opts and opts["end_with_newline"]):
            options.end_with_newline = True
        else:
            options.end_with_newline = False

        if ("eol" in opts and opts["eol"]):
            options.eol = opts["eol"]
        else:
            options.eol = "\n"


        try:
              stdout = cssbeautifier.beautify(text, options)
        except Exception as e:
             stderr = str(e)

        if (not stderr and not stdout):
            stderr = "Formatting error!"

        return stdout, stderr
 def _get_css_input(self, data):
     start_head = data.find("<head>")
     end_head = data.find("</head>")
     start_style_tag = data.find("<style")
     if start_style_tag < 0 or start_head < 0 or start_head > end_head:
         return ""
     if len(data) == start_style_tag + 6:
         return ""
     end_style_tag = data.find(">", start_style_tag)
     end_style = data.find("</style>")
     if (start_head > start_style_tag or end_head < end_style or
             end_style_tag > end_style):
         return ""
     return CSSBeautifier.beautify(data[end_style_tag + 1: end_style])
    def format(self, text):

        text = text.decode('utf-8')
        stderr = ''
        stdout = ''

        try:
            stdout = cssbeautifier.beautify(text, self.options)
        except Exception as e:
            stderr = str(e)

        if (not stderr and not stdout):
            stderr = 'Formatting error!'

        return stdout, stderr
    def format(self, text):

        text = text.decode('utf-8')
        stderr = ''
        stdout = ''

        try:
            stdout = cssbeautifier.beautify(text, self.options)
        except Exception as e:
            stderr = str(e)

        if (not stderr and not stdout):
            stderr = 'Formatting error!'

        return stdout, stderr
Exemplo n.º 15
0
    def format(self, text):
        text = text.decode("utf-8")

        stderr = ""
        stdout = ""
        options = cssbeautifier.default_options()

        if ("indent_size" in self.opts and self.opts["indent_size"]):
            options.indent_size = self.opts["indent_size"]
        else:
            options.indent_size = 4

        if ("indent_char" in self.opts and self.opts["indent_char"]):
            options.indent_char = self.opts["indent_char"]
        else:
            options.indent_char = ' '

        if ("indent_with_tabs" in self.opts and self.opts["indent_with_tabs"]):
            options.indent_with_tabs = True
        else:
            options.indent_with_tabs = False


        if ("selector_separator_newline" in self.opts and self.opts["selector_separator_newline"]):
            options.selector_separator_newline = True
        else:
            options.selector_separator_newline = False

        if ("end_with_newline" in self.opts and self.opts["end_with_newline"]):
            options.end_with_newline = True
        else:
            options.end_with_newline = False

        if ("eol" in self.opts and self.opts["eol"]):
            options.eol = self.opts["eol"]
        else:
            options.eol = "\n"


        try:
              stdout = cssbeautifier.beautify(text, options)
        except Exception as e:
             stderr = str(e)

        if (not stderr and not stdout):
            stderr = "Formatting error!"

        return stdout, stderr
Exemplo n.º 16
0
 def filter(self, data, subfilter=None):
     import jsbeautifier
     import cssbeautifier
     from bs4 import BeautifulSoup as bs
     soup = bs(data, features="lxml")
     scripts = soup.find_all('script')
     for script in scripts:
         if script.string is not None:
             beautified_js = jsbeautifier.beautify(script.string)
             script.string = beautified_js
     styles = soup.find_all('style')
     for style in styles:
         if style.string is not None:
             beautified_css = cssbeautifier.beautify(style.string)
             style.string = beautified_css
     return soup.prettify()
Exemplo n.º 17
0
 def build_page(self, endpoints, path):
     curr_dir = path
     try:
         os.mkdir(curr_dir)
     except:
         pass
     opts = jsbeautifier.default_options()
     opts.indent_size = 2
     opts.max_preserve_newlines = 30
     # Save HTML file
     with open(f"{curr_dir}/{self.name}.page.html", 'w') as html_file:
         html_file.write(self.html)
     # Save SCSS file
     with open(f"{curr_dir}/{self.name}.page.scss", 'w') as scss_file:
         scss_file.write(cssbeautifier.beautify(self.scss, opts))
     # Save TypeScript file
     with open(f"{curr_dir}/{self.name}.page.ts", 'w') as ts_file:
         ts_file.write(jsbeautifier.beautify(self.build_ts(endpoints),
                                             opts))
Exemplo n.º 18
0
def beautifier_test_github_css():
    cssbeautifier.beautify(data, options)
Exemplo n.º 19
0
 def decodesto(self, input, expectation=None):
     self.assertEqual(
         cssbeautifier.beautify(input, self.options), expectation or input)
Exemplo n.º 20
0
 def css(cls, unformatted: str) -> str:
     formatted = cssbeautifier.beautify(
         unformatted,
         opts=cls.cssbeautifier_opts,
     )
     return formatted
def beautifier_test_github_css():
    cssbeautifier.beautify(data, options)
Exemplo n.º 22
0
 def beautify(self, code):
     if self.type is "js":
         return jsbeautifier.beautify(code, self.options)
     if self.type is "css":
         return cssbeautifier.beautify(code, self.options)
Exemplo n.º 23
0
def format_css(unformatted: str, _info_str: str) -> str:
    return cssbeautifier.beautify(unformatted) + "\n"