//程序开始
var __input_json = "";
while (!WScript.StdIn.AtEndOfStream) {
__input_json += WScript.StdIn.ReadAll();
}
var format_str = '';
if (__input_json) {
//if(WScript.Arguments.length)
var _json=__input_json.replace(/^\s+/, '');
format_str = js_beautify(_json);
}
format_str.length?WScript.Echo(format_str):WScript.Echo('Are you sure your input is javascript source file?');
// 处理json
function js_beautify(js_source_text) {
var input, output, token_text, last_type, last_text, last_last_text, last_word, flags, flag_store, indent_string;
var whitespace, wordchar, punct, parser_pos, line_starters, digits;
var prefix, token_type, do_block_just_closed;
var wanted_newline, just_added_newline, n_newlines;
// Some interpreters have unexpected results with foo = baz || bar;
//options = options ? options : {};
// 大括号另起一行 false:不换行; true 换行
//var opt_braces_on_own_line = options.braces_on_own_line ? options.braces_on_own_line : true;
var opt_braces_on_own_line =false;
//缩进
//var opt_indent_size = options.indent_size ? options.indent_size : 4;
var opt_indent_size=4;
//保留空行
//var opt_indent_char = options.indent_char ? options.indent_char : ' ';
var opt_indent_char =' ';
//
//var opt_preserve_newlines = typeof options.preserve_newlines === 'undefined' ? true : options.preserve_newlines;
var opt_preserve_newlines =true;
//var opt_indent_level = options.indent_level ? options.indent_level : 0; // starting indentation
var opt_indent_level = 0;
//var opt_space_after_anon_function = options.space_after_anon_function === 'undefined' ? false : options.space_after_anon_function;
var opt_space_after_anon_function=true;
//var opt_keep_array_indentation = typeof options.keep_array_indentation === 'undefined' ? true : options.keep_array_indentation;
var opt_keep_array_indentation =false;
just_added_newline = false;
// cache the source's length.
var input_length = js_source_text.length;
function trim_output() {
while (output.length && (output[output.length - 1] === ' ' || output[output.length - 1] === indent_string)) {
output.pop();
}
}
function is_array(mode) {
return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]';
}
function print_newline(ignore_repeated) {
flags.eat_next_space = false;
if (opt_keep_array_indentation && is_array(flags.mode)) {
return;
}
ignore_repeated = typeof ignore_repeated === 'undefined' ? true : ignore_repeated;
flags.if_line = false;
trim_output();
if (!output.length) {
return; // no newline on start of file
}
if (output[output.length - 1] !== "\n" || !ignore_repeated) {
just_added_newline = true;
output.push("\n");
}
for (var i = 0; i < flags.indentation_level + (flags.var_line && flags.var_line_reindented ? 1 : 0); i += 1) {
output.push(indent_string);
}
}
function print_single_space() {
if (flags.eat_next_space) {
flags.eat_next_space = false;
return;
}
var last_output = ' ';
if (output.length) {
last_output = output[output.length - 1];
}
if (last_output !== ' ' && last_output !== '\n' && last_output !== indent_string) { // prevent occassional duplicate space
output.push(' ');
}
}
function print_token() {
just_added_newline = false;
flags.eat_next_space = false;
output.push(token_text);
}
function indent() {
flags.indentation_level += 1;
}
function remove_indent() {
if (output.length && output[output.length - 1] === indent_string) {
output.pop();
}
}
function set_mode(mode) {
if (flags) {
flag_store.push(flags);
}
flags = {
mode: mode,
var_line: false,
var_line_tainted: false,
var_line_reindented: false,
in_html_comment: false,
if_line: false,
in_case: false,
eat_next_space: false,
indentation_baseline: -1,
indentation_level: (flags ? flags.indentation_level + ((flags.var_line && flags.var_line_reindented) ? 1 : 0) : opt_indent_level)
};
}
function is_expression(mode) {
return mode === '[EXPRESSION]' || mode === '[INDENTED-EXPRESSION]' || mode === '(EXPRESSION)';
}
function restore_mode() {
do_block_just_closed = flags.mode === 'DO_BLOCK';
if (flag_store.length > 0) {
flags = flag_store.pop();
}
}
function in_array(what, arr) {
for (var i = 0; i < arr.length; i += 1) {
if (arr[i] === what) {
return true;
}
}
return false;
}
// Walk backwards from the colon to find a '?' (colon is part of a ternary op)
// or a '{' (colon is part of a class literal). Along the way, keep track of
// the blocks and expressions we pass so we only trigger on those chars in our
// own level, and keep track of the colons so we only trigger on the matching '?'.
function is_ternary_op() {
var level = 0,
colon_count = 0;
for (var i = output.length - 1; i >= 0; i--) {
switch (output[i]) {
case ':':
if (level === 0) {
colon_count++;
}
break;
case '?':
if (level === 0) {
if (colon_count === 0) {
return true;
} else {
colon_count--;
}
}
break;
case '{':
if (level === 0) {
return false;
}
level--;
break;
case '(':
case '[':
level--;
break;
case ')':
case ']':
case '}':
level++;
break;
}
}
}
function get_next_token() {
n_newlines = 0;
if (parser_pos >= input_length) {
return ['', 'TK_EOF'];
}
wanted_newline = false;
var c = input.charAt(parser_pos);
parser_pos += 1;
var keep_whitespace = opt_keep_array_indentation && is_array(flags.mode);
if (keep_whitespace) {
//
// slight mess to allow nice preservation of array indentation and reindent that correctly
// first time when we get to the arrays:
// var a = [
// ....'something'
// we make note of whitespace_count = 4 into flags.indentation_baseline
// so we know that 4 whitespaces in original source match indent_level of reindented source
//
// and afterwards, when we get to
// 'something,
// .......'something else'
// we know that this should be indented to indent_level + (7 - indentation_baseline) spaces
//
var whitespace_count = 0;
while (in_array(c, whitespace)) {
if (c === "\n") {
trim_output();
output.push("\n");
just_added_newline = true;
whitespace_count = 0;
} else {
if (c === '\t') {
whitespace_count += 4;
} else {