generate gjs documentation, run under gjs
[gnome.introspection-doc-generator] / JsTemplate / Template.js
index 6adb708..bd094ad 100644 (file)
@@ -1,60 +1,63 @@
 //<script type="text/javscript">
 
-Gio = imports.gi.Gio;
-GLib = imports.gi.GLib;
+const Gio     = imports.gi.Gio;
+const GLib    = imports.gi.GLib;
 
-XObject = imports.XObject.XObject;
+const XObject = imports.XObject.XObject;
+const console = imports.console.console;
 
-console = imports.console.console;
+const File    = imports.File.File;
 
 /**
  * Template 
  * 
  * 
  */
-  
-  
 
-Template = XObject.define(
-    
+var Template = XObject.define(
 
     function(cfg) {
-        XObject.extend(this, cfg)
+        XObject.extend(this, cfg);
+
         //this.templateFile = templateFile;
-        if (!this.templateFile  || !this.Link) {
-            throw  "No templateFile or Link sent to Template Contructor..";
-            
+
+        if (!this.templateFile || !this.Link) {
+            throw  "No templateFile or Link sent to Template Constructor..";
         }
-        
-        
-        this.template = Gio.simple_read(this.templateFile);
+        //console.log("this.templateFile: " + this.templateFile);
+
+        //this.template     = Gio.simple_read(this.templateFile);
+        this.template     = File.read(this.templateFile);
+        //console.log("this.template: " + this.template);
+
         this.templateName = GLib.path_get_basename(this.templateFile);
         this.code = "";
         this.parse();
-    }, 
-    Object,  {
-    
+    },
+
+    Object,
 
+    {
         parse : function() {
-            
-            console.log("Parsing template? " + this.templateName);
-            
+
+            console.log("Parsing template: " + this.templateName);
             this.template = this.template.replace(/\{#[\s\S]+?#\}/gi, "");
-            this.code = "var output=``"+this.template;
+            this.code     = "var output=``" + this.template;
 
             this.code = this.code.replace(
                 /<for +each="(.+?)" +in="(.+?)" *>/gi, 
                 function (match, eachName, inName) {
                     return "``;\rvar $"+eachName+"_keys = keys("+inName+");\rfor(var $"+eachName+"_i = 0; $"+eachName+"_i < $"+eachName+"_keys.length; $"+eachName+"_i++) {\rvar $"+eachName+"_last = ($"+eachName+"_i == $"+eachName+"_keys.length-1);\rvar $"+eachName+"_key = $"+eachName+"_keys[$"+eachName+"_i];\rvar "+eachName+" = "+inName+"[$"+eachName+"_key];\routput+=``";
                 }
-            ); 
+            );
+
             this.code = this.code.replace(/<if test="(.+?)">/g, "``;\rif ($1) { \routput+=``");
             this.code = this.code.replace(/<else\s*\/>/g, "``;} \relse\r{ \routput+=``");
-            
+
             this.code = this.code.replace(/<\/(if|for)>/g, "``;\r};\routput+=``");
-            
+
             //File.write("/tmp/jstookit_eval_"+this.templateName+".4.js", this.code);
-            
+
             this.code = this.code.replace(
                 /\{\+\s*([\s\S]+?)\s*\+\}/gi,
                 function (match, code) {
@@ -64,7 +67,7 @@ Template = XObject.define(
                 }
             );
             //File.write("/tmp/jstookit_eval_"+this.templateName+".6.js", this.code);
-            
+
             this.code = this.code.replace(
                 /\{!\s*([\s\S]+?)\s*!\}/gi,
                 function (match, code) {
@@ -76,17 +79,13 @@ Template = XObject.define(
            //File.write("/tmp/jstookit_eval_"+this.templateName+".7.js", this.code);
             this.code = this.code+"``;";
 
-            
-            
             this.code = this.code.replace(/(\r?\n)/g, "\\n");
             this.code = this.code.replace(/"/g, "\\\"");
-            
+
             this.code = this.code.replace(/``/g, "\"");
             this.code = this.code.replace(/\\r/g, "\n");
             //File.write("/tmp/jstookit_eval_"+this.templateName+".9.js", this.code);
             this.code = this.code.replace(/\r/g, "\n\n");
-            
-
         },
 
         toCode : function() {
@@ -128,49 +127,52 @@ Template = XObject.define(
         process : function(data) {
             
             //console.log("processing template");
-            var keys = this.keys;
-            var values = this.values;
+            var keys          = this.keys;
+            var values        = this.values;
             
-            var makeSortby = this.makeSortby;
-            var makeSignature =   XObject.createDelegate(this.makeSignature, this);
-            var summarize = this.summarize ;
-            var makeFuncSkel = this.makeFuncSkel;
-            var resolveLinks = this.resolveLinks;
-            var makeImage = this.makeImage;
+            var makeSortby    = this.makeSortby;
+            var makeSignature = XObject.createDelegate(this.makeSignature, this);
+            var summarize     = this.summarize ;
+            var makeFuncSkel  = this.makeFuncSkel;
+            var resolveLinks  = this.resolveLinks;
+            var makeImage     = this.makeImage;
+
             // usefull for cross refing..
             Template.data = data;
-            
+
             var Link = this.Link;
-            var Options = imports.Options ? imports.Options.Options : false;
+            //var Options = imports.Options ? imports.Options.Options : false;
+
             try {
                 eval(this.code);
             } catch (e) {
-                 Gio.simple_write('/tmp/template.js', this.code);
-                 Seed.print('in /tmp/template.js');
-                throw e;
-                Seed.quit();
+                 //Gio.simple_write('/tmp/template.js', this.code);
+                 File.write('/tmp/template.js', this.code);
+                 //Seed.print('in /tmp/template.js');
+                 console.log('in /tmp/template.js');
+                 //throw e;
+                 //Seed.quit();
             }
-            
-            
+
             //File.write("/tmp/jstookit_eval.js", this.code);
             //try {
             //eval('include     "/tmp/jstookit_eval.js";');
             //includeJs("/tmp/jstookit_eval.js");
                 //eval(this.code);
-           // console.log("done eval of template");   
-            
+            // console.log("done eval of template");
+
             return output;
         },
 
-     
         isdefined : function (typ) {
             return typ != 'undefined';
         },
-        
-        
+
         summarize : function(desc) {
             if (typeof desc != "undefined")
                 return desc.match(/([\w\W]+?\.)[^a-z0-9]/i)? RegExp.$1 : desc;
+            else
+                return '';
         },
 
         /** make a symbol sorter by some attribute */
@@ -180,24 +182,22 @@ Template = XObject.define(
                     a = a[attribute]; //.toLowerCase();
                     b = b[attribute];//.toLowerCase();
                     if (a < b) return -1;
-                    if (a > b) return 1;
-                    return 0;
+                    if (a > b) return  1;
+                    //return 0;
                 }
+                return 0;
             }
         },
+
         makeImage : function(alias) {
             ///  http://library.gnome.org/devel/gtk/stable/notebook.png
-            var ns = alias.split('.').shift();
+            var ns  = alias.split('.').shift();
             var cls = alias.split('.').pop().toLowerCase();
             if (ns != 'Gtk' ) {
                 return '';//ns;
             }
             return '<img class="class-picture" src="http://library.gnome.org/devel/gtk/stable/' + cls + '.png">';
-            
-            
         },
-        
-        
 
         makeSignature : function(params) {
             if (!params) return "()";
@@ -240,30 +240,32 @@ Template = XObject.define(
                 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ")
             +
             ")\n{\n\n}";
-            
+
         },
 
         /** Find symbol {@link ...} strings in text and turn into html links */
         resolveLinks : function (str, from) {
+
             if (!str || typeof(str) == 'undefined') {
                 return '';
             }
-            
+
             // gtk specific. now..
             // @ -> bold.. - they are arguments..
-            
+
             str = str.replace(/@([a-z_]+)/gi,
                 function(match, symbolName) {
                     return '<b>' + symbolName + '</b>';
                 }
             );
+
             // constants.
             str = str.replace(/%([a-z_]+)/gi,
                 function(match, symbolName) {
                     return '<b>' + symbolName + '</b>';
                 }
             );
-            
+
             str = str.replace(/#([a-z_]+)/gi,
                 function(match, symbolName) {
                     return '<b>' + symbolName + '</b>';
@@ -272,9 +274,9 @@ Template = XObject.define(
                     //return new Link().toSymbol(Template.data.ns + '.' + symbolName);
                 }
             );
-            
+
             str = str.replace(/\n/gi, '<br/>');
-            
+
             /*
             str = str.replace(/\{@link ([^} ]+) ?\}/gi,
                 function(match, symbolName) {
@@ -305,9 +307,7 @@ Template = XObject.define(
                 }
             );
             */
-            
+
             return str;
         }
-        
-        
-});
\ No newline at end of file
+});