9a0178c293bd3d76e3c0da340cffc844834f2ae3
[gnome.introspection-doc-generator] / JSDOC / BuildDocs.js
1 //<script type="text/javascript">
2 /**
3         This is the main container for the JSDOC application.
4         @namespace
5 */
6 Gio = imports.gi.Gio;
7
8 XObject = imports.XObject.XObject;
9 File = imports.File.File;
10
11 Template = imports.JsTemplate.Template.Template;
12 Link = imports.JsTemplate.Link.Link; // ?? fixme!??
13
14 Parser      = imports.Parser.Parser;
15 TextStream  = imports.TextStream.TextStream;
16 TokenReader = imports.TokenReader.TokenReader;
17 TokenStream = imports.TokenStream.TokenStream;
18 Symbol      = imports.Symbol.Symbol;
19 DocComment  = imports.DocComment.DocComment;
20
21 /******************    INCLUDES ARE ALL AT THE BOTTOM OF THIS FILE!!!!! *******************/
22
23 // should not realy be here -- or anywhere...??
24
25 function makeSortby(attribute) {
26     return function(a, b) {
27         if (a[attribute] != undefined && b[attribute] != undefined) {
28             a = a[attribute]; //.toLowerCase();
29             b = b[attribute];//.toLowerCase();
30             if (a < b) return -1;
31             if (a > b) return 1;
32             return 0;
33         }
34     }
35 }
36
37 Options = false; // refer to this everywhere!
38
39
40 BuildDocs = {
41     
42     VERSION : "2.0.0",
43     
44     
45     srcFiles : [],
46     
47     
48     build : function (opts)
49     {
50         Options = opts; 
51         Options.init();
52         
53         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
54         //Options.LOG.inform("With options: ");
55         
56         if (Options.cacheDirectory.length && !File.isDirectory(Options.cacheDirectory)) {   
57             File.mkdir(Options.cacheDirectory)
58         }
59         
60         Options.srcFiles = this._getSrcFiles();
61         this._parseSrcFiles();
62         this.symbolSet = Parser.symbols;
63         
64         // this currently uses the concept of publish.js...
65         
66         this.publish();
67          
68         
69         
70     },
71     
72     
73     _getSrcFiles : function() 
74     {
75         this.srcFiles = [];
76         var _this = this;
77         var ext = ["js"];
78         if (Options.ext) {
79             ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
80         }
81         
82         for (var i = 0; i < Options.src.length; i++) {
83             // add to sourcefiles..
84             
85             File.list(Options.src[i] ).forEach(function($) {
86                 if (Options['exclude-src'].indexOf($) > -1) {
87                     return;
88                 }
89                 var thisExt = $.split(".").pop().toLowerCase();
90                 if (ext.indexOf(thisExt) < 0) {
91                     return;
92                 }
93                 _this.srcFiles.push(Options.src[i] + '/' + $);
94             });
95                 
96         }
97         //Seed.print(JSON.stringify(this.srcFiles, null,4));Seed.quit();
98         return this.srcFiles;
99     },
100
101     _parseSrcFiles : function() 
102     {
103         Parser.init();
104         
105         for (var i = 0, l = this.srcFiles.length; i < l; i++) {
106             
107             var srcFile = this.srcFiles[i];
108             
109             
110             var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
111             
112             //println(cacheFile);
113             // disabled at present!@!!
114             
115             if (false && !Options.disablecache  && File.exists(cacheFile)) {
116                 // check filetime?
117                 
118                 var c_mt = File.getTimes(cacheFile);
119                 var o_mt = File.getTimes(srcFile);
120                 //println(c_mt.toSource());
121                // println(o_mt.toSource());
122                
123                 // this check does not appear to work according to the doc's - need to check it out.
124                
125                 if (c_mt[0] > o_mt[0]) { // cached time  > original time!
126                     // use the cached mtimes..
127                     var syms =  JSON.parse(File.read(cacheFile));
128                     
129                     throw "Conversion of cache not done yet!";
130                     
131                     for (var sy in syms) {
132                         //println("ADD:" + sy );
133                        Parser.symbols.addSymbol(syms[sy]);
134                     }
135                     continue;
136                 }
137             }
138             
139             var src = ''
140             try {
141                 Options.LOG.inform("reading : " + srcFile);
142                 src = File.read(srcFile);
143             }
144             catch(e) {
145                 Options.LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
146                 continue;
147             }
148
149             var txs = new TextStream(src);
150             
151             var tr = new TokenReader({ keepComments : true, keepWhite : true , sepIdents: false });
152             
153             var ts = new TokenStream(tr.tokenize(txs));
154         
155             Parser.parse(ts, srcFile);
156               
157             //var outstr = JSON.stringify(
158             //    Parser.filesSymbols[srcFile]._index
159             //);
160             //File.write(cacheFile, outstr);
161              
162                 
163     //          }
164         }
165         
166         
167         
168         Parser.finish();
169     },
170     
171      
172         
173     publish  : function() {
174         Options.LOG.inform("Publishing");
175          
176         // link!!!
177         
178         
179         Options.LOG.inform("Making directories");
180         if (!File.isDirectory(Options.target))
181             File.mkdir(Options.target);
182         if (!File.isDirectory(Options.target+"/symbols"))
183             File.mkdir(Options.target+"/symbols");
184         if (!File.isDirectory(Options.target+"/symbols/src"))
185             File.mkdir(Options.target+"/symbols/src");
186         
187         if (!File.isDirectory(Options.target +"/json")) {
188             File.mkdir(Options.target +"/json");
189         }
190         
191         Options.LOG.inform("Copying files from static: " +Options.templateDir);
192         // copy everything in 'static' into 
193         File.list(Options.templateDir + '/static').forEach(function (f) {
194             Options.LOG.inform("Copy " + Options.templateDir + '/static/' + f + ' to  ' + Options.target + '/' + f);
195             File.copyFile(Options.templateDir + '/static/' + f, Options.target + '/' + f,  Gio.FileCopyFlags.OVERWRITE);
196         });
197         
198         
199         Options.LOG.inform("Setting up templates");
200         // used to check the details of things being linked to
201         Link.symbolSet = this.symbolSet;
202         Link.base = "../";
203         
204         Link.srcFileFlatName = this.srcFileFlatName;
205         Link.srcFileRelName = this.srcFileRelName;
206         
207         var classTemplate = new Template({
208              templateFile : Options.templateDir  + "/class.html",
209              Link : Link
210         });
211         var classesTemplate = new Template({
212             templateFile : Options.templateDir +"/allclasses.html",
213             Link : Link
214         });
215         var classesindexTemplate = new Template({
216             templateFile : Options.templateDir +"/index.html",
217             Link : Link
218         });
219         var fileindexTemplate = new Template({   
220             templateFile : Options.templateDir +"/allfiles.html",
221             Link: Link
222         });
223
224         
225         classTemplate.symbolSet = this.symbolSet;
226         
227         
228         function hasNoParent($) {
229             return ($.memberOf == "")
230         }
231         function isaFile($) {
232             return ($.is("FILE"))
233         }
234         function isaClass($) { 
235             return ($.is("CONSTRUCTOR") || $.isNamespace); 
236         }
237         
238         
239         
240         
241         
242         
243         
244         
245         
246         
247         var symbols = this.symbolSet.toArray();
248         
249         var files = Options.srcFiles;
250         
251         for (var i = 0, l = files.length; i < l; i++) {
252             var file = files[i];
253             var targetDir = Options.target + "/symbols/src/";
254             this.makeSrcFile(file, targetDir);
255         }
256         
257         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
258          
259          //Options.LOG.inform("classTemplate Process : all classes");
260             
261        // var classesIndex = classesTemplate.process(classes); // kept in memory
262         
263         Options.LOG.inform("iterate classes");
264         
265         for (var i = 0, l = classes.length; i < l; i++) {
266             var symbol = classes[i];
267             var output = "";
268             
269             Options.LOG.inform("classTemplate Process : " + symbol.alias);
270             
271             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
272                     classTemplate.process(symbol));
273             
274             
275             
276             // dump out a 
277             
278             this.publishJSON(Options.target+"/json/" + symbol.alias+'.json', symbol)
279             
280             
281             
282         }
283         
284         // regenrate the index with different relative links
285         Link.base = "";
286         //var classesIndex = classesTemplate.process(classes);
287         
288         Options.LOG.inform("build index");
289         
290         File.write(Options.target +  "/index."+ Options.publishExt, 
291             classesindexTemplate.process(classes)
292         );
293         
294         // blank everything???? classesindexTemplate = classesIndex = classes = null;
295         
296  
297         
298         var documentedFiles = symbols.filter(function ($) {
299             return ($.is("FILE"))
300         });
301         
302         var allFiles = [];
303         
304         for (var i = 0; i < files.length; i++) {
305             allFiles.push(new  Symbol(files[i], [], "FILE", new DocComment("/** */")));
306         }
307         
308         for (var i = 0; i < documentedFiles.length; i++) {
309             var offset = files.indexOf(documentedFiles[i].alias);
310             allFiles[offset] = documentedFiles[i];
311         }
312             
313         allFiles = allFiles.sort(makeSortby("name"));
314         Options.LOG.inform("write files index");
315         
316         File.write(Options.target + "/files."+Options.publishExt, 
317             fileindexTemplate.process(allFiles)
318         );
319         
320     },
321     /**
322      * JSON files are lookup files for the documentation
323      * - can be used by IDE's or AJAX based doc tools
324      * 
325      * 
326      */
327     publishJSON : function(file, data)
328     {
329         // what we need to output to be usefull...
330         // a) props..
331         var cfgProperties = [];
332         if (!data.comment.getTag('singleton').length) {
333             cfgProperties = data.configToArray();
334             cfgProperties = cfgProperties.sort(makeSortby("name"));
335             
336         }
337         var props = []; 
338         //println(cfgProperties.toSource());
339         var p ='';
340         for(var i =0; i < cfgProperties.length;i++) {
341             p = cfgProperties[i];
342             props.push( {
343                 name : p.name,
344                 type : p.type,
345                 desc : p.desc,
346                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
347             });
348         }
349         
350          
351         var ownEvents = data.methods.filter( function(e){
352                 return e.isEvent && !e.comment.getTag('hide').length;
353             }).sort(makeSortby("name"));
354              
355         
356         var events = [];
357         var m;
358         for(var i =0; i < ownEvents.length;i++) {
359             m = ownEvents[i];
360             events.push( {
361                 name : m.name.substring(1),
362                 sig : this.makeFuncSkel(m.params),
363                 type : 'function',
364                 desc : m.desc
365             });
366         }
367         //println(props.toSource());
368         // we need to output:
369         //classname => {
370         //    propname => 
371         //        type=>
372         //        desc=>
373         //    }
374
375         var ret = {
376             props : props,
377             events: events
378         };
379         File.write(file, JSON.stringify(ret, null, 2 ));
380         
381         
382         // b) methods
383         // c) events
384         
385         
386     },
387     srcFileRelName : function(sourceFile)
388     {
389       return sourceFile.substring(Options.baseDir.length+1);
390     },
391     srcFileFlatName: function(sourceFile)
392     {
393         var name = this.srcFileRelName(sourceFile);
394         name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
395         return name.replace(/\:/g, "_") + '.html'; //??;
396         
397     },
398     
399     makeSrcFile: function(sourceFile) 
400     {
401         // this stuff works...
402      
403         
404         var name = this.srcFileFlatName(sourceFile);
405         
406         Options.LOG.inform("Write Source file : " + Options.target+"/symbols/src/" + name);
407         var pretty = imports.PrettyPrint.toPretty(File.read(  sourceFile));
408         File.write(Options.target+"/symbols/src/" + name, 
409             '<html><head>' +
410             '<title>' + sourceFile + '</title>' +
411             '<link rel="stylesheet" type="text/css" href="../../../css/highlight-js.css"/>' + 
412             '</head><body class="highlightpage">' +
413             pretty +
414             '</body></html>');
415     },
416     /**
417      * used by JSON output to generate a function skeleton
418      */
419     makeFuncSkel :function(params) {
420         if (!params) return "function ()\n{\n\n}";
421         return "function ("     +
422             params.filter(
423                 function($) {
424                     return $.name.indexOf(".") == -1; // don't show config params in signature
425                 }
426             ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ") +
427         ")\n{\n\n}";
428     }
429         
430  
431     
432 };
433   
434
435
436
437
438
439  
440
441
442
443