JSDOC/Packer.js
[gnome.introspection-doc-generator] / JSDOC / Packer.js
index 820a20d..ccf4fbd 100644 (file)
@@ -1,16 +1,16 @@
 // <script type="text/javascript">
-XObject         = imports.XObject.XObject;
-File            = imports.File.File;
+const XObject         = imports.XObject.XObject;
+const File            = imports.File.File;
 
-TextStream      = imports.TextStream.TextStream;
-TokenReader     = imports.TokenReader.TokenReader;
-ScopeParser     = imports.ScopeParser.ScopeParser;
-TokenStream     = imports.TokenStream.TokenStream;
-CompressWhite   = imports.CompressWhite.CompressWhite;
-Collapse        = imports.Collapse.Collapse;
+const TextStream      = imports.JSDOC.TextStream.TextStream ;
+const TokenReader     = imports.TokenReader.TokenReader;
+const ScopeParser     = imports.ScopeParser.ScopeParser;
+const TokenStream     = imports.TokenStream.TokenStream;
+const CompressWhite   = imports.CompressWhite.CompressWhite;
+const Collapse        = imports.Collapse.Collapse;
 
-GLib = imports.gi.GLib;
-Gio = imports.gi.Gio;
+const GLib = imports.gi.GLib;
+const Gio = imports.gi.Gio;
 /**
  * @namespace JSDOC
  * @class  Packer
@@ -28,6 +28,8 @@ var x = new  Packer({
     files : [ "/location/of/file1.js", "/location/of/file2.js", ... ],
     target : "/tmp/output.js",
     debugTarget : "/tmp/output.debug.js", // merged file without compression.
+    debugTranslateTarget : "/tmp/output.translate.js", // merged with translation
+                                                       // and no compression
     translateJSON: "/tmp/translate.json",
     
     
@@ -68,7 +70,7 @@ x.packFiles(
  * 
 
  */
-Packer = function(cfg)
+const Packer = function(cfg)
 {
     
     XObject.extend(this, cfg);
@@ -89,12 +91,12 @@ Packer = function(cfg)
         
         function dateString(d){
             function pad(n){return n<10 ? '0'+n : n}
-            return d.getUTCFullYear() +
-                 pad(d.getUTCMonth()+1)+
-                 pad(d.getUTCDate())+'_'+
-                 pad(d.getUTCHours())+
-                 pad(d.getUTCMinutes())+
-                 pad(d.getUTCSeconds());
+            return d.getFullYear() +
+                 pad(d.getMonth()+1)+
+                 pad(d.getDate())+'_'+
+                 pad(d.getHours())+
+                 pad(d.getMinutes())+
+                 pad(d.getSeconds());
         }
 
         
@@ -149,9 +151,16 @@ Packer.prototype = {
      */
     module: false,
     /**
-     * @cfg {String} debugTargettarget to write files debug version to (uncompacted)- must be full path.
+     * @cfg {String} debugTarget target to write files debug version to (uncompacted)- must be full path.
      */
     debugTarget : '', // merged file without compression.
+    /**
+     * @cfg {String} debugTranslateTarget target to write files debug version
+     *            to (uncompacted) but with translation- must be full path.
+     */
+    
+    debugTranslateTarget : '', 
+    
     /**
      * @cfg {String} tmpDir  (optional) where to put the temporary files. 
      *      if you set this, then files will not be cleaned up
@@ -223,6 +232,9 @@ Packer.prototype = {
         if (this.debugTarget) {
             File.write(this.debugTarget, "");
         }
+        if (this.debugTranslateTarget) {
+            File.write(this.debugTarget, "");
+        }
         
         for(var i=0; i < this.files.length; i++)  {
             var file = this.files[i];
@@ -233,6 +245,8 @@ Packer.prototype = {
                 continue;
             }
            
+            // debug Target
+            
             if (this.debugTarget) {
                 File.append(this.debugTarget, File.read(file));
             }
@@ -254,20 +268,11 @@ Packer.prototype = {
                 print("compare : " + mt + "=>" + ot);
                 if (mt >= ot) {
                     continue;
-                    /*
-                    // then the min'files time is > than original..
-                    var str = File.read(minfile);
-                    print("using MIN FILE  "+ minfile);
-                    if (str.length) {
-                        File.append(outpath, str + "\n");
-                    }
                     
-                    continue;
-                    */
                 }
                 
             }
-            
+             
             print("COMPRESSING ");
             //var codeComp = pack(str, 10, 0, 0);
             if (File.exists(minfile)) {
@@ -275,13 +280,15 @@ Packer.prototype = {
             }
             var str = File.read(file);
             var str = this.pack(str, file, minfile);
-            if (str.length) {
-                File.write(minfile, str);
-            }
-            
              
           
-        }  
+        }
+        
+        
+        
+        // if we are translating, write the translations strings at the top
+        // of the file..
+        
         if (this.translateJSON) {
             
                
@@ -292,9 +299,7 @@ Packer.prototype = {
             } else {
                 this.out += out;
             }
-            
-            
-            
+             
             File.write(this.translateJSON, "");
             for(var i=0; i < this.files.length; i++)  {
                 var file = this.files[i];
@@ -342,8 +347,10 @@ Packer.prototype = {
             print("using MIN FILE  "+ minfile);
             if (str.length) {
                 if (this.target) {
+                    File.append(this.target, '//' + file + "\n");   
                     File.append(this.target, str + "\n");   
                 } else {
+                    this.out += '//' + file + "\n";
                     this.out += str + "\n";
                 }
                 
@@ -396,7 +403,7 @@ Packer.prototype = {
         
         // and replace if we are generating a different language..
         
-        this.timerPrint("Tokenized");
+        this.timerPrint("Tokenized tokens:" + toks.length);
         //var ts = new TokenStream(toks);
         //print(JSON.stringify(toks, null,4 )); Seed.quit();
         var ts = new Collapse(toks);
@@ -410,12 +417,18 @@ Packer.prototype = {
         sp.mungeSymboltree();
         this.timerPrint("Munged Sym tree");
         print(sp.warnings.join("\n"));
-        
+        this.timerPrint("Compressed");
         
         var out = CompressWhite(new TokenStream(toks), this, this.keepWhite); // do not kill whitespace..
         
         
         this.timerPrint("Compressed");
+        
+         if (out.length) {
+            File.write(minfile, out);
+            this.timerPrint("Write (" + out.length + "bytes) " + minfile);
+        }
+        
         return out;
         
         
@@ -436,21 +449,77 @@ Packer.prototype = {
      * -> this file will need inserting at the start of the application....
      * -> we need to generate 2 files, 
      * -> a reference used to do the translation, and the _T file..
+     *
+     *
+     * We store the trsum on the token...
      * 
      */
     
     writeTranslateFile : function(fn, minfile, toks) 
     {
         
-        var map = {};
+        var map = {};  // 'string=> md5sum'
         var _this = this;
-        toks.forEach(function (t) {
-            if (t.type == 'STRN' && t.name == 'DOUBLE_QUOTE') {
-                var sval = t.data.substring(1,t.data.length-1);
-                var ffn = fn.substring(_this.prefix.length);
-                map[sval] = _this.md5(ffn + '-' + sval);
+        var t, last, next;
+        
+        
+        var tokfind =  function (j,dir) {
+            while (1) {
+                if ((dir < 0) && (j < 0)) {
+                    return false;
+                }
+                if ((dir > 0) && (j >= toks.length)) {
+                    return false;
+                }
+                j += dir;
+                if (toks[j].type != 'WHIT') {
+                    return toks[j];
+                }
             }
-        })
+            return false;
+            
+        }
+        
+        
+        for (var i=0;i<toks.length;i++) {
+            
+            t = toks[i];
+            if (t.type != 'STRN') {
+                continue;
+            }
+            if (t.name != 'DOUBLE_QUOTE') {
+                continue;
+            }
+            
+            last = tokfind(i,-1);
+            next = tokfind(i,+1);
+            
+            // we have to ignore key values on objects
+            
+            // defined by
+            // last == '{' or ',' and
+            // next == ':'
+            
+            if (next &&
+                next.type == 'PUNC' &&
+                next.data == ':' && 
+                last && 
+                last.type == 'PUNC' &&
+                (last.data == ',' || last.data == '{')
+            ){
+                continue; // found object key... - we can not translate these
+            }
+                
+            var sval = t.data.substring(1,t.data.length-1);
+            var ffn = fn.substring(_this.prefix.length);
+            
+            t.trsum = _this.md5(ffn + '-' + sval);
+            map[sval] = t.trsum;
+            
+            
+            
+        }
+        
         
         var transfile = minfile + '.lang.trans';
         var transmd5 = minfile + '.lang';
@@ -479,10 +548,11 @@ Packer.prototype = {
             if (!v.length) {
                 continue;
             }
-            File.append(transfile, l + "\n\t" + JSON.stringify(v) + " : " + JSON.stringify(v));
+            File.append(transfile, l + "\n\t\"" + v  + "\" : \"" + v +"\"");
             l = ',';
             // strings are raw... - as the where encoded to start with!!!
-            File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]='+JSON.stringify(v)+";\n");
+            // so we should not need to encode them again.. - just wrap with "
+            File.append(transmd5, '_T["' + this.md5(ffn + '-' + v) + '"]="'+v+"\";\n");
         }
         File.append(transfile, "\n},"); // always one trailing..
         
@@ -507,6 +577,12 @@ Packer.prototype = {
             return data;
         }
         
+        if (typeof(tok.trsum) == 'undefined') {
+            return data;
+        }
+        
+        return '_T["' + tok.trsum + '"]';
+        
         var sval = data.substring(1,data.length-1);
         // we do not clean up... quoting here!??!!?!?!?!?