//author: 沙加 darkangle.cnblogs.com QQ 5364241
//初始化选项
var indent_size = 1; //缩进空格数, 为1时使用制表符缩进
var indent_char = ' '; //缩进字符
var preserve_newlines = false; //是否保留空行, 默认不保留
//程序开始
var input = "";
while (!WScript.StdIn.AtEndOfStream) {
input += WScript.StdIn.ReadAll();
}
if (indent_size == 1) {
indent_char = '\t';
}
var js_source = input.replace(/^\s+/, '');
var formated_code = '';
if (js_source && js_source.substr(0,1) !== '<') {
formated_code = js_beautify(js_source, {
indent_size: indent_size,
indent_char: indent_char,
preserve_newlines: preserve_newlines
});
}
formated_code.length?WScript.Echo(formated_code):WScript.Echo('Are you sure your input is javascript source file?');
/*
JS Beautifier
---------------
$Date$
$Revision$
Written by Einars Lielmanis, <einars@gmail.com>
http://elfz.laacz.lv/beautify/
Originally converted to javascript by Vital, <vital76@gmail.com>
http://my.opera.com/Vital/blog/2007/11/21/javascript-beautify-on-javascript-translated
You are free to use this in any way you want, in case you find this useful or working for you.
Usage:
js_beautify(js_source_text);
js_beautify(js_source_text, options);
The options are:
indent_size (default 4) — indentation size,
indent_char (default space) — character to indent with,
preserve_newlines (default true) — whether existing line breaks should be preserved,
indent_level (default 0) — initial indentation level, you probably won't need this ever,
e.g
js_beautify(js_source_text, {indent_size: 1, indent_char: '\t'});
*/
function js_beautify(js_source_text, options) {
var input, output, token_text, last_type, last_text, last_word, current_mode, modes, indent_string;
var whitespace, wordchar, punct, parser_pos, line_starters, in_case;
var prefix, token_type, do_block_just_closed, var_line, var_line_tainted, if_line_flag;
var indent_level;
var options = options || {};
var opt_indent_size = options['indent_size'] || 4;
var opt_indent_char = options['indent_char'] || ' ';
var opt_preserve_newlines = typeof options['preserve_newlines'] === 'undefined' ? true: options['preserve_newlines'];
var opt_indent_level = options['indent_level'] || 0; // starting indentation
function trim_output() {
while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
output.pop();
}
}
function print_newline(ignore_repeated) {
ignore_repeated = typeof ignore_repeated === 'undefined' ? true: ignore_repeated;
if_line_flag = false;
trim_output();
if (!output.length) {
return; // no newline on start of file
}
if (output[output.length - 1] !== "\n" || !ignore_repeated) {
output.push("\n");
}
for (var i = 0; i < indent_level; i++) {
output.push(indent_string);
}
}
function print_space() {
var last_output = output.length ? output[output.length - 1] : ' ';
if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
output.push(' ');
}
}
function print_token() {
output.push(token_text);
}
function indent() {
indent_level++;
}
function unindent() {
if (indent_level) {
indent_level--;
}
}
function remove_indent() {
if (output.length && output[output.length - 1] === indent_string) {
output.pop();
}
}
function set_mode(mode) {
modes.push(current_mode);
current_mode = mode;
}
function restore_mode() {
do_block_just_closed = current_mode === 'DO_BLOCK';
current_mode = modes.pop();
}
function in_array(what, arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] === what) {
return true;
}
}
return false;
}
function get_next_token() {
var n_newlines = 0;
var c = '';
do {
if (parser_pos >= input.length) {
return ['', 'TK_EOF'];
}
c = input.charAt(parser_pos);
parser_pos += 1;
if (c === "\n") {
n_newlines += 1;
}
} while ( in_array ( c , whitespace ));
var wanted_newline = false;
if (opt_preserve_newlines) {
if (n_newlines > 1) {
for (var i = 0; i < 2; i++) {
print_newline(i === 0);
}
}
wanted_newline = (n_newlines === 1);
}
if (in_array(c, wordchar)) {
if (parser_pos < input.length) {
while (in_array(input.charAt(parser_pos), wordchar)) {
c += input.charAt(parser_pos);
parser_pos += 1;
if (parser_pos === input.length) {
break;
}
}
} // small and surprisingly unugly hack for 1E-10 representation
if (parser_pos !== input.length && c.match(/^[0-9]+[Ee]$/) && input.charAt(parser_pos) === '-') {
parser_pos += 1;
var t = get_next_token(parser_pos);
c += '-' + t[0];
return [c, 'TK_WORD'];
}
if (c === 'in') { // hack for 'in' operator
return [c, 'TK_OPERATOR'];
}
if (wanted_newline && last_type !== 'TK_OPERATOR' && !if_line_flag) {
print_newline();
}
return [c, 'TK_WORD'];
}
if (c === '(' || c === '[') {
return [c, 'TK_START_EXPR'];
}
if (c === ')' || c === ']') {
return [c, 'TK_END_EXPR'];
}
if (c === '{') {
return [c, 'TK_START_BLOCK'];
}
if (c === '}') {
return [c, 'TK_END_BLOCK'];
}
if (c === ';') {
return [c, 'TK_SEMICOLON'];
}
if (c === '/') {
var comment = ''; // peek for comment /* ... */
if (input.charAt(parser_pos) === '*') {
parser_pos += 1;
if (parser_pos < input.length) {
while (! (input.charAt(parser_pos) === '*' && input.charAt(parser_pos + 1) && input.charAt(parser_pos + 1) === '/') && parser_pos < input.length) {
comment += input.charAt(parser_pos);
parser_pos += 1;
if (parser_pos >= input.length) {
break;
}
}
}
parser_pos += 2;
return ['/*' + comment + '*/', 'TK_BLOCK_COMMENT'];
} // peek for comment // ...
if (input.charAt(parser_pos) === '/') {
comment = c;
while (input.charAt(parser_pos) !== "\x0d" && input.charAt(parser_pos) !== "\x0a") {
comment += input.charAt(parser_pos);
parser_pos += 1;
if (parser_pos >= input.length) {
break;
}
}
parser_pos += 1;
if (wanted_newline) {
print_newline();
}
return [comment, 'TK_COMMENT'];
}
}
if (c === "'" || // string
c === '"' || // string
(c === '/' && ((last_type === 'TK_WORD' && last_text === 'return') || (last_type === 'TK_START_EXPR' || last_type === 'TK_END_BLOCK' || last_type === 'TK_OPERATOR' || last_type === 'TK_EOF' || last_type === 'TK_SEMICOLON')))) { // regexp
var sep = c;
var esc = false;
var resulting_string = '';
if (parser_pos < input.length) {
while (esc || input.charAt(parser_pos) !== sep) {
resulting_string += input.charAt(parser_pos);
if (!esc) {
esc = input.charAt(parser_pos) === '\\';
} else {
esc = false;
}
parser_pos += 1;
if (parser_pos >= input.length) {
break;
}
}
}
parser_pos += 1;
resulting_string = sep + resulting_string + sep;
if (sep == '/') { // regexps may have modifiers /regexp/MOD , so fetch those, too
while (parser_pos < input.length && in_array(input.charAt(parser_pos), wordchar)) {
resulting_string += input.charAt(parser_pos);
parser_pos += 1;
}
}
return [resulting_string, 'TK_STRING'];
}
if (in_array(c, punct)) {
while (parser_pos < input.length && in_array(c + input.charAt(parser_pos), punct)) {
c += input.charAt(parser_pos);
parser_pos += 1;
if (parser_pos >= input.length) {
break;
}
}
return [c, 'TK_OPERATOR'];
}
return [c, 'TK_UNKNOWN'];
} //----------------------------------
indent_string = '';
while (opt_indent_size--) {
- 1
- 2
前往页