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