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 Options = imports.Options.Options;
12 Parser   = imports.Parser.Parser;
13
14 TokenReader = imports.TokenReader.TokenReader;
15 TokenStream = imports.TokenStream.TokenStream;
16 Symbol = imports.Symbol.Symbol;
17 /******************    INCLUDES ARE ALL AT THE BOTTOM OF THIS FILE!!!!! *******************/
18
19 // should not realy be here -- or anywhere...??
20
21
22
23
24
25 BuildDocs = {
26     
27     VERSION : "2.0.0",
28     
29     
30     srcFiles : [],
31     
32     build : function (opts)
33     {
34         
35         XObject.extend(Options, opts);
36          
37         Options.init();
38         
39     
40          
41         
42         Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
43         Options.LOG.inform("With options: ");
44         
45         if (!File.isDirectory(Options.cacheDirectory)) {   
46             File.mkdir(Options.cacheDirectory)
47         }
48         
49         Options.srcFiles = this._getSrcFiles();
50         this._parseSrcFiles();
51         this.symbolSet = Parser.symbols;
52         
53         // this currently uses the concept of publish.js...
54              
55         this.publish();
56          
57         
58         
59     },
60     
61     
62     _getSrcFiles : function() 
63     {
64         this.srcFiles = [];
65         
66         var ext = ["js"];
67         if (Options.ext) {
68             ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
69         }
70         
71         for (var i = 0; i < Options.src.length; i++) {
72             this.srcFiles = this.srcFiles.concat(
73             
74                 File.list(Options.src[i] ).filter(
75                     function($) {
76                         var thisExt = $.split(".").pop().toLowerCase();
77                         return (ext.indexOf(thisExt) > -1); // || thisExt in JSDOC.handlers);
78                             // we're only interested in files with certain extensions
79                     }
80                 )
81             );
82         }
83         
84         return this.srcFiles;
85     },
86
87     _parseSrcFiles : function() 
88     {
89         Parser.init();
90         
91         for (var i = 0, l = this.srcFiles.length; i < l; i++) {
92             
93             var srcFile = this.srcFiles[i];
94             
95             
96             var cacheFile = Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
97             
98             //println(cacheFile);
99             // disabled at present!@!!
100             
101             if (false && !Options.disablecache  && File.exists(cacheFile)) {
102                 // check filetime?
103                 
104                 var c_mt = File.getTimes(cacheFile);
105                 var o_mt = File.getTimes(srcFile);
106                 //println(c_mt.toSource());
107                // println(o_mt.toSource());
108                
109                 // this check does not appear to work according to the doc's - need to check it out.
110                
111                 if (c_mt[0] > o_mt[0]) { // cached time  > original time!
112                     // use the cached mtimes..
113                     var syms =  JSON.parse(File.read(cacheFile));
114                     
115                     throw "Conversion of cache not done yet!";
116                     
117                     for (var sy in syms) {
118                         //println("ADD:" + sy );
119                        Parser.symbols.addSymbol(syms[sy]);
120                     }
121                     continue;
122                 }
123             }
124             
125             var src = ''
126             try {
127                 src = File.read(srcFile);
128             }
129             catch(e) {
130                 LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
131                 continue;
132             }
133
134              
135             var tr = new TokenReader();
136             var ts = new TokenStream(tr.tokenize(src));
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         
175         
176         // used to check the details of things being linked to
177         Link.symbolSet = symbolSet;
178
179         
180         var classTemplate = new Template(publish.conf.templatesDir+"class.tmpl");
181         var classesTemplate = new Template(publish.conf.templatesDir+"allclasses.tmpl");
182         //}
183         //atch(e) {
184         //      print(e.message);
185         ///     quit();
186         //}
187         
188         // filters
189         
190         classTemplate.symbolSet = symbolSet;
191         
192         
193         function hasNoParent($) {return ($.memberOf == "")}
194         function isaFile($) {return ($.is("FILE"))}
195         function isaClass($) { return ($.is("CONSTRUCTOR") || $.isNamespace); }
196         
197         var symbols = symbolSet.toArray();
198         
199         var files = JSDOC.opt.srcFiles;
200         for (var i = 0, l = files.length; i < l; i++) {
201             var file = files[i];
202             var srcDir = publish.conf.outDir + "symbols/src/";
203             makeSrcFile(file, srcDir);
204         }
205         
206         var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
207         
208         Link.base = "../";
209         publish.classesIndex = classesTemplate.process(classes); // kept in memory
210         
211         IO.makeDir(publish.conf.outDir+"json");
212         
213         for (var i = 0, l = classes.length; i < l; i++) {
214             var symbol = classes[i];
215             var output = "";
216             
217             output = classTemplate.process(symbol);
218             println("write " + publish.conf.outDir+"symbols/" +symbol.alias+publish.conf.ext);
219             IO.saveFile(publish.conf.outDir+"symbols/", symbol.alias+publish.conf.ext, output);
220             // dump out a 
221             IO.saveFile(publish.conf.outDir+"json/",  symbol.alias+'.json' , publish.jsonRender(symbol));
222             
223             
224         }
225         
226         // regenrate the index with different relative links
227         Link.base = "";
228         publish.classesIndex = classesTemplate.process(classes);
229         
230         try {
231             var classesindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"index.tmpl");
232         }
233         catch(e) { print(e.message); quit(); }
234         
235         var classesIndex = classesindexTemplate.process(classes);
236         IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
237         classesindexTemplate = classesIndex = classes = null;
238         
239         try {
240             var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
241         }
242         catch(e) { print(e.message); quit(); }
243         
244         var documentedFiles = symbols.filter(isaFile);
245         var allFiles = [];
246         
247         for (var i = 0; i < files.length; i++) {
248             allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
249         }
250         
251         for (var i = 0; i < documentedFiles.length; i++) {
252             var offset = files.indexOf(documentedFiles[i].alias);
253             allFiles[offset] = documentedFiles[i];
254         }
255             
256         allFiles = allFiles.sort(makeSortby("name"));
257
258         var filesIndex = fileindexTemplate.process(allFiles);
259         IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
260         fileindexTemplate = filesIndex = files = null;
261     }
262
263      
264      
265     
266 };
267   
268
269
270
271
272
273  
274
275
276
277