JSDOC/BuildDocs.js
[gnome.introspection-doc-generator] / JSDOC / BuildDocs.js
index 54592a4..024d628 100644 (file)
        This is the main container for the JSDOC application.
        @namespace
 */
+Gio = imports.gi.Gio;
 
-Options = import.Options.Options;
-XObject = import.XObject.XObject;
+XObject = imports.XObject.XObject;
+File = imports.File.File;
+
+Template = imports.JsTemplate.Template.Template;
+Link = imports.JsTemplate.Link.Link; // ?? fixme!??
+
+Parser      = imports.Parser.Parser;
+TextStream  = imports.TextStream.TextStream;
+TokenReader = imports.TokenReader.TokenReader;
+TokenStream = imports.TokenStream.TokenStream;
+Symbol      = imports.Symbol.Symbol;
+DocComment  = imports.DocComment.DocComment;
 
 /******************    INCLUDES ARE ALL AT THE BOTTOM OF THIS FILE!!!!! *******************/
 
 // should not realy be here -- or anywhere...??
 
+function makeSortby(attribute) {
+    return function(a, b) {
+        if (a[attribute] != undefined && b[attribute] != undefined) {
+            a = a[attribute]; //.toLowerCase();
+            b = b[attribute];//.toLowerCase();
+            if (a < b) return -1;
+            if (a > b) return 1;
+            return 0;
+        }
+    }
+}
 
-
+Options = false; // refer to this everywhere!
 
 
 BuildDocs = {
     
     VERSION : "2.0.0",
-    /**
-     * apply properties to an object
-     * 
-     * @param object to get properties (eg. JSDOC.Token.prototype)
-     * @param object of properties, - eg. a list of all the methods
-     * @param default (optional) -- no idea :)
-     */
+    
+    
+    srcFiles : [],
     
     
     build : function (opts)
     {
-        
-        XObject.extend(Options, opts);
-         
+        Options = opts; 
         Options.init();
         
-    
-         
-        
         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
-        Options.LOG.inform("With options: ");
-         
-        // the -c option: use a configuration file - not supported at present..
+        //Options.LOG.inform("With options: ");
         
-         
-             
+        if (Options.cacheDirectory.length && !File.isDirectory(Options.cacheDirectory)) {   
+            File.mkdir(Options.cacheDirectory)
+        }
+        
+        Options.srcFiles = this._getSrcFiles();
+        this._parseSrcFiles();
+        this.symbolSet = Parser.symbols;
         
+        // this currently uses the concept of publish.js...
+        
+        this.publish();
          
         
         
     },
     
     
-    
-    
-    
-    
-    apply : function(o, c, defaults){
-        if(defaults){
-            // no "this" reference for friendly out of scope calls
-            JSDOC.apply(o, defaults);
-        }
-        if(o && c && typeof c == 'object'){
-            for(var p in c){
-                o[p] = c[p];
-            }
+    _getSrcFiles : function() 
+    {
+        this.srcFiles = [];
+        var _this = this;
+        var ext = ["js"];
+        if (Options.ext) {
+            ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
         }
-        return o;
-    },
         
-    
-    /**
-     * Qdump is used to generated cache dumps of objects
-     * @arg mixed t - object/array to dump
-     * @arg string prefix output with
-     * @arg string suffix output with
-     * @arg mixed defaults for the object - so they are not dumped..
-     */
-    
-    toQDump : function(t, pref, suf, defs)
-    {
-        //println("got ToDump: " + pref +" ??? " + suf);
-        //var ret = "JSDOC.Symbol.fromDump(";
-        var qret   = "";
-        defs = (typeof(defs) == "undefined") ? {} : defs;
-           
-        var isAr = t.constructor == Array;
-        var k = '';
-        var v = '';
-        for (var i in t) {
-           // println("dump: " + i + " : " +typeof(t[i]));
+        for (var i = 0; i < Options.src.length; i++) {
+            // add to sourcefiles..
             
-            if (!isAr) {
-                k = typeof(i) == "number" ? i +":" : i.toSource()+":" ;
-            } else {
-                if (typeof(i) != "number") {
-                    continue;
+            File.list(Options.src[i] ).forEach(function($) {
+                if (Options['exclude-src'].indexOf($) > -1) {
+                    return;
+                }
+                var thisExt = $.split(".").pop().toLowerCase();
+                if (ext.indexOf(thisExt) < 0) {
+                    return;
                 }
+                _this.srcFiles.push(Options.src[i] + '/' + $);
+            });
                 
-            }
-            
-            v = t[i];
-            //} else {
-              //  v = i;
-            //}
-            
-            
+        }
+        //Seed.print(JSON.stringify(this.srcFiles, null,4));Seed.quit();
+        return this.srcFiles;
+    },
+
+    _parseSrcFiles : function() 
+    {
+        Parser.init();
+        
+        for (var i = 0, l = this.srcFiles.length; i < l; i++) {
             
-            var def = defs[i];
-            //if ((typeof(defs[i]) != 'undefined')  && (v.toSource() == defs[i]).toSource()) {
-            //    continue;
-            //}
+            var srcFile = this.srcFiles[i];
             
             
+            var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
             
+            //println(cacheFile);
+            // disabled at present!@!!
             
-            switch(typeof(v)) {
-                case "function":
-                    continue;
-              
-                    
-           
-                case 'string':
-                    if (def == v) {
-                        continue;
-                    }
+            if (false && !Options.disablecache  && File.exists(cacheFile)) {
+                // check filetime?
                 
-                    if (!v.length) { // ?? skip empty!!
-                        continue;
-                    }
+                var c_mt = File.getTimes(cacheFile);
+                var o_mt = File.getTimes(srcFile);
+                //println(c_mt.toSource());
+               // println(o_mt.toSource());
+               
+                // this check does not appear to work according to the doc's - need to check it out.
+               
+                if (c_mt[0] > o_mt[0]) { // cached time  > original time!
+                    // use the cached mtimes..
+                    var syms =  JSON.parse(File.read(cacheFile));
                     
-                   if (qret.length) {
-                        qret+=",";
+                    throw "Conversion of cache not done yet!";
+                    
+                    for (var sy in syms) {
+                        //println("ADD:" + sy );
+                       Parser.symbols.addSymbol(syms[sy]);
                     }
-
-                    qret += k + v.toSource();
                     continue;
-                case 'boolean':    
-              
-                case 'number':
-                    if (def == v) {
-                        continue;
-                    }
-                    if (qret.length) {
-                        qret+=",";
-                    }
+                }
+            }
+            
+            var src = ''
+            try {
+                Options.LOG.inform("reading : " + srcFile);
+                src = File.read(srcFile);
+            }
+            catch(e) {
+                Options.LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
+                continue;
+            }
 
-                    qret += k + v;
-                    
-                    continue;
+            var txs = new TextStream(src);
+            
+            var tr = new TokenReader({ keepComments : true, keepWhite : true , sepIdents: false });
+            
+            var ts = new TokenStream(tr.tokenize(txs));
+        
+            Parser.parse(ts, srcFile);
+              
+            //var outstr = JSON.stringify(
+            //    Parser.filesSymbols[srcFile]._index
+            //);
+            //File.write(cacheFile, outstr);
+             
                 
-                // undefined?
-                case "object":
-                    
-                    //if (i == 'tags') {
-                    //    println(k +"?:" + (v.constructor == Array ? "ARRAY" : "OBJECT"));
-                    //}
-                  
-                    // what about raw objects...
-                    if (v.constructor == Array)  {
-                        //println("ARRAYLEN:" + v.length);
-                        var toadd = JSDOC.toQDump(v, '[', ']');
-                        if (k == 'tags') {
-                            println("Array: " + toadd);
-                        }
-                        
-                        if (toadd == "[]") {
-                          continue;
-                        }
-                        
-                        if (qret.length) {
-                            qret+=",";
-                        }
-                        qret += k + toadd;
-                        //println("AFT AR:" + qret);
-                        continue;
-                    }
-                    if (v.constructor == Object)  {
-                        var toadd = JSDOC.toQDump(v, '{', '}');
-                       if (toadd == "{}") {
-                            continue;
-                        }
-                        
-                        if (qret.length) {
-                            qret+=",";
-                        }
-                        qret += k + toadd;
-                        //println("AFT AR:" + qret);
-                        continue;
-                    }
-                    //println("v:toQdump" + typeof(v['toQDump']));
-                    if (typeof(v['toQDump']) != "undefined") {
-                        if (qret.length) {
-                            qret+=",";
-                        }
+    //         }
+        }
+        
+        
+        
+        Parser.finish();
+    },
+    
+     
+        
+    publish  : function() {
+        Options.LOG.inform("Publishing");
+         
+        // link!!!
+        
+        
+        Options.LOG.inform("Making directories");
+        if (!File.isDirectory(Options.target))
+            File.mkdir(Options.target);
+        if (!File.isDirectory(Options.target+"/symbols"))
+            File.mkdir(Options.target+"/symbols");
+        if (!File.isDirectory(Options.target+"/symbols/src"))
+            File.mkdir(Options.target+"/symbols/src");
+        
+        if (!File.isDirectory(Options.target +"/json")) {
+            File.mkdir(Options.target +"/json");
+        }
+        
+        Options.LOG.inform("Copying files from static: " +Options.templateDir);
+        // copy everything in 'static' into 
+        File.list(Options.templateDir + '/static').forEach(function (f) {
+            Options.LOG.inform("Copy " + Options.templateDir + '/static/' + f + ' to  ' + Options.target + '/' + f);
+            File.copyFile(Options.templateDir + '/static/' + f, Options.target + '/' + f,  Gio.FileCopyFlags.OVERWRITE);
+        });
+        
+        
+        Options.LOG.inform("Setting up templates");
+        // used to check the details of things being linked to
+        Link.symbolSet = this.symbolSet;
+        Link.base = "../";
+        
+        var classTemplate = new Template({
+             templateFile : Options.templateDir  + "/class.html",
+             Link : Link
+        });
+        var classesTemplate = new Template({
+            templateFile : Options.templateDir +"/allclasses.html",
+            Link : Link
+        });
+        var classesindexTemplate = new Template({
+            templateFile : Options.templateDir +"/index.html",
+            Link : Link
+        });
+        var fileindexTemplate = new Template({   
+            templateFile : Options.templateDir +"/allfiles.html",
+            Link: Link
+        });
 
-                        qret += k + v.toQDump(v);
-                        continue;
-                    }
-                    println("SKIP   " + i + " : " +typeof(t[i]) + ":" + t[i].toSource());
-                    continue;
-                
-                default:
-                    println("SKIP   " + i + " : " +typeof(t[i]) );
-                
-                    continue;
-                
-            }
+        
+        classTemplate.symbolSet = this.symbolSet;
+        
+        
+        function hasNoParent($) {
+            return ($.memberOf == "")
+        }
+        function isaFile($) {
+            return ($.is("FILE"))
+        }
+        function isaClass($) { 
+            return ($.is("CONSTRUCTOR") || $.isNamespace); 
+        }
+        
+        
+        
+        
+        
+        
+        
+        
+        
+        
+        var symbols = this.symbolSet.toArray();
+        
+        var files = Options.srcFiles;
+        
+        for (var i = 0, l = files.length; i < l; i++) {
+            var file = files[i];
+            var targetDir = Options.target + "/symbols/src/";
+            this.makeSrcFile(file, targetDir);
+        }
+        
+        var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
+         
+         //Options.LOG.inform("classTemplate Process : all classes");
+            
+       // var classesIndex = classesTemplate.process(classes); // kept in memory
+        
+        Options.LOG.inform("iterate classes");
+        
+        for (var i = 0, l = classes.length; i < l; i++) {
+            var symbol = classes[i];
+            var output = "";
+            
+            Options.LOG.inform("classTemplate Process : " + symbol.alias);
+            
+            File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
+                    classTemplate.process(symbol));
+            
+            
+            
+            // dump out a 
+            
+            this.publishJSON(Options.target+"/json/" + symbol.alias+'.json', symbol)
+            
+            
+            
+        }
+        
+        // regenrate the index with different relative links
+        Link.base = "";
+        //var classesIndex = classesTemplate.process(classes);
+        
+        Options.LOG.inform("build index");
+        
+        File.write(Options.target +  "/index."+ Options.publishExt, 
+            classesindexTemplate.process(classes)
+        );
+        
+        // blank everything???? classesindexTemplate = classesIndex = classes = null;
+        
+        
+        var documentedFiles = symbols.filter(function ($) {
+            return ($.is("FILE"))
+        });
+        
+        var allFiles = [];
+        
+        for (var i = 0; i < files.length; i++) {
+            allFiles.push(new  Symbol(files[i], [], "FILE", new DocComment("/** */")));
         }
-       // println(qret);
-        //throw "done";
-        return pref + qret + suf;
+        
+        for (var i = 0; i < documentedFiles.length; i++) {
+            var offset = files.indexOf(documentedFiles[i].alias);
+            allFiles[offset] = documentedFiles[i];
+        }
+            
+        allFiles = allFiles.sort(makeSortby("name"));
+        Options.LOG.inform("write files index");
+        
+        File.write(Options.target + "/files."+Options.publishExt, 
+            fileindexTemplate.process(allFiles)
+        );
+        
     },
     /**
-     * 
-     * Depreciated - use Script.prettyDump() - cant remember real name..
+     * JSON files are lookup files for the documentation
+     * - can be used by IDE's or AJAX based doc tools
      * 
      * 
      */
-    
-    prettyDump : function(src)
+    publishJSON : function(file, data)
     {
-       // println("tidy SRC");
-       return Script.prettyDump(src);
-       /*
-        var ar = Script.tokenize(src);
-       println("tidy" + ar.length);
-        var eret = '';
-        var indent = "";
-        var lbr = 0;
-        
-        // this is reallllllllly sloooooowwwwww!!! = perhaps we should just dump it straight to the file!!!
-        
-        for( var i =0 ;i< ar.length; i++) {
+        // what we need to output to be usefull...
+        // a) props..
+        var cfgProperties = [];
+        if (!data.comment.getTag('singleton').length) {
+            cfgProperties = data.configToArray();
+            cfgProperties = cfgProperties.sort(makeSortby("name"));
             
-            //println(ar[i].toSource());
-            var tok = ar[i];
-            //println("tidy" + i + ":" + tok.val);
-            if ( (tok.val == '{') ||  (tok.val == '[') || (tok.val == '(') ) {
-                indent += "  ";
-                eret += tok.val;
-                lbr = true;
-                continue;
-            }
-            if ( (tok.val == '}') ||  (tok.val == ']') || (tok.val == ')') ) {
-                
-                indent = indent.substring(2); // strip spaces..
-                if (i < (ar.length-1) && ar[i+1].val == ',') {
-                    ar[i].val +=",";
-                    i++;
-                    
+        }
+        var props = []; 
+        //println(cfgProperties.toSource());
+        var p ='';
+        for(var i =0; i < cfgProperties.length;i++) {
+            p = cfgProperties[i];
+            props.push( {
+                name : p.name,
+                type : p.type,
+                desc : p.desc,
+                memberOf : p.memberOf == data.alias ? '' : p.memberOf
+            });
+        }
+        
+         
+        var ownEvents = data.methods.filter( function(e){
+                return e.isEvent && !e.comment.getTag('hide').length;
+            }).sort(makeSortby("name"));
+             
+        
+        var events = [];
+        var m;
+        for(var i =0; i < ownEvents.length;i++) {
+            m = ownEvents[i];
+            events.push( {
+                name : m.name.substring(1),
+                sig : this.makeFuncSkel(m.params),
+                type : 'function',
+                desc : m.desc
+            });
+        }
+        //println(props.toSource());
+        // we need to output:
+        //classname => {
+        //    propname => 
+        //        type=>
+        //        desc=>
+        //    }
+
+        var ret = {
+            props : props,
+            events: events
+        };
+        File.write(file, JSON.stringify(ret, null, 2 ));
+        
+        
+        // b) methods
+        // c) events
+        
+        
+    },
+    makeSrcFile: function(sourceFile) 
+    {
+        // this stuff works...
+     
+        
+        print("SF: " + sourceFile);
+        var name = sourceFile.substring(Options.baseDir.length+1);
+        print("SHORT NAME: " + name);
+        name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
+        print("SHORT NAME _: " + name);
+        name = name.replace(/\:/g, "_"); //??
+        print("SHORT NAME :: " + name);
+        
+        Options.LOG.inform("Write Source file : " + Options.target+"/symbols/src/" + name);
+        var pretty = imports.PrettyPrint.toPretty(File.read(  sourceFile));
+        File.write(Options.target+"/symbols/src/" + name, 
+            '<html><head>' +
+            '<title>' + sourceFile + '</title>' +
+            '<link rel="stylesheet" type="text/css" href="../../../highlight-js.css"/>' + 
+            '</head><body class="highlightpage">' +
+            pretty +
+            '</body></html>');
+    },
+    /**
+     * used by JSON output to generate a function skeleton
+     */
+    makeFuncSkel :function(params) {
+        if (!params) return "function ()\n{\n\n}";
+        return "function ("    +
+            params.filter(
+                function($) {
+                    return $.name.indexOf(".") == -1; // don't show config params in signature
                 }
-                eret += (lbr ? "" : "\n" +  indent ) + tok.val  ;
-                lbr = true;
-                continue;
-            }
-            if ( (tok.val == ',')) {
-                eret += tok.val;
-                lbr = true;
-                continue;
-            }
-            if (lbr) {
-                eret += "\n" + indent;
-            }
-            lbr = false;
-            eret += tok.val;
-            
-            
-       }
-       return eret;
-        */
+            ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ") +
+        ")\n{\n\n}";
     }
-
+       
     
 };