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