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