Skip to content

Commit

Permalink
editplus格式化代码
Browse files Browse the repository at this point in the history
editplus格式化代码
  • Loading branch information
hizhengfu committed May 22, 2012
0 parents commit c62cbec
Show file tree
Hide file tree
Showing 7 changed files with 2,518 additions and 0 deletions.
65 changes: 65 additions & 0 deletions JSBeautify.wsf
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<job id="JSBeautify">
<script src="beautify.js" language="JScript"></script>
<script src="beautify-css.js" language="JScript"></script>
<script src="beautify-html.js" language="JScript"></script>
<script src="format-sql.js" language="JScript"></script>
<script src="format-css.js" language="JScript"></script>
<script language="JScript">
var opts = {
indent_size: '4',//缩进1的时候表示tab,其它数字表示多少个空格
indent_char: ' ',//缩进字符
preserve_newlines:false,//是否替换新行
insert_newlines:false,//css中是否插入新行
brace_style: 'collapse',
indent_scripts:'normal',
jslint_happy: true,
keep_array_indentation:false,//保留数组格式
space_after_anon_function:true,
space_before_conditional:true
};
var args = WScript.Arguments;
var filepath = args(0)||'';
if(args.length>0) {
var stream = new ActiveXObject("ADODB.Stream"),
source='',
result='';

stream.Mode = 3; // 常用值 1:读,2:写,3:读写
stream.Type = 2; // 1:二进制,2:文本(默认)
stream.Charset = 'UTF-8'; // 指定编码
stream.Open();
stream.LoadFromFile(filepath);
source = stream.ReadText(-1); // 读取全部内容
stream.Close();

var extension=filepath.substring(filepath.lastIndexOf('.')+1,filepath.length);
switch (extension) {
case 'js':
case 'vb':
case 'wsf':
result=js_beautify(source, opts);
break;
case 'html':
case 'htm':
opts.max_char='120';
opts.unformatted=['a', 'sub', 'sup', 'b', 'i', 'u'];
result=style_html(source, opts);
break;
case 'css':
result=css_beautify(source, opts);
break;
case 'sql':
result=formatSql(source, opts);
break;
default:
result=formatCss(source, opts);
break;
}
WScript.StdOut.Write(result);
}else{
//调试可以用下面这个来查看运行情况
WScript.Echo("Are you sure your input is javascript source file?");
}
WScript.Quit();
</script>
</job>
198 changes: 198 additions & 0 deletions beautify-css.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
CSS Beautifier
---------------
Written by Harutyun Amirjanyan, (amirjanyan@gmail.com)
Based on code initially developed by: Einar Lielmanis, <elfz@laacz.lv>
http://jsbeautifier.org/
You are free to use this in any way you want, in case you find this useful or working for you.
Usage:
css_beautify(source_text);
css_beautify(source_text, options);
The options are:
indent_size (default 4) — indentation size,
indent_char (default space) — character to indent with,
e.g
css_beautify(css_source_text, {
'indent_size': 1,
'indent_char': '\t'
});
*/

// http://www.w3.org/TR/CSS21/syndata.html#tokenization
// http://www.w3.org/TR/css3-syntax/
function css_beautify(source_text, options) {
options = options || {};
var indentSize = options.indent_size || 4;
var indentCharacter = options.indent_char || ' ';

// compatibility
if (typeof indentSize == "string")
indentSize = parseInt(indentSize);


// tokenizer
var whiteRe = /^\s+$/;
var wordRe = /[\w$\-_]/;

var pos = -1, ch;
function next() {
return ch = source_text.charAt(++pos)
}
function peek() {
return source_text.charAt(pos+1)
}
function eatString(comma) {
var start = pos;
while(next()){
if (ch=="\\"){
next();
next();
} else if (ch == comma) {
break;
} else if (ch == "\n") {
break;
}
}
return source_text.substring(start, pos + 1);
}

function eatWhitespace() {
var start = pos;
while (whiteRe.test(peek()))
pos++;
return pos != start;
}

function skipWhitespace() {
var start = pos;
do{
}while (whiteRe.test(next()))
return pos != start + 1;
}

function eatComment() {
var start = pos;
next();
while (next()) {
if (ch == "*" && peek() == "/") {
pos ++;
break;
}
}

return source_text.substring(start, pos + 1);
}


function lookBack(str, index) {
return output.slice(-str.length + (index||0), index).join("").toLowerCase() == str;
}

// printer
var indentString = source_text.match(/^[\r\n]*[\t ]*/)[0];
var singleIndent = Array(indentSize + 1).join(indentCharacter);
var indentLevel = 0;
function indent() {
indentLevel++;
indentString += singleIndent;
}
function outdent() {
indentLevel--;
indentString = indentString.slice(0, -indentSize);
}

print = {}
print["{"] = function(ch) {
print.singleSpace();
output.push(ch);
print.newLine();
}
print["}"] = function(ch) {
print.newLine();
output.push(ch);
print.newLine();
}

print.newLine = function(keepWhitespace) {
if (!keepWhitespace)
while (whiteRe.test(output[output.length - 1]))
output.pop();

if (output.length)
output.push('\n');
if (indentString)
output.push(indentString);
}
print.singleSpace = function() {
if (output.length && !whiteRe.test(output[output.length - 1]))
output.push(' ');
}
var output = [];
if (indentString)
output.push(indentString);
/*_____________________--------------------_____________________*/

while(true) {
var isAfterSpace = skipWhitespace();

if (!ch)
break;

if (ch == '{') {
indent();
print["{"](ch);
} else if (ch == '}') {
outdent();
print["}"](ch);
} else if (ch == '"' || ch == '\'') {
output.push(eatString(ch))
} else if (ch == ';') {
output.push(ch, '\n', indentString);
} else if (ch == '/' && peek() == '*') { // comment
print.newLine();
output.push(eatComment(), "\n", indentString);
} else if (ch == '(') { // may be a url
output.push(ch);
eatWhitespace();
if (lookBack("url", -1) && next()) {
if (ch != ')' && ch != '"' && ch != '\'')
output.push(eatString(')'));
else
pos--;
}
} else if (ch == ')') {
output.push(ch);
} else if (ch == ',') {
eatWhitespace();
output.push(ch);
print.singleSpace();
} else if (ch == ']') {
output.push(ch);
} else if (ch == '[' || ch == '=') { // no whitespace before or after
eatWhitespace();
output.push(ch);
} else {
if (isAfterSpace)
print.singleSpace();

output.push(ch);
}
}


var sweetCode = output.join('').replace(/[\n ]+$/, '');
return sweetCode;
}


if (typeof exports !== "undefined")
exports.css_beautify = css_beautify;
Loading

0 comments on commit c62cbec

Please sign in to comment.