grat 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ function SanityTest(func,test_name){var test_func=func||function(x){return x};var test_name=test_name||"";var n_failed=0;var n_succeeded=0;var failures=[];this.test_function=function(func,name){test_func=func;test_name=name||""};this.expect=function(parameters,expected_value){var result=test_func(parameters);if((result===expected_value)||(expected_value instanceof Array&&result.join(", ")==expected_value.join(", "))){n_succeeded+=1}else{n_failed+=1;failures.push([test_name,parameters,expected_value,result])}};this.results_raw=function(){var results="";if(n_failed===0){if(n_succeeded===0){results="No tests run."}else{results="All "+n_succeeded+" tests passed."}}else{for(var i=0;i<failures.length;i++){var f=failures[i];if(f[0]){f[0]=f[0]+" "}results+="---- "+f[0]+"input -------\n"+this.prettyprint(f[1])+"\n";results+="---- "+f[0]+"expected ----\n"+this.prettyprint(f[2])+"\n";results+="---- "+f[0]+"output ------\n"+this.prettyprint(f[3])+"\n\n"}results+=n_failed+" tests failed.\n"}return results};this.results=function(){return this.lazy_escape(this.results_raw())};this.prettyprint=function(something,quote_strings){var type=typeof something;switch(type.toLowerCase()){case"string":if(quote_strings){return"'"+something.replace("'","\\'")+"'"}else{return something}case"number":return""+something;case"boolean":return something?"true":"false";case"undefined":return"undefined";case"object":if(something instanceof Array){var x=[];var expected_index=0;for(k in something){if(k==expected_index){x.push(this.prettyprint(something[k],true));expected_index+=1}else{x.push("\n"+k+": "+this.prettyprint(something[k],true))}}return"["+x.join(", ")+"]"}else{return"object: "+something}default:return type+": "+something}};this.lazy_escape=function(str){return str.replace(/</g,"&lt;").replace(/\>/g,"&gt;").replace(/\n/g,"<br />")};this.log=function(){if(window.console){if(console.firebug){console.log.apply(console,Array.prototype.slice.call(arguments))}else{console.log.call(console,Array.prototype.slice.call(arguments))}}}}function js_beautify(js_source_text,options){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 indent_level,wanted_newline,just_added_newline;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;var opt_space_after_anon_function=options.space_after_anon_function==="undefined"?false:options.space_after_anon_function;just_added_newline=false;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;flags.if_line=false;trim_output();if(!output.length){return}if(output[output.length-1]!=="\n"||!ignore_repeated){just_added_newline=true;output.push("\n")}for(var i=0;i<indent_level;i+=1){output.push(indent_string)}}function print_space(){var last_output=" ";if(output.length){last_output=output[output.length-1]}if(last_output!==" "&&last_output!=="\n"&&last_output!==indent_string){output.push(" ")}}function print_token(){just_added_newline=false;output.push(token_text)}function indent(){indent_level+=1}function unindent(){if(indent_level){indent_level-=1}}function remove_indent(){if(output.length&&output[output.length-1]===indent_string){output.pop()}}function print_javadoc_comment(){var lines=token_text.split("\n");output.push(lines[0]);for(var i=1;i<lines.length;i++){print_newline();output.push(" ");output.push(lines[i].replace(/^\s+/,""))}}function set_mode(mode){if(flags){flag_store.push(flags)}flags={mode:mode,var_line:false,var_line_tainted:false,if_line:false,in_case:false}}function is_expression(mode){return mode==="[EXPRESSION]"||mode==="[INDENTED-EXPRESSION]"||mode==="(EXPRESSION)"}function restore_mode(){do_block_just_closed=flags.mode==="DO_BLOCK";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}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(){var n_newlines=0;if(parser_pos>=input.length){return["","TK_EOF"]}var c=input.charAt(parser_pos);parser_pos+=1;while(in_array(c,whitespace)){if(parser_pos>=input.length){return["","TK_EOF"]}if(c==="\n"){n_newlines+=1}c=input.charAt(parser_pos);parser_pos+=1}wanted_newline=false;if(opt_preserve_newlines){if(n_newlines>1){for(var i=0;i<2;i+=1){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}}}if(parser_pos!==input.length&&c.match(/^[0-9]+[Ee]$/)&&(input.charAt(parser_pos)==="-"||input.charAt(parser_pos)==="+")){var sign=input.charAt(parser_pos);parser_pos+=1;var t=get_next_token(parser_pos);c+=sign+t[0];return[c,"TK_WORD"]}if(c==="in"){return[c,"TK_OPERATOR"]}if(wanted_newline&&last_type!=="TK_OPERATOR"&&!flags.if_line){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="";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"]}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==="'"||c==='"'||(c==="/"&&((last_type==="TK_WORD"&&last_text==="return")||(last_type==="TK_START_EXPR"||last_type==="TK_START_BLOCK"||last_type==="TK_END_BLOCK"||last_type==="TK_OPERATOR"||last_type==="TK_EOF"||last_type==="TK_SEMICOLON")))){var sep=c;var esc=false;var resulting_string=c;if(parser_pos<input.length){if(sep==="/"){var in_char_class=false;while(esc||in_char_class||input.charAt(parser_pos)!==sep){resulting_string+=input.charAt(parser_pos);if(!esc){esc=input.charAt(parser_pos)==="\\";if(input.charAt(parser_pos)==="["){in_char_class=true}else{if(input.charAt(parser_pos)==="]"){in_char_class=false}}}else{esc=false}parser_pos+=1;if(parser_pos>=input.length){return[resulting_string,"TK_STRING"]}}}else{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){return[resulting_string,"TK_STRING"]}}}}parser_pos+=1;resulting_string+=sep;if(sep==="/"){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(c==="#"){var sharp="#";if(parser_pos<input.length&&in_array(input.charAt(parser_pos),digits)){do{c=input.charAt(parser_pos);sharp+=c;parser_pos+=1}while(parser_pos<input.length&&c!=="#"&&c!=="=");if(c==="#"){return[sharp,"TK_WORD"]}else{return[sharp,"TK_OPERATOR"]}}}if(c==="<"&&input.substring(parser_pos-1,parser_pos+3)==="<!--"){parser_pos+=3;return["<!--","TK_COMMENT"]}if(c==="-"&&input.substring(parser_pos-1,parser_pos+2)==="-->"){parser_pos+=2;if(wanted_newline){print_newline()}return["-->","TK_COMMENT"]}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>0){indent_string+=opt_indent_char;opt_indent_size-=1}indent_level=opt_indent_level;input=js_source_text;last_word="";last_type="TK_START_EXPR";last_text="";last_last_text="";output=[];do_block_just_closed=false;whitespace="\n\r\t ".split("");wordchar="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$".split("");digits="0123456789".split("");punct="+ - * / % & ++ -- = += -= *= /= %= == === != !== > < >= <= >> << >>> >>>= >>= <<= && &= | || ! !! , : ? ^ ^= |= ::".split(" ");line_starters="continue,try,throw,return,var,if,switch,case,default,for,while,break,function".split(",");flag_store=[];set_mode("BLOCK");parser_pos=0;while(true){var t=get_next_token(parser_pos);token_text=t[0];token_type=t[1];if(token_type==="TK_EOF"){break}switch(token_type){case"TK_START_EXPR":if(token_text==="["){if(last_type==="TK_WORD"||last_text===")"){if(last_word==="return"||last_word==="throw"){print_space()}set_mode("(EXPRESSION)");print_token();break}if(flags.mode==="[EXPRESSION]"||flags.mode==="[INDENTED-EXPRESSION]"){if(last_last_text==="]"&&last_text===","){indent();print_newline();set_mode("[INDENTED-EXPRESSION]")}else{if(last_text==="["){indent();print_newline();set_mode("[INDENTED-EXPRESSION]")}else{set_mode("[EXPRESSION]")}}}else{set_mode("[EXPRESSION]")}}else{set_mode("(EXPRESSION)")}if(last_text===";"||last_type==="TK_START_BLOCK"){print_newline()}else{if(last_type==="TK_END_EXPR"||last_type==="TK_START_EXPR"){}else{if(last_type!=="TK_WORD"&&last_type!=="TK_OPERATOR"){print_space()}else{if(last_word==="function"){if(opt_space_after_anon_function){print_space()}}else{if(in_array(last_word,line_starters)){print_space()}}}}}print_token();break;case"TK_END_EXPR":if(token_text==="]"&&flags.mode==="[INDENTED-EXPRESSION]"){unindent()}restore_mode();print_token();break;case"TK_START_BLOCK":if(last_word==="do"){set_mode("DO_BLOCK")}else{set_mode("BLOCK")}if(last_type!=="TK_OPERATOR"&&last_type!=="TK_START_EXPR"){if(last_type==="TK_START_BLOCK"){print_newline()}else{print_space()}}print_token();indent();break;case"TK_END_BLOCK":if(last_type==="TK_START_BLOCK"){if(just_added_newline){remove_indent()}else{trim_output()}unindent()}else{unindent();print_newline()}print_token();restore_mode();break;case"TK_WORD":if(do_block_just_closed){print_space();print_token();print_space();do_block_just_closed=false;break}if(token_text==="function"){if(last_text===";"||last_text==="}"){print_newline(false);print_newline(false)}}if(token_text==="case"||token_text==="default"){if(last_text===":"){remove_indent()}else{unindent();print_newline();indent()}print_token();flags.in_case=true;break}prefix="NONE";if(last_type==="TK_END_BLOCK"){if(!in_array(token_text.toLowerCase(),["else","catch","finally"])){prefix="NEWLINE"}else{prefix="SPACE";print_space()}}else{if(last_type==="TK_SEMICOLON"&&(flags.mode==="BLOCK"||flags.mode==="DO_BLOCK")){prefix="NEWLINE"}else{if(last_type==="TK_SEMICOLON"&&is_expression(flags.mode)){prefix="SPACE"}else{if(last_type==="TK_STRING"){prefix="NEWLINE"}else{if(last_type==="TK_WORD"){prefix="SPACE"}else{if(last_type==="TK_START_BLOCK"){prefix="NEWLINE"}else{if(last_type==="TK_END_EXPR"){print_space();prefix="NEWLINE"}}}}}}}if(last_type!=="TK_END_BLOCK"&&in_array(token_text.toLowerCase(),["else","catch","finally"])){print_newline()}else{if(in_array(token_text,line_starters)||prefix==="NEWLINE"){if(last_text==="else"){print_space()}else{if((last_type==="TK_START_EXPR"||last_text==="="||last_text===",")&&token_text==="function"){}else{if(last_text==="return"||last_text==="throw"){print_space()}else{if(last_type!=="TK_END_EXPR"){if((last_type!=="TK_START_EXPR"||token_text!=="var")&&last_text!==":"){if(token_text==="if"&&last_word==="else"&&last_text!=="{"){print_space()}else{print_newline()}}}else{if(in_array(token_text,line_starters)&&last_text!==")"){print_newline()}}}}}}else{if(prefix==="SPACE"){print_space()}}}print_token();last_word=token_text;if(token_text==="var"){flags.var_line=true;flags.var_line_tainted=false}if(token_text==="if"||token_text==="else"){flags.if_line=true}break;case"TK_SEMICOLON":print_token();flags.var_line=false;break;case"TK_STRING":if(last_type==="TK_START_BLOCK"||last_type==="TK_END_BLOCK"||last_type==="TK_SEMICOLON"){print_newline()}else{if(last_type==="TK_WORD"){print_space()}}print_token();break;case"TK_OPERATOR":var start_delim=true;var end_delim=true;if(flags.var_line&&token_text===","&&(is_expression(flags.mode))){flags.var_line_tainted=false}if(flags.var_line){if(token_text===","){if(flags.var_line_tainted){print_token();print_newline();output.push(indent_string);flags.var_line_tainted=false;break}else{flags.var_line_tainted=false}}else{flags.var_line_tainted=true;if(token_text===":"){flags.var_line=false}}}if(last_text==="return"||last_text==="throw"){print_space();print_token();break}if(token_text===":"&&flags.in_case){print_token();print_newline();flags.in_case=false;break}if(token_text==="::"){print_token();break}if(token_text===","){if(flags.var_line){if(flags.var_line_tainted){print_token();print_newline();flags.var_line_tainted=false}else{print_token();print_space()}}else{if(last_type==="TK_END_BLOCK"){print_token();print_newline()}else{if(flags.mode==="BLOCK"){print_token();print_newline()}else{print_token();print_space()}}}break}else{if(token_text==="--"||token_text==="++"){if(last_text===";"){if(flags.mode==="BLOCK"){print_newline();start_delim=true;end_delim=false}else{start_delim=true;end_delim=false}}else{if(last_text==="{"){print_newline()}start_delim=false;end_delim=false}}else{if((token_text==="!"||token_text==="+"||token_text==="-")&&(last_text==="return"||last_text==="case")){start_delim=true;end_delim=false}else{if((token_text==="!"||token_text==="+"||token_text==="-")&&last_type==="TK_START_EXPR"){start_delim=false;end_delim=false}else{if(last_type==="TK_OPERATOR"){start_delim=false;end_delim=false}else{if(last_type==="TK_END_EXPR"){start_delim=true;end_delim=true}else{if(token_text==="."){start_delim=false;end_delim=false}else{if(token_text===":"){if(is_ternary_op()){start_delim=true}else{start_delim=false}}}}}}}}}if(start_delim){print_space()}print_token();if(end_delim){print_space()}break;case"TK_BLOCK_COMMENT":print_newline();if(token_text.substring(0,3)=="/**"){print_javadoc_comment()}else{print_token()}print_newline();break;case"TK_COMMENT":print_space();print_token();print_newline();break;case"TK_UNKNOWN":print_token();break}last_last_text=last_text;last_type=token_type;last_text=token_text}return output.join("").replace(/\n+$/,"")}var indent_size=4;var indent_char=" ";var preserve_newlines=true;var space_after_anon_function=true;function test_beautifier(input){return js_beautify(input,{indent_size:indent_size,indent_char:indent_char,preserve_newlines:preserve_newlines,space_after_anon_function:space_after_anon_function})}var sanitytest;function bt(input,expected){var wrapped_input,wrapped_expectation;expected=expected||input;sanitytest.expect(input,expected);if(indent_size===4&&input){wrapped_input="{\n"+input+"\nindent;}";wrapped_expectation="{\n"+expected.replace(/^(.+)$/mg," $1")+"\n indent;\n}";sanitytest.expect(wrapped_input,wrapped_expectation)}}function run_beautifier_tests(test_obj){sanitytest=test_obj||new SanityTest();sanitytest.test_function(test_beautifier,"js_beautify");indent_size=4;tests_passed=0;tests_failed=0;indent_char=" ";test_result="";preserve_newlines=true;space_after_anon_function=true;bt("");bt("a = 1","a = 1");bt("a=1","a = 1");bt("a();\n\nb();","a();\n\nb();");bt("var a = 1 var b = 2","var a = 1\nvar b = 2");bt("var a=1, b=c[d], e=6;","var a = 1,\n b = c[d],\n e = 6;");bt('a = " 12345 "');bt("a = ' 12345 '");bt("if (a == 1) b = 2;","if (a == 1) b = 2;");bt("if(1){2}else{3}","if (1) {\n 2\n} else {\n 3\n}");bt("if(1||2);","if (1 || 2);");bt("(a==1)||(b==2)","(a == 1) || (b == 2)");bt("var a = 1 if (2) 3;","var a = 1\nif (2) 3;");bt("a = a + 1");bt("a = a == 1");bt("/12345[^678]*9+/.match(a)");bt("a /= 5");bt("a = 0.5 * 3");bt("a *= 10.55");bt("a < .5");bt("a <= .5");bt("a<.5","a < .5");bt("a<=.5","a <= .5");bt("a = 0xff;");bt("a=0xff+4","a = 0xff + 4");bt("a = [1, 2, 3, 4]");bt("F*(g/=f)*g+b","F * (g /= f) * g + b");bt("a.b({c:d})","a.b({\n c: d\n})");bt("a.b\n(\n{\nc:\nd\n}\n)","a.b({\n c: d\n})");bt("a=!b","a = !b");bt("a?b:c","a ? b : c");bt("a?1:2","a ? 1 : 2");bt("a?(b):c","a ? (b) : c");bt('x={a:1,b:w=="foo"?x:y,c:z}','x = {\n a: 1,\n b: w == "foo" ? x : y,\n c: z\n}');bt("x=a?b?c?d:e:f:g;","x = a ? b ? c ? d : e : f : g;");bt("x=a?b?c?d:{e1:1,e2:2}:f:g;","x = a ? b ? c ? d : {\n e1: 1,\n e2: 2\n} : f : g;");bt("function void(void) {}");bt("if(!a)foo();","if (!a) foo();");bt("a=~a","a = ~a");bt("a;/*comment*/b;","a;\n/*comment*/\nb;");bt("if(a)break;","if (a) break;");bt("if(a){break}","if (a) {\n break\n}");bt("if((a))foo();","if ((a)) foo();");bt("for(var i=0;;)","for (var i = 0;;)");bt("a++;","a++;");bt("for(;;i++)","for (;; i++)");bt("for(;;++i)","for (;; ++i)");bt("return(1)","return (1)");bt("try{a();}catch(b){c();}finally{d();}","try {\n a();\n} catch(b) {\n c();\n} finally {\n d();\n}");bt("(xx)()");bt("a[1]()");bt("if(a){b();}else if(c) foo();","if (a) {\n b();\n} else if (c) foo();");bt("switch(x) {case 0: case 1: a(); break; default: break}","switch (x) {\ncase 0:\ncase 1:\n a();\n break;\ndefault:\n break\n}");bt("switch(x){case -1:break;case !y:break;}","switch (x) {\ncase -1:\n break;\ncase !y:\n break;\n}");bt("a !== b");bt("if (a) b(); else c();","if (a) b();\nelse c();");bt("// comment\n(function something() {})");bt("{\n\n x();\n\n}");bt("if (a in b) foo();");bt("{a:1, b:2}","{\n a: 1,\n b: 2\n}");bt("a={1:[-1],2:[+1]}","a = {\n 1: [-1],\n 2: [+1]\n}");bt("var l = {'a':'1', 'b':'2'}","var l = {\n 'a': '1',\n 'b': '2'\n}");bt("if (template.user[n] in bk) foo();");bt("{{}/z/}","{\n {}\n /z/\n}");bt("return 45","return 45");bt("If[1]","If[1]");bt("Then[1]","Then[1]");bt("a = 1e10","a = 1e10");bt("a = 1.3e10","a = 1.3e10");bt("a = 1.3e-10","a = 1.3e-10");bt("a = -1.3e-10","a = -1.3e-10");bt("a = 1e-10","a = 1e-10");bt("a = e - 10","a = e - 10");bt("a = 11-10","a = 11 - 10");bt("a = 1;// comment\n","a = 1; // comment");bt("a = 1; // comment\n","a = 1; // comment");bt("a = 1;\n // comment\n","a = 1;\n// comment");bt("if (a) {\n do();\n}");bt("if\n(a)\nb();","if (a) b();");bt("if (a) {\n// comment\n}else{\n// comment\n}","if (a) {\n // comment\n} else {\n // comment\n}");bt("if (a) {\n// comment\n// comment\n}","if (a) {\n // comment\n // comment\n}");bt("if (a) b() else c();","if (a) b()\nelse c();");bt("if (a) b() else if c() d();","if (a) b()\nelse if c() d();");bt("{}");bt("{\n\n}");bt("do { a(); } while ( 1 );","do {\n a();\n} while (1);");bt("do {} while (1);");bt("do {\n} while (1);","do {} while (1);");bt("do {\n\n} while (1);");bt("var a = x(a, b, c)");bt("delete x if (a) b();","delete x\nif (a) b();");bt("delete x[x] if (a) b();","delete x[x]\nif (a) b();");bt("for(var a=1,b=2)","for (var a = 1, b = 2)");bt("for(var a=1,b=2,c=3)","for (var a = 1, b = 2, c = 3)");bt("for(var a=1,b=2,c=3;d<3;d++)","for (var a = 1, b = 2, c = 3; d < 3; d++)");bt("function x(){(a||b).c()}","function x() {\n (a || b).c()\n}");bt("function x(){return - 1}","function x() {\n return -1\n}");bt("function x(){return ! a}","function x() {\n return !a\n}");bt("a = 'a'\nb = 'b'");bt("a = /reg/exp");bt("a = /reg/");bt("/abc/.test()");bt("/abc/i.test()");bt("{/abc/i.test()}","{\n /abc/i.test()\n}");bt("{x=#1=[]}","{\n x = #1=[]\n}");bt("{a:#1={}}","{\n a: #1={}\n}");bt("{a:#1#}","{\n a: #1#\n}");test_beautifier("{a:#1","{\n a: #1");test_beautifier("{a:#","{\n a: #");test_beautifier("<!--\nvoid();\n// -->","<!--\nvoid();\n// -->");test_beautifier("a=/regexp","a = /regexp");bt("{a:#1=[],b:#1#,c:#999999#}","{\n a: #1=[],\n b: #1#,\n c: #999999#\n}");bt("a = 1e+2");bt("a = 1e-2");bt("do{x()}while(a>1)","do {\n x()\n} while (a > 1)");bt("x(); /reg/exp.match(something)","x();\n/reg/exp.match(something)");bt("something();(","something();\n(");bt("function namespace::something()");test_beautifier("<!--\nsomething();\n-->","<!--\nsomething();\n-->");test_beautifier("<!--\nif(i<0){bla();}\n-->","<!--\nif (i < 0) {\n bla();\n}\n-->");test_beautifier("<!--\nsomething();\n-->\n<!--\nsomething();\n-->","<!--\nsomething();\n-->\n<!--\nsomething();\n-->");test_beautifier("<!--\nif(i<0){bla();}\n-->\n<!--\nif(i<0){bla();}\n-->","<!--\nif (i < 0) {\n bla();\n}\n-->\n<!--\nif (i < 0) {\n bla();\n}\n-->");bt("{foo();--bar;}","{\n foo();\n --bar;\n}");bt("{foo();++bar;}","{\n foo();\n ++bar;\n}");bt("{--bar;}","{\n --bar;\n}");bt("{++bar;}","{\n ++bar;\n}");bt("a(/abc\\/\\/def/);b()","a(/abc\\/\\/def/);\nb()");bt("a(/a[b\\[\\]c]d/);b()","a(/a[b\\[\\]c]d/);\nb()");test_beautifier("a(/a[b\\[","a(/a[b\\[");bt("a(/[a/b]/);b()","a(/[a/b]/);\nb()");bt("a=[[1,2],[4,5],[7,8]]","a = [\n [1, 2],\n [4, 5],\n [7, 8]]");bt("a=[a[1],b[4],c[d[7]]]","a = [a[1], b[4], c[d[7]]]");bt("[1,2,[3,4,[5,6],7],8]","[1, 2, [3, 4, [5, 6], 7], 8]");bt('[[["1","2"],["3","4"]],[["5","6","7"],["8","9","0"]],[["1","2","3"],["4","5","6","7"],["8","9","0"]]]','[\n [\n ["1", "2"],\n ["3", "4"]],\n [\n ["5", "6", "7"],\n ["8", "9", "0"]],\n [\n ["1", "2", "3"],\n ["4", "5", "6", "7"],\n ["8", "9", "0"]]]');bt("{[x()[0]];indent;}","{\n [x()[0]];\n indent;\n}");bt("return ++i","return ++i");bt("return !!x","return !!x");bt("return !x","return !x");bt("return [1,2]","return [1, 2]");bt("return;","return;");bt("return\nfunc","return\nfunc");bt("/**\n* foo\n*/","/**\n * foo\n */");bt(" /**\n * foo\n */","/**\n * foo\n */");bt("{\n/**\n* foo\n*/\n}","{\n /**\n * foo\n */\n}");bt("var a,b,c=1,d,e,f=2","var a, b, c = 1,\n d, e, f = 2");bt("var a,b,c=[],d,e,f=2","var a, b, c = [],\n d, e, f = 2");bt("function () {\n var a, b, c, d, e = [],\n f;\n}");space_after_anon_function=true;bt("// comment 1\n(function()","// comment 1\n(function ()");bt("var a1, b1, c1, d1 = 0, c = function() {}, d = '';","var a1, b1, c1, d1 = 0,\n c = function () {},\n d = '';");bt("var o1=$.extend(a,function(){alert(x);}","var o1 = $.extend(a, function () {\n alert(x);\n}");bt("var o1=$.extend(a);function(){alert(x);}","var o1 = $.extend(a);\n\nfunction () {\n alert(x);\n}");space_after_anon_function=false;bt("// comment 2\n(function()","// comment 2\n(function()");bt("var a2, b2, c2, d2 = 0, c = function() {}, d = '';","var a2, b2, c2, d2 = 0,\n c = function() {},\n d = '';");bt("var o2=$.extend(a,function(){alert(x);}","var o2 = $.extend(a, function() {\n alert(x);\n}");bt("var o2=$.extend(a);function(){alert(x);}","var o2 = $.extend(a);\n\nfunction() {\n alert(x);\n}");bt("{[y[a]];keep_indent;}","{\n [y[a]];\n keep_indent;\n}");bt("if (x) {y} else { if (x) {y}}","if (x) {\n y\n} else {\n if (x) {\n y\n }\n}");indent_size=1;indent_char=" ";bt("{ one_char() }","{\n one_char()\n}");indent_size=4;indent_char=" ";bt("{ one_char() }","{\n one_char()\n}");indent_size=1;indent_char="\t";bt("{ one_char() }","{\n\tone_char()\n}");preserve_newlines=false;bt("var\na=dont_preserve_newlines","var a = dont_preserve_newlines");preserve_newlines=true;bt("var\na=do_preserve_newlines","var\na = do_preserve_newlines");return sanitytest}function style_html(html_source,indent_size,indent_character,max_char){var Parser,multi_parser;function Parser(){this.pos=0;this.token="";this.current_mode="CONTENT";this.tags={parent:"parent1",parentcount:1,parent1:""};this.tag_type="";this.token_text=this.last_token=this.last_text=this.token_type="";this.Utils={whitespace:"\n\r\t ".split(""),single_token:"br,input,link,meta,!doctype,basefont,base,area,hr,wbr,param,img,isindex,?xml,embed".split(","),extra_liners:"head,body,/html".split(","),in_array:function(what,arr){for(var i=0;i<arr.length;i++){if(what===arr[i]){return true}}return false}};this.get_content=function(){var input_char="";var content=[];var space=false;while(this.input.charAt(this.pos)!=="<"){if(this.pos>=this.input.length){return content.length?content.join(""):["","TK_EOF"]}input_char=this.input.charAt(this.pos);this.pos++;this.line_char_count++;if(this.Utils.in_array(input_char,this.Utils.whitespace)){if(content.length){space=true}this.line_char_count--;continue}else{if(space){if(this.line_char_count>=this.max_char){content.push("\n");for(var i=0;i<this.indent_level;i++){content.push(this.indent_string)}this.line_char_count=0}else{content.push(" ");this.line_char_count++}space=false}}content.push(input_char)}return content.length?content.join(""):""};this.get_script=function(){var input_char="";var content=[];var reg_match=new RegExp("<\/script>","igm");reg_match.lastIndex=this.pos;var reg_array=reg_match.exec(this.input);var end_script=reg_array?reg_array.index:this.input.length;while(this.pos<end_script){if(this.pos>=this.input.length){return content.length?content.join(""):["","TK_EOF"]}input_char=this.input.charAt(this.pos);this.pos++;content.push(input_char)}return content.length?content.join(""):""};this.record_tag=function(tag){if(this.tags[tag+"count"]){this.tags[tag+"count"]++;this.tags[tag+this.tags[tag+"count"]]=this.indent_level}else{this.tags[tag+"count"]=1;this.tags[tag+this.tags[tag+"count"]]=this.indent_level}this.tags[tag+this.tags[tag+"count"]+"parent"]=this.tags.parent;this.tags.parent=tag+this.tags[tag+"count"]};this.retrieve_tag=function(tag){if(this.tags[tag+"count"]){var temp_parent=this.tags.parent;while(temp_parent){if(tag+this.tags[tag+"count"]===temp_parent){break}temp_parent=this.tags[temp_parent+"parent"]}if(temp_parent){this.indent_level=this.tags[tag+this.tags[tag+"count"]];this.tags.parent=this.tags[temp_parent+"parent"]}delete this.tags[tag+this.tags[tag+"count"]+"parent"];delete this.tags[tag+this.tags[tag+"count"]];if(this.tags[tag+"count"]==1){delete this.tags[tag+"count"]}else{this.tags[tag+"count"]--}}};this.get_tag=function(){var input_char="";var content=[];var space=false;do{if(this.pos>=this.input.length){return content.length?content.join(""):["","TK_EOF"]}input_char=this.input.charAt(this.pos);this.pos++;this.line_char_count++;if(this.Utils.in_array(input_char,this.Utils.whitespace)){space=true;this.line_char_count--;continue}if(input_char==="'"||input_char==='"'){if(!content[1]||content[1]!=="!"){input_char+=this.get_unformatted(input_char);space=true}}if(input_char==="="){space=false}if(content.length&&content[content.length-1]!=="="&&input_char!==">"&&space){if(this.line_char_count>=this.max_char){this.print_newline(false,content);this.line_char_count=0}else{content.push(" ");this.line_char_count++}space=false}content.push(input_char)}while(input_char!==">");var tag_complete=content.join("");var tag_index;if(tag_complete.indexOf(" ")!=-1){tag_index=tag_complete.indexOf(" ")}else{tag_index=tag_complete.indexOf(">")}var tag_check=tag_complete.substring(1,tag_index).toLowerCase();if(tag_complete.charAt(tag_complete.length-2)==="/"||this.Utils.in_array(tag_check,this.Utils.single_token)){this.tag_type="SINGLE"}else{if(tag_check==="script"){this.record_tag(tag_check);this.tag_type="SCRIPT"}else{if(tag_check==="style"){this.record_tag(tag_check);this.tag_type="STYLE"}else{if(tag_check.charAt(0)==="!"){if(tag_check.indexOf("[if")!=-1){if(tag_complete.indexOf("!IE")!=-1){var comment=this.get_unformatted("-->",tag_complete);content.push(comment)}this.tag_type="START"}else{if(tag_check.indexOf("[endif")!=-1){this.tag_type="END";this.unindent()}else{if(tag_check.indexOf("[cdata[")!=-1){var comment=this.get_unformatted("]]>",tag_complete);content.push(comment);this.tag_type="SINGLE"}else{var comment=this.get_unformatted("-->",tag_complete);content.push(comment);this.tag_type="SINGLE"}}}}else{if(tag_check.charAt(0)==="/"){this.retrieve_tag(tag_check.substring(1));this.tag_type="END"}else{this.record_tag(tag_check);this.tag_type="START"}if(this.Utils.in_array(tag_check,this.Utils.extra_liners)){this.print_newline(true,this.output)}}}}}return content.join("")};this.get_unformatted=function(delimiter,orig_tag){if(orig_tag&&orig_tag.indexOf(delimiter)!=-1){return""}var input_char="";var content="";var space=true;do{input_char=this.input.charAt(this.pos);this.pos++;if(this.Utils.in_array(input_char,this.Utils.whitespace)){if(!space){this.line_char_count--;continue}if(input_char==="\n"||input_char==="\r"){content+="\n";for(var i=0;i<this.indent_level;i++){content+=this.indent_string}space=false;this.line_char_count=0;continue}}content+=input_char;this.line_char_count++;space=true}while(content.indexOf(delimiter)==-1);return content};this.get_token=function(){var token;if(this.last_token==="TK_TAG_SCRIPT"){var temp_token=this.get_script();if(typeof temp_token!=="string"){return temp_token}token=js_beautify(temp_token,{indent_size:this.indent_size,indent_char:this.indent_character,indent_level:this.indent_level});return[token,"TK_CONTENT"]}if(this.current_mode==="CONTENT"){token=this.get_content();if(typeof token!=="string"){return token}else{return[token,"TK_CONTENT"]}}if(this.current_mode==="TAG"){token=this.get_tag();if(typeof token!=="string"){return token}else{var tag_name_type="TK_TAG_"+this.tag_type;return[token,tag_name_type]}}};this.printer=function(js_source,indent_character,indent_size,max_char){this.input=js_source||"";this.output=[];this.indent_character=indent_character||" ";this.indent_string="";this.indent_size=indent_size||2;this.indent_level=0;this.max_char=max_char||70;this.line_char_count=0;for(var i=0;i<this.indent_size;i++){this.indent_string+=this.indent_character}this.print_newline=function(ignore,arr){this.line_char_count=0;if(!arr||!arr.length){return}if(!ignore){while(this.Utils.in_array(arr[arr.length-1],this.Utils.whitespace)){arr.pop()}}arr.push("\n");for(var i=0;i<this.indent_level;i++){arr.push(this.indent_string)}};this.print_token=function(text){this.output.push(text)};this.indent=function(){this.indent_level++};this.unindent=function(){if(this.indent_level>0){this.indent_level--}}};return this}multi_parser=new Parser();multi_parser.printer(html_source,indent_character,indent_size);while(true){var t=multi_parser.get_token();multi_parser.token_text=t[0];multi_parser.token_type=t[1];if(multi_parser.token_type==="TK_EOF"){break}switch(multi_parser.token_type){case"TK_TAG_START":case"TK_TAG_SCRIPT":case"TK_TAG_STYLE":multi_parser.print_newline(false,multi_parser.output);multi_parser.print_token(multi_parser.token_text);multi_parser.indent();multi_parser.current_mode="CONTENT";break;case"TK_TAG_END":multi_parser.print_newline(true,multi_parser.output);multi_parser.print_token(multi_parser.token_text);multi_parser.current_mode="CONTENT";break;case"TK_TAG_SINGLE":multi_parser.print_newline(false,multi_parser.output);multi_parser.print_token(multi_parser.token_text);multi_parser.current_mode="CONTENT";break;case"TK_CONTENT":if(multi_parser.token_text!==""){multi_parser.print_newline(false,multi_parser.output);multi_parser.print_token(multi_parser.token_text)}multi_parser.current_mode="TAG";break}multi_parser.last_token=multi_parser.token_type;multi_parser.last_text=multi_parser.token_text}return multi_parser.output.join("")};
@@ -10,27 +10,16 @@
10
10
  %a{:href => url}
11
11
  %strong= url
12
12
  == (a #{content.type})
13
- / %form.add_field
14
- / %fieldset
15
- / %legend Add a field
16
- / .fieldname
17
- / %label{:for => 'fieldname'} Field Name:
18
- / %input{:name => 'fieldname'}
19
- / .fieldtype
20
- / %label{:for => 'fieldtype'} Field Type:
21
- / %select{:name => 'fieldtype'}
22
- / %option{:value => 'text'} Text
23
- / %option{:value => 'textarea'} TextArea
24
- / .submit
25
- / %input{:name => 'fieldsubmit', :type => 'submit', :value => "Add field!"}
26
13
 
27
14
  %form{:action => "/admin#{url}", :method => 'post'}
28
15
  .template
29
16
  %label{:for => form_nest('template_url')} Template
30
- %select{:name => form_nest('template')}
31
- %option None
17
+ %select{:name => form_nest('template_url')}
18
+ %option{:value=>''} None
32
19
  - Grat::Template.all.each do |template|
33
- %option{:value => template.url}= template.url
20
+ - option_hash = {:value => template.url}
21
+ - option_hash.merge!(:selected => 'selected') if template.url == content.template_url
22
+ %option{option_hash}= template.url
34
23
  .url
35
24
  %label{:for => form_nest('url')} Url
36
25
  %input{:name => form_nest('url'), :value => content.url}
@@ -49,4 +38,7 @@
49
38
  %a.add{:href=>'#new_field'} Add a New Field
50
39
 
51
40
  .submit
52
- %input{:name => form_nest('submit'), :type => 'submit', :value => "Let's save this thing"}
41
+ %input{:name => form_nest('submit'), :type => 'submit', :value => "Let's save this thing"}
42
+ .defaults
43
+ %label{:for => form_nest('default_content_vars')} Default Content
44
+ %textarea{:id => 'default_content_vars', :name => form_nest('default_content_vars')}= content.default_content_vars.to_json
@@ -8,6 +8,8 @@
8
8
  %script{:type=>"text/javascript", :src=>"/gratfiles/jquery-combined.min.js"}
9
9
  %script{:type=>"text/javascript", :src=>"/gratfiles/application.js"}
10
10
 
11
+ %script{:type=>"text/javascript", :src=>"/gratfiles/js-beautifier.min.js"}
12
+
11
13
  %link{:rel => 'stylesheet', :type => 'text/css', :media => 'all', :href => '/gratfiles/oocss.min.css'}
12
14
  %link{:rel => 'stylesheet', :type => 'text/css', :media => 'all', :href => '/gratfiles/custom.css'}
13
15
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grat
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sam Schenkman-Moore
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-31 00:00:00 -04:00
12
+ date: 2009-11-01 01:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -89,8 +89,22 @@ files:
89
89
  - lib/grat/template.rb
90
90
  - public/gratfiles/application.js
91
91
  - public/gratfiles/custom.css
92
+ - public/gratfiles/einars-js-beautify/HTML-Beautify.js
93
+ - public/gratfiles/einars-js-beautify/beautify-cl.js
94
+ - public/gratfiles/einars-js-beautify/beautify-tests.js
95
+ - public/gratfiles/einars-js-beautify/beautify.js
96
+ - public/gratfiles/einars-js-beautify/bin/beautify_js
97
+ - public/gratfiles/einars-js-beautify/index.html
98
+ - public/gratfiles/einars-js-beautify/javascriptobfuscator_unpacker.js
99
+ - public/gratfiles/einars-js-beautify/license.txt
100
+ - public/gratfiles/einars-js-beautify/sanitytest.js
101
+ - public/gratfiles/einars-js-beautify/unmaintained/bbedit/jsBeautify_BBED.scpt
102
+ - public/gratfiles/einars-js-beautify/unmaintained/c-sharp/JSBeautify.cs
103
+ - public/gratfiles/einars-js-beautify/unmaintained/opera-userscript/make_opera_userscript.sh
104
+ - public/gratfiles/einars-js-beautify/unmaintained/opera-userscript/opera_userscript.js
92
105
  - public/gratfiles/favicon.ico
93
106
  - public/gratfiles/jquery-combined.min.js
107
+ - public/gratfiles/js-beautifier.min.js
94
108
  - public/gratfiles/oocss.min.css
95
109
  - public/gratfiles/oocss/content.css
96
110
  - public/gratfiles/oocss/grids.css