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