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