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