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.TokenReader.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             var ts = new TokenStream(txs);
137         
138             Parser.parse(ts, srcFile);
139               
140             //var outstr = JSON.stringify(
141             //    Parser.filesSymbols[srcFile]._index
142             //);
143             //File.write(cacheFile, outstr);
144              
145                 
146     //          }
147         }
148         
149         
150         
151         Parser.finish();
152     },
153     
154      
155         
156     publish  : function() {
157         
158          
159         // link!!!
160         
161         
162         
163         if (!File.exists(Options.target))
164             File.mkdir(Options.target);
165         if (!File.exists(Options.target+"/symbols"))
166             File.mkdir(Options.target+"/symbols");
167         if (!File.exists(Options.target+"/symbols/src"))
168             File.mkdir(Options.target+"/symbols/src");
169         
170         // copy everything in 'static' into 
171         File.list(Options.templatesDir + '/static').forEach(function (f) {
172             File.copy(Options.templatesDir + '/static/' + f, Options.target + '/' + f);
173         });
174         if (!File.isDirectory(Options.target +"/json")) {
175             File.makeDir(Options.target +"/json");
176         }
177         
178         // used to check the details of things being linked to
179         Link.symbolSet = symbolSet;
180         Link.base = "../";
181         
182         var classTemplate = new Template({
183              templateFile : Options.templatesDir  + "/class.tmpl",
184              Link : Link
185         });
186         var classesTemplate = new Template({
187             templateFile : Options.templatesDir +"/allclasses.tmpl",
188             Link : Link
189         });
190         var classesindexTemplate = new Template({
191             templateFile : Options.templatesDir +"/index.tmpl",
192             Link : Link
193         });
194         var fileindexTemplate = new Template({   
195             templateFile : Options.templatesDir +"/allfiles.tmpl",
196             Link: Link
197         });
198
199         
200         classTemplate.symbolSet = symbolSet;
201         
202         
203         function hasNoParent($) {
204             return ($.memberOf == "")
205         }
206         function isaFile($) {
207             return ($.is("FILE"))
208         }
209         function isaClass($) { 
210             return ($.is("CONSTRUCTOR") || $.isNamespace); 
211         }
212         
213         var symbols = symbolSet.toArray();
214         
215         var files = Options.srcFiles;
216         
217         for (var i = 0, l = files.length; i < l; i++) {
218             var file = files[i];
219             var targetDir = Options.target + "/symbols/src/";
220             this.makeSrcFile(file, targetDir);
221         }
222         
223         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
224          
225        var 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         var classesIndex = classesTemplate.process(classes);
249         
250           
251         
252         File.write(Options.target +  "/index."+ Options.publishExt, 
253             classesindexTemplate.process(classes)
254         );
255         
256         // blank everything???? classesindexTemplate = classesIndex = classes = null;
257         
258  
259         
260         var documentedFiles = symbols.filter(function ($) {
261             return ($.is("FILE"))
262         });
263         
264         var allFiles = [];
265         
266         for (var i = 0; i < files.length; i++) {
267             allFiles.push(new  Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
268         }
269         
270         for (var i = 0; i < documentedFiles.length; i++) {
271             var offset = files.indexOf(documentedFiles[i].alias);
272             allFiles[offset] = documentedFiles[i];
273         }
274             
275         allFiles = allFiles.sort(makeSortby("name"));
276         File.write(Options.target , "/files."+Options.publishExt, 
277             fileindexTemplate.process(allFiles)
278         );
279         
280     },
281
282     publishJSON : function(file, data)
283     {
284         // what we need to output to be usefull...
285         // a) props..
286         var cfgProperties = [];
287         if (!data.comment.getTag('singleton').length) {
288             cfgProperties = data.configToArray();
289             cfgProperties = cfgProperties.sort(makeSortby("name"));
290             
291         }
292         var props = []; 
293         //println(cfgProperties.toSource());
294         var p ='';
295         for(var i =0; i < cfgProperties.length;i++) {
296             p = cfgProperties[i];
297             props.push( {
298                 name : p.name,
299                 type : p.type,
300                 desc : p.desc,
301                 memberOf : p.memberOf == data.alias ? '' : p.memberOf
302             });
303         }
304         
305          
306         var ownEvents = data.methods.filter( function(e){
307                 return e.isEvent && !e.comment.getTag('hide').length;
308             }).sort(makeSortby("name"));
309              
310         
311         var events = [];
312         var m;
313         for(var i =0; i < ownEvents.length;i++) {
314             m = ownEvents[i];
315             events.push( {
316                 name : m.name.substring(1),
317                 sig : makeFuncSkel(m.params),
318                 type : 'function',
319                 desc : m.desc
320             });
321         }
322         //println(props.toSource());
323         // we need to output:
324         //classname => {
325         //    propname => 
326         //        type=>
327         //        desc=>
328         //    }
329
330         var ret = {
331             props : props,
332             events: events
333         };
334         File.write(file, JSON.stringify(ret, null, 2 ));
335         
336         
337         // b) methods
338         // c) events
339         
340         
341     },
342     makeSrcFile: function(sourceFile) 
343     {
344         
345         
346         name = sourceFile.substring(Options.baseDir.length);
347         name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
348         name = name.replace(/\:/g, "_"); //??
349         
350         
351         var pretty = imports.PrettyPrint.toPretty(File.read(sourceFile));
352         File.write(Options.target+"/symbols/src/" + name, 
353             '<html><head>' +
354             '<title>' + sourceFile + '</title>' +
355             '<link rel="stylesheet" type="text/css" href="../../../highlight-js.css"/>' + 
356             '</head><body class="highlightpage">' +
357             pretty +
358             '</body></html>');
359     }
360      
361     
362 };
363   
364
365
366
367
368
369  
370
371
372
373