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