def __init__(self, formatter): self.formatter = formatter self.options = cssbeautifier.default_options() # parse custom options from settings file self.fill_custom_options( formatter.settings.get('codeformatter_css_options'))
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
def resetOptions(self): false = False true = True self.options = cssbeautifier.default_options() self.options.indent_size = 1 self.options.indent_char = '\t' self.options.selector_separator_newline = true self.options.end_with_newline = false
def resetOptions(self): false = False true = True self.options = cssbeautifier.default_options() self.options.indent_size = 1 self.options.indent_char = '\t' self.options.selector_separator_newline = true self.options.end_with_newline = false self.options.newline_between_rules = false self.options.consistent_comment_rule = false
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 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
def __init__(self, settings, type, view): self.settings = settings self.type = type self.view = view if self.type is "js": self.options = jsbeautifier.default_options() if self.type is "css": self.options = cssbeautifier.default_options() self.augment_options(settings) view_settings = self.view.settings() if view_settings.get("translate_tabs_to_spaces"): self.options.indent_char = "\t" if self.options.indent_char is " ": self.options.indent_size = int(view_settings.get("tab_size")) else: self.options.indent_size = 1
def setUpClass(cls): false = False true = True default_options = cssbeautifier.default_options() default_options.indent_size = 1 default_options.indent_char = '\t' default_options.selector_separator_newline = true default_options.end_with_newline = false default_options.newline_between_rules = false default_options.indent_size = 1 default_options.indent_char = '\t' default_options.selector_separator_newline = true default_options.end_with_newline = false default_options.newline_between_rules = false cls.default_options = default_options
#!/usr/bin/env python # -*- coding: utf-8 -*- import io import os import copy import cssbeautifier options = cssbeautifier.default_options() options.wrap_line_length = 80 data = "" def beautifier_test_github_css(): cssbeautifier.beautify(data, options) def report_perf(fn): import timeit iter = 5 time = timeit.timeit(fn + "()", setup="from __main__ import " + fn + "; gc.enable()", number=iter) print(fn + ": " + str(iter / time) + " cycles/sec") if __name__ == "__main__": dirname = os.path.dirname(os.path.abspath(__file__)) github_file = os.path.join(dirname, "../", "test/resources/github.css") data = copy.copy("".join(io.open(github_file).readlines()))
class CodeFormatter: class Mode(enum.Enum): C = enum.auto() CPP = enum.auto() CPP_CLI = enum.auto() OBJECTIVE_C = enum.auto() CSHARP = enum.auto() JAVA = enum.auto() # astyle configuration astyle_opts_default = { 'kwargs': { 'mode': 'c', 'style': 'java', 'indent': 'spaces=4', 'lineend': 'linux', }, 'args': { 'add-braces': True, 'attach-inlines': True, 'attach-classes': True, 'attach-namespaces': True, 'break-closing-braces': True, 'break-one-line-headers': True, 'convert-tabs': True, 'indent-col1-comments': True, 'indent-switches': True, 'pad-oper': True, }, } astyle_opts = { Mode.C: { 'kwargs': { 'mode': 'c', }, 'args': {}, }, Mode.CPP: { 'kwargs': { 'mode': 'c', }, 'args': {}, }, Mode.CPP_CLI: { 'kwargs': { 'mode': 'c', }, 'args': {}, }, Mode.OBJECTIVE_C: { 'kwargs': { 'mode': 'c', }, 'args': {}, }, Mode.CSHARP: { 'kwargs': { 'mode': 'cs', }, 'args': {}, }, Mode.JAVA: { 'kwargs': { 'mode': 'java', }, 'args': {}, }, } # python configuration autopep8_opts = { 'max_line_length': 100, 'aggressive': 3, } # javascript options jsbeautifier_opts = jsbeautifier.default_options() jsbeautifier_opts.end_with_newline = False jsbeautifier_opts.indent_size = 4 # css options cssbeautifier_opts = cssbeautifier.default_options() cssbeautifier_opts.end_with_newline = False cssbeautifier_opts.indent_size = 2 # xml options xml_features = 'lxml' # or alternately 'html5lib' @classmethod def astyle( cls, unformatted: str, mode: Mode, ) -> str: command = ['astyle'] # create options dict opts = copy.deepcopy(cls.astyle_opts_default) for key, val in cls.astyle_opts[mode].items(): opts[key].update(val) # add kwargs to command for key, val in opts['kwargs'].items(): kwarg = f'--{key}={val}' command.append(kwarg) # add args to command for key, val in opts['args'].items(): if val: key = f'--{key}' command.append(key) # run the command ret = subprocess.run(command, input=unformatted, encoding='utf-8', capture_output=True) formatted = ret.stdout return formatted @classmethod def c(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.C formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def cpp(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.CPP formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def cpp_cli(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.CPP_CLI formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def objective_c(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.OBJECTIVE_C formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def cs(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.CSHARP formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def java(cls, unformatted: str) -> str: mode = CodeFormatter.Mode.JAVA formatted = cls.astyle(unformatted=unformatted, mode=mode) return formatted @classmethod def xml(cls, unformatted: str) -> str: soup = bs4.BeautifulSoup( unformatted, features=cls.xml_features, ) formatted = soup.prettify() return formatted @classmethod def python(cls, unformatted: str) -> str: formatted = autopep8.fix_code( unformatted, options=cls.autopep8_opts, ) return formatted @classmethod def javascript(cls, unformatted: str) -> str: formatted = jsbeautifier.beautify( unformatted, opts=cls.jsbeautifier_opts, ) return formatted @classmethod def css(cls, unformatted: str) -> str: formatted = cssbeautifier.beautify( unformatted, opts=cls.cssbeautifier_opts, ) return formatted
#!/usr/bin/env python # -*- coding: utf-8 -*- import os import copy import cssbeautifier options = cssbeautifier.default_options() options.wrap_line_length = 80 data = '' def beautifier_test_github_css(): cssbeautifier.beautify(data, options) def report_perf(fn): import timeit iter = 5 time = timeit.timeit( fn + "()", setup="from __main__ import " + fn + "; gc.enable()", number=iter) print(fn + ": " + str(iter / time) + " cycles/sec") if __name__ == '__main__': dirname = os.path.dirname(os.path.abspath(__file__)) github_file = os.path.join(
def __init__(self, formatter): self.formatter = formatter self.options = cssbeautifier.default_options() # parse custom options from settings file self.fill_custom_options(formatter.settings.get('codeformatter_css_options'))