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
7
8 XObject = imports.XObject.XObject;
9 File = imports.File.File;
10
11 Template = imports.JsTemplate.Template.Template;
12
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);
183         });
184         
185         
186         Options.LOG.inform("Setting up templates");
187         // used to check the details of things being linked to
188         Link.symbolSet = 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 = 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         
222         var symbols = symbolSet.toArray();
223         
224         var files = Options.srcFiles;
225         
226         for (var i = 0, l = files.length; i < l; i++) {
227             var file = files[i];
228             var targetDir = Options.target + "/symbols/src/";
229             this.makeSrcFile(file, targetDir);
230         }
231         
232         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
233          
234        var classesIndex = classesTemplate.process(classes); // kept in memory
235         
236         
237         
238         for (var i = 0, l = classes.length; i < l; i++) {
239             var symbol = classes[i];
240             var output = "";
241             
242             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
243                     classTemplate.process(symbol));
244             
245             print("write " + Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt);
246             
247             // dump out a 
248             
249             this.publishJSON(Options.target+"/json/",  symbol.alias+'.json', symbol)
250             
251             
252             
253         }
254         
255         // regenrate the index with different relative links
256         Link.base = "";
257         var classesIndex = classesTemplate.process(classes);
258         
259           
260         
261         File.write(Options.target +  "/index."+ Options.publishExt, 
262             classesindexTemplate.process(classes)
263         );
264         
265         // blank everything???? classesindexTemplate = classesIndex = classes = null;
266         
267  
268         
269         var documentedFiles = symbols.filter(function ($) {
270             return ($.is("FILE"))
271         });
272         
273         var allFiles = [];
274         
275         for (var i = 0; i < files.length; i++) {
276             allFiles.push(new  Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
277         }
278         
279         for (var i = 0; i < documentedFiles.length; i++) {
280             var offset = files.indexOf(documentedFiles[i].alias);
281             allFiles[offset] = documentedFiles[i];
282         }
283             
284         allFiles = allFiles.sort(makeSortby("name"));
285         File.write(Options.target , "/files."+Options.publishExt, 
286             fileindexTemplate.process(allFiles)
287         );
288         
289     },
290
291     publishJSON : function(file, data)
292     {
293         // what we need to output to be usefull...
294         // a) props..
295         var cfgProperties = [];
296         if (!data.comment.getTag('singleton').length) {
297             cfgProperties = data.configToArray();
298             cfgProperties = cfgProperties.sort(makeSortby("name"));
299             
300         }
301         var props = []; 
302         //println(cfgProperties.toSource());
303         var p ='';
304         for(var i =0; i < cfgProperties.length;i++) {
305             p = cfgProperties[i];
306             props.push( {
307                 name : p.name,
308                 type : p.type,
309                 desc : p.desc,
310                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
311             });
312         }
313         
314          
315         var ownEvents = data.methods.filter( function(e){
316                 return e.isEvent && !e.comment.getTag('hide').length;
317             }).sort(makeSortby("name"));
318              
319         
320         var events = [];
321         var m;
322         for(var i =0; i < ownEvents.length;i++) {
323             m = ownEvents[i];
324             events.push( {
325                 name : m.name.substring(1),
326                 sig : makeFuncSkel(m.params),
327                 type : 'function',
328                 desc : m.desc
329             });
330         }
331         //println(props.toSource());
332         // we need to output:
333         //classname => {
334         //    propname => 
335         //        type=>
336         //        desc=>
337         //    }
338
339         var ret = {
340             props : props,
341             events: events
342         };
343         File.write(file, JSON.stringify(ret, null, 2 ));
344         
345         
346         // b) methods
347         // c) events
348         
349         
350     },
351     makeSrcFile: function(sourceFile) 
352     {
353         
354         
355         name = sourceFile.substring(Options.baseDir.length);
356         name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
357         name = name.replace(/\:/g, "_"); //??
358         
359         
360         var pretty = imports.PrettyPrint.toPretty(File.read(sourceFile));
361         File.write(Options.target+"/symbols/src/" + name, 
362             '<html><head>' +
363             '<title>' + sourceFile + '</title>' +
364             '<link rel="stylesheet" type="text/css" href="../../../highlight-js.css"/>' + 
365             '</head><body class="highlightpage">' +
366             pretty +
367             '</body></html>');
368     }
369      
370     
371 };
372   
373
374
375
376
377
378  
379
380
381
382