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         var classesindexTemplate = new Template({
193             templateFile : Options.templatesDir +"/index.tmpl",
194             Link : Link
195         });
196         
197         
198         classTemplate.symbolSet = symbolSet;
199         
200         
201         function hasNoParent($) {
202             return ($.memberOf == "")
203         }
204         function isaFile($) {
205             return ($.is("FILE"))
206         }
207         function isaClass($) { 
208             return ($.is("CONSTRUCTOR") || $.isNamespace); 
209         }
210         
211         var symbols = symbolSet.toArray();
212         
213         var files = Options.srcFiles;
214         
215         for (var i = 0, l = files.length; i < l; i++) {
216             var file = files[i];
217             var srcDir = Options.target + "/symbols/src/";
218             this.makeSrcFile(file, srcDir);
219         }
220         
221         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
222         
223         Link.base = "../";
224         
225         publish.classesIndex = classesTemplate.process(classes); // kept in memory
226         
227         
228         
229         for (var i = 0, l = classes.length; i < l; i++) {
230             var symbol = classes[i];
231             var output = "";
232             
233             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
234                     classTemplate.process(symbol));
235             
236             print("write " + Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt);
237             
238             // dump out a 
239             
240             this.publishJSON(Options.target+"/json/",  symbol.alias+'.json', symbol)
241             
242             
243             
244         }
245         
246         // regenrate the index with different relative links
247         Link.base = "";
248         publish.classesIndex = classesTemplate.process(classes);
249         
250         try {
251             var classesindexTemplate = new JSDOC.JsPlate(Options.templatesDir+"index.tmpl");
252         }
253         catch(e) { print(e.message); quit(); }
254         
255         var classesIndex = classesindexTemplate.process(classes);
256         IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
257         classesindexTemplate = classesIndex = classes = null;
258         
259         try {
260             var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
261         }
262         catch(e) { print(e.message); quit(); }
263         
264         var documentedFiles = symbols.filter(isaFile);
265         var allFiles = [];
266         
267         for (var i = 0; i < files.length; i++) {
268             allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
269         }
270         
271         for (var i = 0; i < documentedFiles.length; i++) {
272             var offset = files.indexOf(documentedFiles[i].alias);
273             allFiles[offset] = documentedFiles[i];
274         }
275             
276         allFiles = allFiles.sort(makeSortby("name"));
277
278         var filesIndex = fileindexTemplate.process(allFiles);
279         IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
280         fileindexTemplate = filesIndex = files = null;
281     }
282
283     publishJSON = function(file, data)
284     {
285         // what we need to output to be usefull...
286         // a) props..
287         var cfgProperties = [];
288         if (!data.comment.getTag('singleton').length) {
289             cfgProperties = data.configToArray();
290             cfgProperties = cfgProperties.sort(makeSortby("name"));
291             
292         }
293         var props = []; 
294         //println(cfgProperties.toSource());
295         var p ='';
296         for(var i =0; i < cfgProperties.length;i++) {
297             p = cfgProperties[i];
298             props.push( {
299                 name : p.name,
300                 type : p.type,
301                 desc : p.desc,
302                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
303             });
304         }
305         
306          
307         var ownEvents = data.methods.filter( function(e){
308                 return e.isEvent && !e.comment.getTag('hide').length;
309             }).sort(makeSortby("name"));
310              
311         
312         var events = [];
313         var m;
314         for(var i =0; i < ownEvents.length;i++) {
315             m = ownEvents[i];
316             events.push( {
317                 name : m.name.substring(1),
318                 sig : makeFuncSkel(m.params),
319                 type : 'function',
320                 desc : m.desc
321             });
322         }
323         //println(props.toSource());
324         // we need to output:
325         //classname => {
326         //    propname => 
327         //        type=>
328         //        desc=>
329         //    }
330
331         var ret = {
332             props : props,
333             events: events
334         };
335         File.write(file, JSON.stringify(ret, null, 2 ));
336         
337         
338         // b) methods
339         // c) events
340         
341         
342     },
343     makeSrcFile: function(path, srcDir, name) {
344         if (Options.outputSource) return;
345         
346         return; // not done?
347         
348         if (!name) {
349             name = path.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
350             name = name.replace(/\:/g, "_");
351         }
352         
353         var src = {
354                 path: path, 
355                 name:name, 
356                 charset: IO.encoding, 
357                 hilited: ""
358         };
359         
360          
361         if (src.hilited) {
362             IO.saveFile(srcDir, name+publish.conf.ext, src.hilited);
363         }
364     }
365      
366     
367 };
368   
369
370
371
372
373
374  
375
376
377
378