JSDOC/CompressWhite.js
authoralan <alan@alanfast.akbkhome.com>
Mon, 19 Apr 2010 04:50:15 +0000 (12:50 +0800)
committeralan <alan@alanfast.akbkhome.com>
Mon, 19 Apr 2010 04:50:15 +0000 (12:50 +0800)
JSDOC/CompressWhite.js

index e69de29..22b99e6 100644 (file)
@@ -0,0 +1,248 @@
+ // <script type="text/javascript">
+/**
+ * 
+ * pack a javascript file, and return a shorter version!
+ * 
+ * a bit picky at present with ; and crlf reading...
+ * 
+ */
+JSDOC.CompressWhite =  function (ts, packer)
+{
+    
+    ts.rewind();
+    //var str = File.read(fn);
+    var rep_var = 1;
+    
+    while (true) {
+        var tok = ts.next();
+        if (!tok) {
+            break;
+        }
+        if (tok._isWS) {
+            continue;
+            //if (tok._isDoc) {
+            //    continue;
+            //}
+            // just spaces, not \n!
+            //if (tok.data.indexOf("\n") < 0) {
+            //    continue;
+           // }
+            
+            
+        }
+        
+        // add semi-colon's where linebreaks are used... - not foolproof yet.!
+        if (tok.isTypeN(Script.TOKidentifier)) {
+            //var tokident = ts.look(-1).data + tok.data + ts.look(1).data +  ts.look(2).data;
+            // a = new function() {} 
+            if (ts.look(1).isTypeN(Script.TOKassign) && ts.look(2).isTypeN(Script.TOKnew) && 
+                ts.look(3).isTypeN(Script.TOKfunction)) {
+                // freeze time.. 
+                var cu = ts.cursor;
+                
+                ts.balance("lparen");
+                
+                
+                ts.balance("lbrace");
+                // if next is not ';' -> make it so...
+                
+                if (!ts.look(1).isTypeN(Script.TOKsemicolon) && !ts.look(1).isTypeN(Script.TOKrbrace) && ts.look(1,true).isLineBreak()) {
+                    ts.cur().outData = ts.cur().data +";";
+                }
+                // restore.. 
+                ts.cursor = cu;
+                continue;
+            }
+            // a = function() { ...
+               
+            if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKfunction)) {
+                // freeze time.. 
+                //println("got = function() ");
+                var cu = ts.cursor;
+                
+                ts.balance("lparen");
+                ts.balance("lbrace");
+                // if next is not ';' -> make it so...
+                // although this var a=function(){},v,c; causes 
+                if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
+                    ts.cur().outData = ts.cur().data+";";
+                }
+                // restore.. 
+                ts.cursor = cu;
+                continue;
+            }
+            // function a () { ... };
+                /*
+            if (ts.look(-1).isTypeN(Script.TOKfunction) &&  ts.look(1).isTypeN(Script.TOKlparen)) {
+                // freeze time.. 
+                //println("got = function() ");
+                var cu = ts.cursor;
+                
+                ts.balance("lparen");
+                ts.balance("lbrace");
+                // if next is not ';' -> make it so...
+                // although this var a=function(){},v,c; causes 
+                if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
+                    ts.cur().outData = ts.cur().data+";";
+                }
+                // restore.. 
+                ts.cursor = cu;
+                continue;
+            }
+            */
+            
+            // a = { ....
+                
+            if (ts.look(1).isTypeN(Script.TOKassign) &&  ts.look(2).isTypeN(Script.TOKlbrace)) {
+                // freeze time.. 
+                //println("----------*** 3 *** --------------");
+                var cu = ts.cursor;
+                
+                if (!ts.balance("lbrace") ){
+                    throw "could not find end lbrace!!!";
+                }
+                // if next is not ';' -> make it so...
+
+                if (!ts.look(1).isData(';') && !ts.look(1).isData('}') && ts.look(1,true).isLineBreak()) {
+                    ts.cur().outData = ts.cur().data +";";
+                }
+                // restore.. 
+                ts.cursor = cu;
+                continue;
+            }
+            
+            // any more??
+        }
+        
+        
+        
+         
+        //println("got Token: " + tok.type);
+        
+        
+        
+        switch(tok.tokN) {
+            // things that need space appending
+            case Script.TOKfunction:
+            case Script.TOKbreak:
+            case Script.TOKcontinue:
+                // if next item is a identifier..
+                if (ts.look(1).isTypeN(Script.TOKidentifier) || ts.look(1).data.match(/^[a-z]+$/i) ) { // as include is a keyword for us!!
+                   tok.outData =  tok.data + " ";
+                }
+                continue;
+                
+                
+            case Script.TOKreturn: // if next item is not a semi; (or }
+                if (ts.look(1).isData(';') || ts.look(1).isData('}')) {
+                    continue;
+                }
+                tok.outData =  tok.data + " ";
+                
+                continue;
+            
+                
+            case Script.TOKelse: // if next item is not a semi; (or }
+                if (!ts.look(1).isTypeN(Script.TOKif)) {
+                    continue;
+                }
+                
+                tok.outData =  tok.data + " ";
+                continue;
+            
+            case Script.TOKplusplus: // if previous was a plus or next is a + add a space..
+            case Script.TOKminusminus: // if previous was a - or next is a - add a space..
+            
+                var p = (Script.TOKminusminus == tok.tokN ? '-' : '+'); 
+            
+                if (ts.look(1).data == p) {
+                    tok.outData =  tok.data + " ";
+                }
+                if (ts.look(-1).data == p) {
+                    tok.outData =  " " +  tok.data;
+                    
+                }
+                continue;
+            
+            case Script.TOKin: // before and after?? 
+            case Script.TOKinstanceof:
+                
+                tok.outData = " " + tok.data + " ";
+                continue;
+            
+            case Script.TOKvar: // always after..
+            case Script.TOKnew:
+            case Script.TOKdelete:
+            case Script.TOKthrow:
+            case Script.TOKnew:
+            case Script.TOKcase:
+            case Script.TOKtypeof:
+            case Script.TOKvoid:
+                tok.outData =  tok.data + " ";
+                
+                continue
+             case Script.TOKsemicolon:
+                //remove semicolon before brace -- 
+                //if(ts.look(1).isTypeN(Script.TOKrbrace)) {
+                //    tok.outData = '';
+               // }
+                continue;
+           
+            default:
+                continue;
+        }
+    }
+    
+    ts.rewind();
+    
+    
+    //var f = new File(minfile, File.NEW);
+    
+    var out = '';
+    var outoff = 0;
+    out.length = ts.slen; // prealloc.
+    out = '';
+    while (true) {
+        var tok = ts.next();
+        if (!tok) {
+            break;
+        }
+        if (tok._isWS) {
+            continue;
+        }
+        
+        
+        if (tok.isTypeN(Script.TOKidentifier) && tok.identifier && tok.identifier.mungedValue.length) {
+            //f.write(tok.identifier.mungedValue);
+            out +=tok.identifier.mungedValue;
+            continue;
+        }
+        
+        // at this point we can apply a text translation kit...
+        
+        if (tok.type == 'string') {
+            if (packer.stringHandler) {
+                out += packer.stringHandler(tok);
+                continue;
+            }
+        }
+        //f.write(tok.outData);
+        out += tok.outData;
+        
+        if ((tok.outData == ';') && (out.length - outoff > 255)) {
+            outoff = out.length;
+            out += '\n';
+        }
+    }
+    //f.close();
+    /*
+    // remove the last ';' !!!
+    if (out.substring(out.length-1) == ';') {
+        return out.substring(0,out.length-1);
+       }
+    */
+    return out;
+    
+}
+    
+    
\ No newline at end of file