//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[0] !== '<') {
formated_code =js_beautify(js_source, {indent_size: indent_size, indent_char: indent_char, preserve_newlines:preserve_newlines});
}
if(!formated_code.length==0)
WScript.Echo(formated_code);
else
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();
}