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
14 Parser   = imports.Parser.Parser;
15 TextStream = imports.TextStream.TextStream;
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 Options = false; // refer to this everywhere!
25
26
27 BuildDocs = {
28     
29     VERSION : "2.0.0",
30     
31     
32     srcFiles : [],
33     
34     
35     build : function (opts)
36     {
37         Options = opts; 
38         Options.init();
39         
40         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
41         //Options.LOG.inform("With options: ");
42         
43         if (Options.cacheDirectory.length && !File.isDirectory(Options.cacheDirectory)) {   
44             File.mkdir(Options.cacheDirectory)
45         }
46         
47         Options.srcFiles = this._getSrcFiles();
48         this._parseSrcFiles();
49         this.symbolSet = Parser.symbols;
50         
51         // this currently uses the concept of publish.js...
52              
53         this.publish();
54          
55         
56         
57     },
58     
59     
60     _getSrcFiles : function() 
61     {
62         this.srcFiles = [];
63         
64         var ext = ["js"];
65         if (Options.ext) {
66             ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
67         }
68         
69         for (var i = 0; i < Options.src.length; i++) {
70             this.srcFiles = this.srcFiles.concat(
71             
72                 File.list(Options.src[i] ).filter(
73                     function($) {
74                         var thisExt = $.split(".").pop().toLowerCase();
75                         return (ext.indexOf(thisExt) > -1); // || thisExt in JSDOC.handlers);
76                             // we're only interested in files with certain extensions
77                     }
78                 )
79             );
80         }
81         
82         return this.srcFiles;
83     },
84
85     _parseSrcFiles : function() 
86     {
87         Parser.init();
88         
89         for (var i = 0, l = this.srcFiles.length; i < l; i++) {
90             
91             var srcFile = this.srcFiles[i];
92             
93             
94             var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
95             
96             //println(cacheFile);
97             // disabled at present!@!!
98             
99             if (false && !Options.disablecache  && File.exists(cacheFile)) {
100                 // check filetime?
101                 
102                 var c_mt = File.getTimes(cacheFile);
103                 var o_mt = File.getTimes(srcFile);
104                 //println(c_mt.toSource());
105                // println(o_mt.toSource());
106                
107                 // this check does not appear to work according to the doc's - need to check it out.
108                
109                 if (c_mt[0] > o_mt[0]) { // cached time  > original time!
110                     // use the cached mtimes..
111                     var syms =  JSON.parse(File.read(cacheFile));
112                     
113                     throw "Conversion of cache not done yet!";
114                     
115                     for (var sy in syms) {
116                         //println("ADD:" + sy );
117                        Parser.symbols.addSymbol(syms[sy]);
118                     }
119                     continue;
120                 }
121             }
122             
123             var src = ''
124             try {
125                 Options.LOG.warn("reading : " + srcFile);
126                 src = File.read(srcFile);
127             }
128             catch(e) {
129                 LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
130                 continue;
131             }
132
133             var txs = new TextStream(src);
134             
135             var tr = new TokenReader({ keepComments : true, keepWhite : true });
136             
137             var ts = new TokenStream(tr.tokenize(txs));
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