1 //<script type="text/javascript">
3 This is the main container for the JSDOC application.
8 XObject = imports.XObject.XObject;
9 File = imports.File.File;
11 Template = imports.JsTemplate.Template.Template;
12 Link = imports.JsTemplate.Link.Link; // ?? fixme!??
14 Parser = imports.Parser.Parser;
15 TextStream = imports.TextStream.TextStream;
16 TokenReader = imports.TokenReader.TokenReader;
17 TokenStream = imports.TokenStream.TokenStream;
18 Symbol = imports.Symbol.Symbol;
19 DocComment = imports.DocComment.DocComment;
22 // should not realy be here -- or anywhere...??
24 function makeSortby(attribute) {
25 return function(a, b) {
26 if (a[attribute] != undefined && b[attribute] != undefined) {
27 a = a[attribute]; //.toLowerCase();
28 b = b[attribute];//.toLowerCase();
36 Options = false; // refer to this everywhere!
47 build : function (opts)
52 Options.LOG.inform("JsDoc Toolkit main() running at "+new Date()+".");
53 //Options.LOG.inform("With options: ");
55 if (Options.cacheDirectory.length && !File.isDirectory(Options.cacheDirectory)) {
56 File.mkdir(Options.cacheDirectory)
59 Options.srcFiles = this._getSrcFiles();
60 this._parseSrcFiles();
61 this.symbolSet = Parser.symbols;
63 // this currently uses the concept of publish.js...
71 * create a list of files in this.srcFiles using list of directories / files in Options.src
75 _getSrcFiles : function()
81 ext = Options.ext.split(",").map(function($) {return $.toLowerCase()});
84 for (var i = 0; i < Options.src.length; i++) {
85 // add to sourcefiles..
86 if (!File.isDirectory(Options.src[i])) {
87 _this.srcFiles.push(Options.src[i]);
90 File.list(Options.src[i] ).forEach(function($) {
91 if (Options['exclude-src'].indexOf($) > -1) {
94 var thisExt = $.split(".").pop().toLowerCase();
95 if (ext.indexOf(thisExt) < 0) {
98 _this.srcFiles.push(Options.src[i] + '/' + $);
102 //Seed.print(JSON.stringify(this.srcFiles, null,4));Seed.quit();
103 return this.srcFiles;
106 * Parse the source files.
110 _parseSrcFiles : function()
114 for (var i = 0, l = this.srcFiles.length; i < l; i++) {
116 var srcFile = this.srcFiles[i];
118 Options.LOG.inform("reading i : " + i + " length : " + this.srcFiles.length);
120 var cacheFile = !Options.cacheDirectory.length ? false :
121 Options.cacheDirectory + srcFile.replace(/\//g, '_') + ".cache";
124 // disabled at present!@!!
126 if (cacheFile && File.exists(cacheFile)) {
129 var c_mt = File.mtime(cacheFile);
130 var o_mt = File.mtime(srcFile);
131 //println(c_mt.toSource());
132 // println(o_mt.toSource());
134 // this check does not appear to work according to the doc's - need to check it out.
136 if (c_mt > o_mt) { // cached time > original time!
137 // use the cached mtimes..
138 print("Read " + cacheFile);
139 var syms = JSON.parse(File.read(cacheFile), function(k, v) {
141 if (typeof(v) != 'object') {
144 if (typeof(v['*object']) == 'undefined') {
147 var cls = imports[v['*object']][v['*object']];
148 //print(v['*object']);
151 XObject.extend(ret, v);
156 //print("Add sybmols " + cacheFile);
157 for (var sy in syms._index) {
158 // print("ADD:" + sy );
159 Parser.symbols.addSymbol(syms._index[sy]);
167 Options.LOG.inform("reading : " + srcFile);
168 src = File.read(srcFile);
171 Options.LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
175 var txs = new TextStream(src);
177 var tr = new TokenReader({ keepComments : true, keepWhite : true , sepIdents: false });
179 var ts = new TokenStream(tr.tokenize(txs));
181 Parser.parse(ts, srcFile);
184 File.write(cacheFile,
186 Parser.symbolsToObject(srcFile),
192 //var outstr = JSON.stringify(
193 // Parser.filesSymbols[srcFile]._index
195 //File.write(cacheFile, outstr);
208 publish : function() {
209 Options.LOG.inform("Publishing");
214 Options.LOG.inform("Making directories");
215 if (!File.isDirectory(Options.target))
216 File.mkdir(Options.target);
217 if (!File.isDirectory(Options.target+"/symbols"))
218 File.mkdir(Options.target+"/symbols");
219 if (!File.isDirectory(Options.target+"/symbols/src"))
220 File.mkdir(Options.target+"/symbols/src");
222 if (!File.isDirectory(Options.target +"/json")) {
223 File.mkdir(Options.target +"/json");
226 Options.LOG.inform("Copying files from static: " +Options.templateDir);
227 // copy everything in 'static' into
228 File.list(Options.templateDir + '/static').forEach(function (f) {
229 Options.LOG.inform("Copy " + Options.templateDir + '/static/' + f + ' to ' + Options.target + '/' + f);
230 File.copyFile(Options.templateDir + '/static/' + f, Options.target + '/' + f, Gio.FileCopyFlags.OVERWRITE);
234 Options.LOG.inform("Setting up templates");
235 // used to check the details of things being linked to
236 Link.symbolSet = this.symbolSet;
239 Link.srcFileFlatName = this.srcFileFlatName;
240 Link.srcFileRelName = this.srcFileRelName;
242 var classTemplate = new Template({
243 templateFile : Options.templateDir + "/class.html",
246 var classesTemplate = new Template({
247 templateFile : Options.templateDir +"/allclasses.html",
250 var classesindexTemplate = new Template({
251 templateFile : Options.templateDir +"/index.html",
254 var fileindexTemplate = new Template({
255 templateFile : Options.templateDir +"/allfiles.html",
260 classTemplate.symbolSet = this.symbolSet;
263 function hasNoParent($) {
264 return ($.memberOf == "")
266 function isaFile($) {
267 return ($.is("FILE"))
269 function isaClass($) {
270 return ($.is("CONSTRUCTOR") || $.isNamespace);
282 var symbols = this.symbolSet.toArray();
284 var files = Options.srcFiles;
286 for (var i = 0, l = files.length; i < l; i++) {
288 var targetDir = Options.target + "/symbols/src/";
289 this.makeSrcFile(file, targetDir);
292 var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
294 //Options.LOG.inform("classTemplate Process : all classes");
296 // var classesIndex = classesTemplate.process(classes); // kept in memory
298 Options.LOG.inform("iterate classes");
302 for (var i = 0, l = classes.length; i < l; i++) {
303 var symbol = classes[i];
306 Options.LOG.inform("classTemplate Process : " + symbol.alias);
311 File.write(Options.target+"/symbols/" +symbol.alias+'.' + Options.publishExt ,
312 classTemplate.process(symbol));
314 jsonAll[symbol.alias] = this.publishJSON(symbol);
320 File.write(Options.target+"/json/roodata.json",
328 // regenrate the index with different relative links
330 //var classesIndex = classesTemplate.process(classes);
332 Options.LOG.inform("build index");
334 File.write(Options.target + "/index."+ Options.publishExt,
335 classesindexTemplate.process(classes)
338 // blank everything???? classesindexTemplate = classesIndex = classes = null;
342 var documentedFiles = symbols.filter(function ($) {
343 return ($.is("FILE"))
348 for (var i = 0; i < files.length; i++) {
349 allFiles.push(new Symbol(files[i], [], "FILE", new DocComment("/** */")));
352 for (var i = 0; i < documentedFiles.length; i++) {
353 var offset = files.indexOf(documentedFiles[i].alias);
354 allFiles[offset] = documentedFiles[i];
357 allFiles = allFiles.sort(makeSortby("name"));
358 Options.LOG.inform("write files index");
360 File.write(Options.target + "/files."+Options.publishExt,
361 fileindexTemplate.process(allFiles)
369 * JSON files are lookup files for the documentation
370 * - can be used by IDE's or AJAX based doc tools
374 publishJSON : function(data)
376 // what we need to output to be usefull...
378 var cfgProperties = [];
379 if (!data.comment.getTag('singleton').length) {
380 cfgProperties = data.configToArray();
381 cfgProperties = cfgProperties.sort(makeSortby("alias"));
385 //println(cfgProperties.toSource());
387 for(var i =0; i < cfgProperties.length;i++) {
388 p = cfgProperties[i];
393 memberOf : p.memberOf == data.alias ? '' : p.memberOf
398 var ownEvents = data.methods.filter( function(e){
399 return e.isEvent && !e.comment.getTag('hide').length;
400 }).sort(makeSortby("name"));
405 for(var i =0; i < ownEvents.length;i++) {
408 name : m.name.substring(1),
409 sig : this.makeFuncSkel(m.params),
414 //println(props.toSource());
415 // we need to output:
435 srcFileRelName : function(sourceFile)
437 return sourceFile.substring(Options.baseDir.length+1);
439 srcFileFlatName: function(sourceFile)
441 var name = this.srcFileRelName(sourceFile);
442 name = name.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
443 return name.replace(/\:/g, "_") + '.html'; //??;
447 makeSrcFile: function(sourceFile)
449 // this stuff works...
452 var name = this.srcFileFlatName(sourceFile);
454 Options.LOG.inform("Write Source file : " + Options.target+"/symbols/src/" + name);
455 var pretty = imports.PrettyPrint.toPretty(File.read( sourceFile));
456 File.write(Options.target+"/symbols/src/" + name,
458 '<title>' + sourceFile + '</title>' +
459 '<link rel="stylesheet" type="text/css" href="../../../css/highlight-js.css"/>' +
460 '</head><body class="highlightpage">' +
465 * used by JSON output to generate a function skeleton
467 makeFuncSkel :function(params) {
468 if (!params) return "function ()\n{\n\n}";
469 return "function (" +
472 return $.name.indexOf(".") == -1; // don't show config params in signature
474 ).map( function($) { return $.name == 'this' ? '_self' : $.name; } ).join(", ") +