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 Options = imports.Options.Options;
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
25
26
27 BuildDocs = {
28     
29     VERSION : "2.0.0",
30     
31     
32     srcFiles : [],
33     
34     build : function (opts)
35     {
36         
37         XObject.extend(Options, opts);
38          
39         Options.init();
40         
41     
42          
43         
44         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
45         Options.LOG.inform("With options: ");
46         
47         if (!File.isDirectory(Options.cacheDirectory)) {   
48             File.mkdir(Options.cacheDirectory)
49         }
50         
51         Options.srcFiles = this._getSrcFiles();
52         this._parseSrcFiles();
53         this.symbolSet = Parser.symbols;
54         
55         // this currently uses the concept of publish.js...
56              
57         this.publish();
58          
59         
60         
61     },
62     
63     
64     _getSrcFiles : function() 
65     {
66         this.srcFiles = [];
67         
68         var ext = ["js"];
69         if (Options.ext) {
70             ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
71         }
72         
73         for (var i = 0; i < Options.src.length; i++) {
74             this.srcFiles = this.srcFiles.concat(
75             
76                 File.list(Options.src[i] ).filter(
77                     function($) {
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                 src = File.read(srcFile);
130             }
131             catch(e) {
132                 LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
133                 continue;
134             }
135
136              
137             var tr = new TokenReader();
138             var ts = new TokenStream(tr.tokenize(src));
139         
140             Parser.parse(ts, srcFile);
141               
142             //var outstr = JSON.stringify(
143             //    Parser.filesSymbols[srcFile]._index
144             //);
145             //File.write(cacheFile, outstr);
146              
147                 
148     //          }
149         }
150         
151         
152         
153         Parser.finish();
154     }
155     
156      
157         
158     publish  : function() {
159         
160          
161         // link!!!
162         
163         
164         
165         if (!File.exists(Options.target))
166             File.mkdir(Options.target);
167         if (!File.exists(Options.target+"/symbols"))
168             File.mkdir(Options.target+"/symbols");
169         if (!File.exists(Options.target+"/symbols/src"))
170             File.mkdir(Options.target+"/symbols/src");
171         
172         // copy everything in 'static' into 
173         File.list(Options.templatesDir + '/static').forEach(function (f) {
174             File.copy(Options.templatesDir + '/static/' + f, Options.target + '/' + f);
175         });
176         if (!File.isDirectory(Options.target +"/json")) {
177             File.makeDir(Options.target +"/json");
178         }
179         
180         // used to check the details of things being linked to
181         Link.symbolSet = symbolSet;
182
183         
184         var classTemplate = new Template({
185              templateFile : Options.templatesDir  + "/class.tmpl",
186              Link : Link
187         });
188         var classesTemplate = new Template(
189             templateFile : Options.templatesDir +"/allclasses.tmpl",
190             Link : Link
191         );
192         
193         classTemplate.symbolSet = symbolSet;
194         
195         
196         function hasNoParent($) {
197             return ($.memberOf == "")
198         }
199         function isaFile($) {
200             return ($.is("FILE"))
201         }
202         function isaClass($) { 
203             return ($.is("CONSTRUCTOR") || $.isNamespace); 
204         }
205         
206         var symbols = symbolSet.toArray();
207         
208         var files = Options.srcFiles;
209         
210         for (var i = 0, l = files.length; i < l; i++) {
211             var file = files[i];
212             var srcDir = Options.target + "/symbols/src/";
213             this.makeSrcFile(file, srcDir);
214         }
215         
216         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
217         
218         Link.base = "../";
219         
220         publish.classesIndex = classesTemplate.process(classes); // kept in memory
221         
222         
223         
224         for (var i = 0, l = classes.length; i < l; i++) {
225             var symbol = classes[i];
226             var output = "";
227             
228             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
229                     classTemplate.process(symbol));
230             
231             print("write " + Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt);
232             
233             // dump out a 
234             
235             this.publishJSON(Options.target+"/json/",  symbol.alias+'.json', symbol)
236             
237             
238             
239         }
240         
241         // regenrate the index with different relative links
242         Link.base = "";
243         publish.classesIndex = classesTemplate.process(classes);
244         
245         try {
246             var classesindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"index.tmpl");
247         }
248         catch(e) { print(e.message); quit(); }
249         
250         var classesIndex = classesindexTemplate.process(classes);
251         IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
252         classesindexTemplate = classesIndex = classes = null;
253         
254         try {
255             var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
256         }
257         catch(e) { print(e.message); quit(); }
258         
259         var documentedFiles = symbols.filter(isaFile);
260         var allFiles = [];
261         
262         for (var i = 0; i < files.length; i++) {
263             allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
264         }
265         
266         for (var i = 0; i < documentedFiles.length; i++) {
267             var offset = files.indexOf(documentedFiles[i].alias);
268             allFiles[offset] = documentedFiles[i];
269         }
270             
271         allFiles = allFiles.sort(makeSortby("name"));
272
273         var filesIndex = fileindexTemplate.process(allFiles);
274         IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
275         fileindexTemplate = filesIndex = files = null;
276     }
277
278     publishJSON = function(file, data)
279     {
280         // what we need to output to be usefull...
281         // a) props..
282         var cfgProperties = [];
283         if (!data.comment.getTag('singleton').length) {
284             cfgProperties = data.configToArray();
285             cfgProperties = cfgProperties.sort(makeSortby("name"));
286             
287         }
288         var props = []; 
289         //println(cfgProperties.toSource());
290         var p ='';
291         for(var i =0; i < cfgProperties.length;i++) {
292             p = cfgProperties[i];
293             props.push( {
294                 name : p.name,
295                 type : p.type,
296                 desc : p.desc,
297                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
298             });
299         }
300         
301          
302         var ownEvents = data.methods.filter( function(e){
303                 return e.isEvent && !e.comment.getTag('hide').length;
304             }).sort(makeSortby("name"));
305              
306         
307         var events = [];
308         var m;
309         for(var i =0; i < ownEvents.length;i++) {
310             m = ownEvents[i];
311             events.push( {
312                 name : m.name.substring(1),
313                 sig : makeFuncSkel(m.params),
314                 type : 'function',
315                 desc : m.desc
316             });
317         }
318         //println(props.toSource());
319         // we need to output:
320         //classname => {
321         //    propname => 
322         //        type=>
323         //        desc=>
324         //    }
325
326         var ret = {
327             props : props,
328             events: events
329         };
330         File.write(file, JSON.stringify(ret, null, 2 ));
331         
332         
333         // b) methods
334         // c) events
335         
336         
337     }
338     makeSrcFile: function(path, srcDir, name) {
339         if (Options.outputSource) return;
340         
341         return; // not done?
342         
343         if (!name) {
344             name = path.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
345             name = name.replace(/\:/g, "_");
346         }
347         
348         var src = {
349                 path: path, 
350                 name:name, 
351                 charset: IO.encoding, 
352                 hilited: ""
353         };
354         
355          
356         if (src.hilited) {
357             IO.saveFile(srcDir, name+publish.conf.ext, src.hilited);
358         }
359     }
360      
361     
362 };
363   
364
365
366
367
368
369  
370
371
372
373