mirror of
https://github.com/beautifier/js-beautify.git
synced 2024-11-23 20:59:43 +00:00
Merge branch 'master' into master
This commit is contained in:
commit
cfafd3dff1
@ -555,6 +555,7 @@ function Beautifier(source_text, options) {
|
||||
print_string(eatString(')'));
|
||||
} else {
|
||||
pos--;
|
||||
parenLevel++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -416,6 +416,7 @@ function Beautifier(source_text, options) {
|
||||
print_string(eatString(')'));
|
||||
} else {
|
||||
pos--;
|
||||
parenLevel++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -1300,6 +1300,22 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
|
||||
'}');
|
||||
|
||||
|
||||
//============================================================
|
||||
// Handle LESS function parameters
|
||||
reset_options();
|
||||
t(
|
||||
'div{.px2rem(width,12);}',
|
||||
// -- output --
|
||||
'div {\n' +
|
||||
'\t.px2rem(width, 12);\n' +
|
||||
'}');
|
||||
t(
|
||||
'div {\n' +
|
||||
'\tbackground: url("//test.com/dummy.png");\n' +
|
||||
'\t.px2rem(width, 12);\n' +
|
||||
'}');
|
||||
|
||||
|
||||
//============================================================
|
||||
// Psuedo-classes vs Variables
|
||||
reset_options();
|
||||
@ -1519,6 +1535,11 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
|
||||
t("@myvar:10px;.tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
t("@myvar:10px; .tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
|
||||
//mixins
|
||||
t("div{.px2rem(width,12);}", "div {\n\t.px2rem(width, 12);\n}");
|
||||
// mixin next to 'background: url("...")' should not add a line break after the comma
|
||||
t("div {\n\tbackground: url(\"//test.com/dummy.png\");\n\t.px2rem(width, 12);\n}");
|
||||
|
||||
// test options
|
||||
opts.indent_size = 2;
|
||||
opts.indent_char = ' ';
|
||||
|
8
python/css-beautify
Executable file
8
python/css-beautify
Executable file
@ -0,0 +1,8 @@
|
||||
#! /usr/bin/env python
|
||||
#
|
||||
# Stub script to run cssbeautifier
|
||||
#
|
||||
import sys
|
||||
from cssbeautifier import main
|
||||
sys.exit(main())
|
||||
|
@ -25,9 +25,13 @@
|
||||
|
||||
from __future__ import print_function
|
||||
import sys
|
||||
import os
|
||||
import io
|
||||
import re
|
||||
import copy
|
||||
import getopt
|
||||
from jsbeautifier.__version__ import __version__
|
||||
from jsbeautifier import isFileDifferent, mkdir_p
|
||||
from cssbeautifier.css.options import BeautifierOptions
|
||||
from cssbeautifier.css.beautifier import Beautifier
|
||||
|
||||
@ -43,9 +47,18 @@ def beautify(string, opts=default_options()):
|
||||
|
||||
def beautify_file(file_name, opts=default_options()):
|
||||
if file_name == '-': # stdin
|
||||
stream = sys.stdin
|
||||
try:
|
||||
if sys.stdin.isatty():
|
||||
raise Exception()
|
||||
|
||||
stream = sys.stdin
|
||||
except Exception as ex:
|
||||
print("Must pipe input or define input file.\n", file=sys.stderr)
|
||||
usage(sys.stderr)
|
||||
raise Exception()
|
||||
else:
|
||||
stream = open(file_name)
|
||||
|
||||
content = ''.join(stream.readlines())
|
||||
b = Beautifier(content, opts)
|
||||
return b.beautify()
|
||||
@ -57,8 +70,134 @@ def usage(stream=sys.stdout):
|
||||
|
||||
CSS beautifier (http://jsbeautifier.org/)
|
||||
|
||||
Usage: cssbeautifier.py [options] <infile>
|
||||
|
||||
<infile> can be "-", which means stdin.
|
||||
|
||||
Input options:
|
||||
|
||||
-i, --stdin Read input from stdin
|
||||
|
||||
Output options:
|
||||
|
||||
-s, --indent-size=NUMBER Indentation size. (default 4).
|
||||
-c, --indent-char=CHAR Character to indent with. (default space).
|
||||
-e, --eol=STRING Character(s) to use as line terminators.
|
||||
(default first newline in file, otherwise "\\n")
|
||||
-t, --indent-with-tabs Indent with tabs, overrides -s and -c
|
||||
--preserve-newlines Preserve existing line breaks.
|
||||
--disable-selector-separator-newline
|
||||
Do not print each selector on a separate line.
|
||||
-n, --end-with-newline End output with newline
|
||||
--disable-newline-between-rules
|
||||
Do not print empty line between rules.
|
||||
--space-around-combinator Print spaces around combinator.
|
||||
-r, --replace Write output in-place, replacing input
|
||||
-o, --outfile=FILE Specify a file to output to (default stdout)
|
||||
|
||||
Rarely needed options:
|
||||
|
||||
-h, --help, --usage Prints this help statement.
|
||||
-v, --version Show the version
|
||||
|
||||
""", file=stream)
|
||||
if stream == sys.stderr:
|
||||
return 1
|
||||
else:
|
||||
return 0
|
||||
|
||||
def main():
|
||||
|
||||
argv = sys.argv[1:]
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(argv, "hvio:rs:c:e:tn",
|
||||
['help', 'usage', 'version', 'stdin', 'outfile=', 'replace',
|
||||
'indent-size=', 'indent-char=', 'eol=', 'indent-with-tabs',
|
||||
'preserve-newlines', 'disable-selector-separator-newline',
|
||||
'end-with-newline', 'disable-newline-between-rules',
|
||||
'space-around-combinator'])
|
||||
except getopt.GetoptError as ex:
|
||||
print(ex, file=sys.stderr)
|
||||
return usage(sys.stderr)
|
||||
|
||||
css_options = default_options()
|
||||
|
||||
file = None
|
||||
outfile = 'stdout'
|
||||
replace = False
|
||||
if len(args) == 1:
|
||||
file = args[0]
|
||||
|
||||
for opt, arg in opts:
|
||||
if opt in ('--stdin', '-i'):
|
||||
file = '-'
|
||||
elif opt in ('--outfile', '-o'):
|
||||
outfile = arg
|
||||
elif opt in ('--replace', '-r'):
|
||||
replace = True
|
||||
elif opt in ('--version', '-v'):
|
||||
return print(__version__)
|
||||
elif opt in ('--help', '--usage', '-h'):
|
||||
return usage()
|
||||
|
||||
elif opt in ('--indent-size', '-s'):
|
||||
css_options.indent_size = int(arg)
|
||||
elif opt in ('--indent-char', '-c'):
|
||||
css_options.indent_char = arg
|
||||
elif opt in ('--eol', '-e'):
|
||||
css_options.eol = arg
|
||||
elif opt in ('--indent-with-tabs', '-t'):
|
||||
css_options.indent_with_tabs = True
|
||||
elif opt in ('--preserve-newlines'):
|
||||
css_options.preserve_newlines = True
|
||||
elif opt in ('--disable-selector-separator-newline'):
|
||||
css_options.selector_separator_newline = False
|
||||
elif opt in ('--end-with-newline', '-n'):
|
||||
css_options.end_with_newline = True
|
||||
elif opt in ('--disable-newline-between-rules'):
|
||||
css_options.newline_between_rules = False
|
||||
elif opt in ('--space-around-combinator'):
|
||||
css_options.space_around_combinator = True
|
||||
|
||||
|
||||
if not file:
|
||||
file = '-'
|
||||
|
||||
try:
|
||||
if outfile == 'stdout' and replace and not file == '-':
|
||||
outfile = file
|
||||
|
||||
pretty = beautify_file(file, css_options)
|
||||
|
||||
if outfile == 'stdout':
|
||||
# python automatically converts newlines in text to "\r\n" when on windows
|
||||
# switch to binary to prevent this
|
||||
if sys.platform == "win32":
|
||||
import msvcrt
|
||||
msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
|
||||
|
||||
sys.stdout.write(pretty)
|
||||
else:
|
||||
if isFileDifferent(outfile, pretty):
|
||||
mkdir_p(os.path.dirname(outfile))
|
||||
|
||||
# python automatically converts newlines in text to "\r\n" when on windows
|
||||
# set newline to empty to prevent this
|
||||
with io.open(outfile, 'wt', newline='') as f:
|
||||
print('writing ' + outfile, file=sys.stderr)
|
||||
try:
|
||||
f.write(pretty)
|
||||
except TypeError:
|
||||
# This is not pretty, but given how we did the version import
|
||||
# it is the only way to do this without having setup.py fail on a missing six dependency.
|
||||
six = __import__("six")
|
||||
f.write(six.u(pretty))
|
||||
|
||||
|
||||
except Exception as ex:
|
||||
print(ex, file=sys.stderr)
|
||||
return 1
|
||||
|
||||
# Success
|
||||
return 0
|
||||
|
@ -425,7 +425,8 @@ class Beautifier:
|
||||
and self.ch is not '\'':
|
||||
printer.print_string(self.eatString(')'))
|
||||
else:
|
||||
self.pos = self.pos - 1
|
||||
self.pos -= 1
|
||||
parenLevel += 1
|
||||
else:
|
||||
parenLevel += 1
|
||||
printer.preserveSingleSpace(isAfterSpace)
|
||||
|
@ -1258,6 +1258,22 @@ class CSSBeautifierTest(unittest.TestCase):
|
||||
'}')
|
||||
|
||||
|
||||
#============================================================
|
||||
# Handle LESS function parameters
|
||||
self.reset_options();
|
||||
t(
|
||||
'div{.px2rem(width,12);}',
|
||||
# -- output --
|
||||
'div {\n' +
|
||||
'\t.px2rem(width, 12);\n' +
|
||||
'}')
|
||||
t(
|
||||
'div {\n' +
|
||||
'\tbackground: url("//test.com/dummy.png");\n' +
|
||||
'\t.px2rem(width, 12);\n' +
|
||||
'}')
|
||||
|
||||
|
||||
#============================================================
|
||||
# Psuedo-classes vs Variables
|
||||
self.reset_options();
|
||||
|
@ -238,6 +238,11 @@ function run_css_tests(test_obj, Urlencoded, js_beautify, html_beautify, css_bea
|
||||
t("@myvar:10px;.tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
t("@myvar:10px; .tabs{width:10px;}", "@myvar: 10px;\n.tabs {\n\twidth: 10px;\n}");
|
||||
|
||||
//mixins
|
||||
t("div{.px2rem(width,12);}", "div {\n\t.px2rem(width, 12);\n}");
|
||||
// mixin next to 'background: url("...")' should not add a line break after the comma
|
||||
t("div {\n\tbackground: url(\"//test.com/dummy.png\");\n\t.px2rem(width, 12);\n}");
|
||||
|
||||
// test options
|
||||
opts.indent_size = 2;
|
||||
opts.indent_char = ' ';
|
||||
|
@ -388,6 +388,14 @@ exports.test_data = {
|
||||
output: '.generate-columns(@n, @i: 1) when (@i =< @n) {\n\t.column-@{i} {\n\t\twidth: (@i * 100% / @n);\n\t}\n\t.generate-columns(@n, (@i + 1));\n}'
|
||||
}
|
||||
],
|
||||
}, {
|
||||
name: "Handle LESS function parameters",
|
||||
description: "",
|
||||
tests: [
|
||||
{ input: 'div{.px2rem(width,12);}', output: 'div {\n\t.px2rem(width, 12);\n}' },
|
||||
//mixin next to 'background: url("...")' should not add a linebreak after the comma
|
||||
{ unchanged: 'div {\n\tbackground: url("//test.com/dummy.png");\n\t.px2rem(width, 12);\n}' }
|
||||
],
|
||||
}, {
|
||||
name: "Psuedo-classes vs Variables",
|
||||
description: "",
|
||||
|
Loading…
Reference in New Issue
Block a user