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