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