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
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         
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             this.srcFiles = this.srcFiles.concat(
71             
72                 File.list(Options.src[i] ).filter(
73                     function($) {
74                         var thisExt = $.split(".").pop().toLowerCase();
75                         return (ext.indexOf(thisExt) > -1); // || thisExt in JSDOC.handlers);
76                             // we're only interested in files with certain extensions
77                     }
78                 )
79             );
80         }
81         
82         return this.srcFiles;
83     },
84
85     _parseSrcFiles : function() 
86     {
87         Parser.init();
88         
89         for (var i = 0, l = this.srcFiles.length; i < l; i++) {
90             
91             var srcFile = this.srcFiles[i];
92             
93             
94             var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
95             
96             //println(cacheFile);
97             // disabled at present!@!!
98             
99             if (false && !Options.disablecache  && File.exists(cacheFile)) {
100                 // check filetime?
101                 
102                 var c_mt = File.getTimes(cacheFile);
103                 var o_mt = File.getTimes(srcFile);
104                 //println(c_mt.toSource());
105                // println(o_mt.toSource());
106                
107                 // this check does not appear to work according to the doc's - need to check it out.
108                
109                 if (c_mt[0] > o_mt[0]) { // cached time  > original time!
110                     // use the cached mtimes..
111                     var syms =  JSON.parse(File.read(cacheFile));
112                     
113                     throw "Conversion of cache not done yet!";
114                     
115                     for (var sy in syms) {
116                         //println("ADD:" + sy );
117                        Parser.symbols.addSymbol(syms[sy]);
118                     }
119                     continue;
120                 }
121             }
122             
123             var src = ''
124             try {
125                 src = File.read(srcFile);
126             }
127             catch(e) {
128                 LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
129                 continue;
130             }
131
132             var txs = new TextStream(src);
133             
134             var tr = new TokenReader({ keepComments : true, keepWhite : true });
135             var ts = new TokenStream(txs);
136         
137             Parser.parse(ts, srcFile);
138               
139             //var outstr = JSON.stringify(
140             //    Parser.filesSymbols[srcFile]._index
141             //);
142             //File.write(cacheFile, outstr);
143              
144                 
145     //          }
146         }
147         
148         
149         
150         Parser.finish();
151     },
152     
153      
154         
155     publish  : function() {
156         
157          
158         // link!!!
159         
160         
161         
162         if (!File.exists(Options.target))
163             File.mkdir(Options.target);
164         if (!File.exists(Options.target+"/symbols"))
165             File.mkdir(Options.target+"/symbols");
166         if (!File.exists(Options.target+"/symbols/src"))
167             File.mkdir(Options.target+"/symbols/src");
168         
169         // copy everything in 'static' into 
170         File.list(Options.templatesDir + '/static').forEach(function (f) {
171             File.copy(Options.templatesDir + '/static/' + f, Options.target + '/' + f);
172         });
173         if (!File.isDirectory(Options.target +"/json")) {
174             File.makeDir(Options.target +"/json");
175         }
176         
177         // used to check the details of things being linked to
178         Link.symbolSet = symbolSet;
179         Link.base = "../";
180         
181         var classTemplate = new Template({
182              templateFile : Options.templatesDir  + "/class.tmpl",
183              Link : Link
184         });
185         var classesTemplate = new Template({
186             templateFile : Options.templatesDir +"/allclasses.tmpl",
187             Link : Link
188         });
189         var classesindexTemplate = new Template({
190             templateFile : Options.templatesDir +"/index.tmpl",
191             Link : Link
192         });
193         var fileindexTemplate = new Template({   
194             templateFile : Options.templatesDir +"/allfiles.tmpl",
195             Link: Link
196         });
197
198         
199         classTemplate.symbolSet = symbolSet;
200         
201         
202         function hasNoParent($) {
203             return ($.memberOf == "")
204         }
205         function isaFile($) {
206             return ($.is("FILE"))
207         }
208         function isaClass($) { 
209             return ($.is("CONSTRUCTOR") || $.isNamespace); 
210         }
211         
212         var symbols = symbolSet.toArray();
213         
214         var files = Options.srcFiles;
215         
216         for (var i = 0, l = files.length; i < l; i++) {
217             var file = files[i];
218             var targetDir = Options.target + "/symbols/src/";
219             this.makeSrcFile(file, targetDir);
220         }
221         
222         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
223          
224        var classesIndex = classesTemplate.process(classes); // kept in memory
225         
226         
227         
228         for (var i = 0, l = classes.length; i < l; i++) {
229             var symbol = classes[i];
230             var output = "";
231             
232             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
233                     classTemplate.process(symbol));
234             
235             print("write " + Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt);
236             
237             // dump out a 
238             
239             this.publishJSON(Options.target+"/json/",  symbol.alias+'.json', symbol)
240             
241             
242             
243         }
244         
245         // regenrate the index with different relative links
246         Link.base = "";
247         var classesIndex = classesTemplate.process(classes);
248         
249           
250         
251         File.write(Options.target +  "/index."+ Options.publishExt, 
252             classesindexTemplate.process(classes)
253         );
254         
255         // blank everything???? classesindexTemplate = classesIndex = classes = null;
256         
257  
258         
259         var documentedFiles = symbols.filter(function ($) {
260             return ($.is("FILE"))
261         });
262         
263         var allFiles = [];
264         
265         for (var i = 0; i < files.length; i++) {
266             allFiles.push(new  Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
267         }
268         
269         for (var i = 0; i < documentedFiles.length; i++) {
270             var offset = files.indexOf(documentedFiles[i].alias);
271             allFiles[offset] = documentedFiles[i];
272         }
273             
274         allFiles = allFiles.sort(makeSortby("name"));
275         File.write(Options.target , "/files."+Options.publishExt, 
276             fileindexTemplate.process(allFiles)
277         );
278         
279     },
280
281     publishJSON : function(file, data)
282     {
283         // what we need to output to be usefull...
284         // a) props..
285         var cfgProperties = [];
286         if (!data.comment.getTag('singleton').length) {
287             cfgProperties = data.configToArray();
288             cfgProperties = cfgProperties.sort(makeSortby("name"));
289             
290         }
291         var props = []; 
292         //println(cfgProperties.toSource());
293         var p ='';
294         for(var i =0; i < cfgProperties.length;i++) {
295             p = cfgProperties[i];
296             props.push( {
297                 name : p.name,
298                 type : p.type,
299                 desc : p.desc,
300                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
301             });
302         }
303         
304          
305         var ownEvents = data.methods.filter( function(e){
306                 return e.isEvent && !e.comment.getTag('hide').length;
307             }).sort(makeSortby("name"));
308              
309         
310         var events = [];
311         var m;
312         for(var i =0; i < ownEvents.length;i++) {
313             m = ownEvents[i];
314             events.push( {
315                 name : m.name.substring(1),
316                 sig : makeFuncSkel(m.params),
317                 type : 'function',
318                 desc : m.desc
319             });
320         }
321         //println(props.toSource());
322         // we need to output:
323         //classname => {
324         //    propname => 
325         //        type=>
326         //        desc=>
327         //    }
328
329         var ret = {
330             props : props,
331             events: events
332         };
333         File.write(file, JSON.stringify(ret, null, 2 ));
334         
335         
336         // b) methods
337         // c) events
338         
339         
340     },
341     makeSrcFile: function(sourceFile) 
342     {
343         
344         
345         name = sourceFile.substring(Options.baseDir.length);
346         name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
347         name = name.replace(/\:/g, "_"); //??
348         
349         
350         var pretty = imports.PrettyPrint.toPretty(File.read(sourceFile));
351         File.write(Options.target+"/symbols/src/" + name, 
352             '<html><head>' +
353             '<title>' + sourceFile + '</title>' +
354             '<link rel="stylesheet" type="text/css" href="../../../highlight-js.css"/>' + 
355             '</head><body class="highlightpage">' +
356             pretty +
357             '</body></html>');
358     }
359      
360     
361 };
362   
363
364
365
366
367
368  
369
370
371
372