JSDOC/Packer.js
authoralan <alan@alanfast.akbkhome.com>
Wed, 21 Apr 2010 07:20:47 +0000 (15:20 +0800)
committeralan <alan@alanfast.akbkhome.com>
Wed, 21 Apr 2010 07:20:47 +0000 (15:20 +0800)
JSDOC/Packer.js
JSDOC/ScopeParser.js
JSDOC/Token.js
JSDOC/TokenStream.js
pack.js

index c640e53..352a6e1 100644 (file)
@@ -105,9 +105,13 @@ Packer.prototype = {
      * @prop cleanup {Boolean} (optional) clean up temp files after done - 
      *    Defaults to false if you set tmpDir, otherwise true.
      */
-    cleanup : true, //
-    
+    cleanup : true,  
     
+    /**
+     * @prop prefix {String} (optional) prefix of directory to be stripped of when
+     *    Calculating md5 of filename 
+     */
+    prefix : '',  
     out : '', // if no target is specified - then this will contain the result
     
     packAll : function()  // do the packing (run from constructor)
@@ -278,7 +282,8 @@ Packer.prototype = {
         
         // at this point we can write a language file...
         if (this.translateJSON) {
-            this.writeTranslateFile(fn, minfile, tr.translateMap);
+            
+            this.writeTranslateFile(fn, minfile, toks);
         }
         
         this.activeFile = fn;
@@ -320,10 +325,22 @@ Packer.prototype = {
      * 
      */
     
-    writeTranslateFile : function(fn, minfile, map
+    writeTranslateFile : function(fn, minfile, toks
     {
+        
+        var map = {};
+        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.push( sval);
+            }
+        })
+        
         var transfile = minfile + '.lang.trans';
         var transmd5 = minfile + '.lang';
+        print("writeTranslateFile "  + transfile);
         var i = 0;
         var v = '';
         if (File.exists(transfile)) {
@@ -336,11 +353,10 @@ Packer.prototype = {
         if (!i ) {
             return; // no strings in file...
         }
-        var ff = fn.split('/');
-        var ffn = ff[ff.length-1];
+        var ffn = fn.substring(this.prefix.length);
          
          
-        File.write(transfile, "\n" + ffn.toSource() + " : {");
+        File.write(transfile, "\n'" + ffn  + "' : {");
         var l = '';
         var _tout = {}
          
@@ -365,12 +381,12 @@ Packer.prototype = {
     {
         //print("STRING HANDLER");
        // callback when outputing compressed file, 
-       var data = tok.outData !== false ? tok.outData : tok.data;
+       var data = tok.data;
         if (!this.translateJSON) {
          //   print("TURNED OFF");
             return data;
         }
-        if (tok.name == SINGLE_QUOTE) {
+        if (tok.name == 'SINGLE_QUOTE') {
             return data;
         }
         
@@ -383,12 +399,11 @@ Packer.prototype = {
        //     return tok.outData;
        // }
         
+        var sval = tok.data.substring(1,data.length-1);
+        var fn = this.activeFile.substring(this.prefix.length);
         
         
-        
-        var ff = this.activeFile.split('/');
-        var ffn = ff[ff.length-1];
-        return '_T["' + this.md5(ffn + '-' + sval) + '"]';
+        return '_T["' + this.md5(fn + '-' + sval) + '"]';
         
         
     }
index 57df912..1b315b0 100644 (file)
@@ -228,12 +228,15 @@ ScopeParser.prototype = {
                         if (!token) { // can return false at EOF!
                             break;
                         }
-                        if (token.name == "VAR") { // kludge..
+                        if (token.name == "VAR" || token.data == ',') { // kludge..
                             continue;
                         }
                         //this.logR("parseScope GOT VAR  : <B>" + token.toString() + "</B>"); 
                         if (token.type !="NAME") {
-                            print(token.toString());
+                            for(var i = Math.max(this.ts.cursor-10,0); i < this.ts.cursor+1; i++) {
+                                print(this.ts.tokens[i].toString());
+                            }
+                            
                             print( "var without ident");
                             Seed.quit()
                         }
index 30e03f1..0486c68 100644 (file)
@@ -36,7 +36,7 @@ Token = Object.define(
     {
          toString: function()
         {
-            return 'type:' + this.type + ', name:' + this.name + ', data:' + this.data;
+            return 'line:' + this.line + ', type:' + this.type + ', name:' + this.name + ', data:' + this.data;
         },
         
         
index 831a99a..b5385a6 100644 (file)
@@ -34,7 +34,9 @@ TokenStream = Object.define(
             if (typeof n == "undefined") n = 0;
 
             if (considerWhitespace == true) {
-                if (this.cursor+n < 0 || this.cursor+n > this.tokens.length) return {};
+                if (this.cursor+n < 0 || this.cursor+n > (this.tokens.length -1)) {
+                    return new Token("", "VOID", "START_OF_STREAM");
+                }
                 return this.tokens[this.cursor+n];
             }
             else {
diff --git a/pack.js b/pack.js
index 9342046..bb63856 100755 (executable)
--- a/pack.js
+++ b/pack.js
@@ -7,6 +7,8 @@
  * -t Translate json file.
  * -w Cache / working dir.
  * -C no cleanup (use with -w if you need are using a cache directory.)
+ * -p prefix for translation md5 generator (directory that files are in, and is removed 
+ *    from path when generating an md5 for the translated name.
  *
  * compresses files listed as arguments and outputs result
  */
@@ -41,11 +43,18 @@ for(var i =0; i < args.length;i++) {
         i++;
         continue;
     }
-    
+    if (args[i] == '-p') {
+        cfg.prefix = args[i+1];
+        i++;
+        continue;
+    }
     if (args[i] == '-C') {
         cfg.cleanup = false;
         continue;
     }
+    if (cfg.files.indexOf(args[i]) > -1) {
+        continue; // remove dupes.
+    }
     cfg.files.push(args[i]);
 }
 var pack;
@@ -53,7 +62,7 @@ try {
     pack = new Packer(cfg)
 } catch (e) {
     print("ERROR " + e.toString());
-    Seed.quit();
+    throw e;
 }
 if (!pack.target) {
     print(pack.out);