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         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
44         //Options.LOG.inform("With options: ");
45         
46         if (!File.isDirectory(Options.cacheDirectory)) {   
47             File.mkdir(Options.cacheDirectory)
48         }
49         
50         Options.srcFiles = this._getSrcFiles();
51         this._parseSrcFiles();
52         this.symbolSet = Parser.symbols;
53         
54         // this currently uses the concept of publish.js...
55              
56         this.publish();
57          
58         
59         
60     },
61     
62     
63     _getSrcFiles : function() 
64     {
65         this.srcFiles = [];
66         
67         var ext = ["js"];
68         if (Options.ext) {
69             ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
70         }
71         
72         for (var i = 0; i < Options.src.length; i++) {
73             this.srcFiles = this.srcFiles.concat(
74             
75                 File.list(Options.src[i] ).filter(
76                     function($) {
77                         var thisExt = $.split(".").pop().toLowerCase();
78                         return (ext.indexOf(thisExt) > -1); // || thisExt in JSDOC.handlers);
79                             // we're only interested in files with certain extensions
80                     }
81                 )
82             );
83         }
84         
85         return this.srcFiles;
86     },
87
88     _parseSrcFiles : function() 
89     {
90         Parser.init();
91         
92         for (var i = 0, l = this.srcFiles.length; i < l; i++) {
93             
94             var srcFile = this.srcFiles[i];
95             
96             
97             var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
98             
99             //println(cacheFile);
100             // disabled at present!@!!
101             
102             if (false && !Options.disablecache  && File.exists(cacheFile)) {
103                 // check filetime?
104                 
105                 var c_mt = File.getTimes(cacheFile);
106                 var o_mt = File.getTimes(srcFile);
107                 //println(c_mt.toSource());
108                // println(o_mt.toSource());
109                
110                 // this check does not appear to work according to the doc's - need to check it out.
111                
112                 if (c_mt[0] > o_mt[0]) { // cached time  > original time!
113                     // use the cached mtimes..
114                     var syms =  JSON.parse(File.read(cacheFile));
115                     
116                     throw "Conversion of cache not done yet!";
117                     
118                     for (var sy in syms) {
119                         //println("ADD:" + sy );
120                        Parser.symbols.addSymbol(syms[sy]);
121                     }
122                     continue;
123                 }
124             }
125             
126             var src = ''
127             try {
128                 src = File.read(srcFile);
129             }
130             catch(e) {
131                 LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
132                 continue;
133             }
134
135              
136             var tr = new TokenReader();
137             var ts = new TokenStream(tr.tokenize(src));
138         
139             Parser.parse(ts, srcFile);
140               
141             //var outstr = JSON.stringify(
142             //    Parser.filesSymbols[srcFile]._index
143             //);
144             //File.write(cacheFile, outstr);
145              
146                 
147     //          }
148         }
149         
150         
151         
152         Parser.finish();
153     }
154     
155      
156         
157     publish  : function() {
158         
159          
160         // link!!!
161         
162         
163         
164         if (!File.exists(Options.target))
165             File.mkdir(Options.target);
166         if (!File.exists(Options.target+"/symbols"))
167             File.mkdir(Options.target+"/symbols");
168         if (!File.exists(Options.target+"/symbols/src"))
169             File.mkdir(Options.target+"/symbols/src");
170         
171         // copy everything in 'static' into 
172         File.list(Options.templatesDir + '/static').forEach(function (f) {
173             File.copy(Options.templatesDir + '/static/' + f, Options.target + '/' + f);
174         });
175         if (!File.isDirectory(Options.target +"/json")) {
176             File.makeDir(Options.target +"/json");
177         }
178         
179         // used to check the details of things being linked to
180         Link.symbolSet = symbolSet;
181         Link.base = "../";
182         
183         var classTemplate = new Template({
184              templateFile : Options.templatesDir  + "/class.tmpl",
185              Link : Link
186         });
187         var classesTemplate = new Template({
188             templateFile : Options.templatesDir +"/allclasses.tmpl",
189             Link : Link
190         });
191         var classesindexTemplate = new Template({
192             templateFile : Options.templatesDir +"/index.tmpl",
193             Link : Link
194         });
195         var fileindexTemplate = new Template({   
196             templateFile : Options.templatesDir +"/allfiles.tmpl",
197             Link: Link
198         });
199
200         
201         classTemplate.symbolSet = symbolSet;
202         
203         
204         function hasNoParent($) {
205             return ($.memberOf == "")
206         }
207         function isaFile($) {
208             return ($.is("FILE"))
209         }
210         function isaClass($) { 
211             return ($.is("CONSTRUCTOR") || $.isNamespace); 
212         }
213         
214         var symbols = symbolSet.toArray();
215         
216         var files = Options.srcFiles;
217         
218         for (var i = 0, l = files.length; i < l; i++) {
219             var file = files[i];
220             var targetDir = Options.target + "/symbols/src/";
221             this.makeSrcFile(file, targetDir);
222         }
223         
224         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
225          
226        var classesIndex = classesTemplate.process(classes); // kept in memory
227         
228         
229         
230         for (var i = 0, l = classes.length; i < l; i++) {
231             var symbol = classes[i];
232             var output = "";
233             
234             File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
235                     classTemplate.process(symbol));
236             
237             print("write " + Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt);
238             
239             // dump out a 
240             
241             this.publishJSON(Options.target+"/json/",  symbol.alias+'.json', symbol)
242             
243             
244             
245         }
246         
247         // regenrate the index with different relative links
248         Link.base = "";
249         var classesIndex = classesTemplate.process(classes);
250         
251           
252         
253         File.write(Options.target +  "/index."+ Options.publishExt, 
254             classesindexTemplate.process(classes)
255         );
256         
257         // blank everything???? classesindexTemplate = classesIndex = classes = null;
258         
259  
260         
261         var documentedFiles = symbols.filter(function ($) {
262             return ($.is("FILE"))
263         });
264         
265         var allFiles = [];
266         
267         for (var i = 0; i < files.length; i++) {
268             allFiles.push(new  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         File.write(Options.target , "/files."+Options.publishExt, 
278             fileindexTemplate.process(allFiles)
279         );
280         
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(sourceFile) 
344     {
345         
346         
347         name = sourceFile.substring(Options.baseDir.length);
348         name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
349         name = name.replace(/\:/g, "_"); //??
350         
351         
352         var pretty = imports.PrettyPrint.toPretty(File.read(sourceFile));
353         File.write(Options.target+"/symbols/src" + name, 
354             '<html><head>' +
355             '<title>' + sourceFile + '</title>' +
356             '<link rel="stylesheet" type="text/css" href="../../../highlight-js.css"/>' + 
357             '</head><body class="highlightpage">' +
358             pretty +
359             '</body></html>');
360     }
361      
362     
363 };
364   
365
366
367
368
369
370  
371
372
373
374