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         
177         
178         // used to check the details of things being linked to
179         Link.symbolSet = symbolSet;
180
181         
182         var classTemplate = new Template(publish.conf.templatesDir+"class.tmpl");
183         var classesTemplate = new Template(publish.conf.templatesDir+"allclasses.tmpl");
184         //}
185         //atch(e) {
186         //      print(e.message);
187         ///     quit();
188         //}
189         
190         // filters
191         
192         classTemplate.symbolSet = symbolSet;
193         
194         
195         function hasNoParent($) {return ($.memberOf == "")}
196         function isaFile($) {return ($.is("FILE"))}
197         function isaClass($) { return ($.is("CONSTRUCTOR") || $.isNamespace); }
198         
199         var symbols = symbolSet.toArray();
200         
201         var files = JSDOC.opt.srcFiles;
202         for (var i = 0, l = files.length; i < l; i++) {
203             var file = files[i];
204             var srcDir = publish.conf.outDir + "symbols/src/";
205             makeSrcFile(file, srcDir);
206         }
207         
208         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
209         
210         Link.base = "../";
211         publish.classesIndex = classesTemplate.process(classes); // kept in memory
212         
213         IO.makeDir(publish.conf.outDir+"json");
214         
215         for (var i = 0, l = classes.length; i < l; i++) {
216             var symbol = classes[i];
217             var output = "";
218             
219             output = classTemplate.process(symbol);
220             println("write " + publish.conf.outDir+"symbols/" +symbol.alias+publish.conf.ext);
221             IO.saveFile(publish.conf.outDir+"symbols/", symbol.alias+publish.conf.ext, output);
222             // dump out a 
223             IO.saveFile(publish.conf.outDir+"json/",  symbol.alias+'.json' , publish.jsonRender(symbol));
224             
225             
226         }
227         
228         // regenrate the index with different relative links
229         Link.base = "";
230         publish.classesIndex = classesTemplate.process(classes);
231         
232         try {
233             var classesindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"index.tmpl");
234         }
235         catch(e) { print(e.message); quit(); }
236         
237         var classesIndex = classesindexTemplate.process(classes);
238         IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
239         classesindexTemplate = classesIndex = classes = null;
240         
241         try {
242             var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
243         }
244         catch(e) { print(e.message); quit(); }
245         
246         var documentedFiles = symbols.filter(isaFile);
247         var allFiles = [];
248         
249         for (var i = 0; i < files.length; i++) {
250             allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
251         }
252         
253         for (var i = 0; i < documentedFiles.length; i++) {
254             var offset = files.indexOf(documentedFiles[i].alias);
255             allFiles[offset] = documentedFiles[i];
256         }
257             
258         allFiles = allFiles.sort(makeSortby("name"));
259
260         var filesIndex = fileindexTemplate.process(allFiles);
261         IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
262         fileindexTemplate = filesIndex = files = null;
263     }
264
265      
266      
267     
268 };
269   
270
271
272
273
274
275  
276
277
278
279