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