sbin/res/jsdoc/app/lib/JSDOC/JsDoc.js
changeset 1072 ac1eacb3aa33
parent 1071 02c04d2c8fd8
child 1073 687133dc13cf
equal deleted inserted replaced
1071:02c04d2c8fd8 1072:ac1eacb3aa33
     1 /**
       
     2 	@constructor
       
     3 	@param [opt] Used to override the commandline options. Useful for testing.
       
     4 	@version $Id: JsDoc.js 831 2010-03-09 14:24:56Z micmath $
       
     5 */
       
     6 JSDOC.JsDoc = function(/**object*/ opt) {
       
     7 	if (opt) {
       
     8 		JSDOC.opt = opt;
       
     9 	}
       
    10 	
       
    11 	if (JSDOC.opt.h) {
       
    12 		JSDOC.usage();
       
    13 		quit();
       
    14 	}
       
    15 	
       
    16 	// defend against options that are not sane 
       
    17 	if (JSDOC.opt._.length == 0) {
       
    18 		LOG.warn("No source files to work on. Nothing to do.");
       
    19 		quit();
       
    20 	}
       
    21 	if (JSDOC.opt.t === true || JSDOC.opt.d === true) {
       
    22 		JSDOC.usage();
       
    23 	}
       
    24 	
       
    25 	if (typeof JSDOC.opt.d == "string") {
       
    26 		if (!JSDOC.opt.d.charAt(JSDOC.opt.d.length-1).match(/[\\\/]/)) {
       
    27 			JSDOC.opt.d = JSDOC.opt.d+"/";
       
    28 		}
       
    29 		LOG.inform("Output directory set to '"+JSDOC.opt.d+"'.");
       
    30 		IO.mkPath(JSDOC.opt.d);
       
    31 	}
       
    32 	if (JSDOC.opt.e) IO.setEncoding(JSDOC.opt.e);
       
    33 	
       
    34 	// the -r option: scan source directories recursively
       
    35 	if (typeof JSDOC.opt.r == "boolean") JSDOC.opt.r = 10;
       
    36 	else if (!isNaN(parseInt(JSDOC.opt.r))) JSDOC.opt.r = parseInt(JSDOC.opt.r);
       
    37 	else JSDOC.opt.r = 1;
       
    38 	
       
    39 	// the -D option: define user variables
       
    40 	var D = {};
       
    41 	if (JSDOC.opt.D) {
       
    42 		for (var i = 0; i < JSDOC.opt.D.length; i++) {
       
    43 			var param = JSDOC.opt.D[i];
       
    44 			// remove first and last character if both == "
       
    45 			if ( 
       
    46 				param.length > 1
       
    47 				&& param.charAt(0) == '"'
       
    48 				&& param.charAt(param.length-1) == '"'
       
    49 			) {
       
    50 				param = param.substr(1, param.length-2);
       
    51 			}
       
    52 			var defineParts = param.split(":");
       
    53 			if (defineParts && defineParts.length > 1) {
       
    54 				for ( var dpIdx = 2; dpIdx < defineParts.length; dpIdx++ ) {
       
    55 					defineParts[1] += ':' + defineParts[dpIdx];
       
    56 				}
       
    57 				D[defineParts[0]] = defineParts[1];
       
    58 			}
       
    59 		}
       
    60 	}
       
    61 	JSDOC.opt.D = D;
       
    62 	// combine any conf file D options with the commandline D options
       
    63 	if (defined(JSDOC.conf)) for (var c in JSDOC.conf.D) {
       
    64  		if (!defined(JSDOC.opt.D[c])) {
       
    65  			JSDOC.opt.D[c] = JSDOC.conf.D[c];
       
    66  		}
       
    67  	}
       
    68 	
       
    69 	// Give plugins a chance to initialize
       
    70 	if (defined(JSDOC.PluginManager)) {
       
    71 		JSDOC.PluginManager.run("onInit", JSDOC.opt);
       
    72 	}
       
    73 
       
    74 	JSDOC.opt.srcFiles = JSDOC.JsDoc._getSrcFiles();
       
    75 	JSDOC.JsDoc._parseSrcFiles();
       
    76 	JSDOC.JsDoc.symbolSet = JSDOC.Parser.symbols;
       
    77 }
       
    78 
       
    79 /**
       
    80 	Retrieve source file list.
       
    81 	@returns {String[]} The pathnames of the files to be parsed.
       
    82  */
       
    83 JSDOC.JsDoc._getSrcFiles = function() {
       
    84 	JSDOC.JsDoc.srcFiles = [];
       
    85 	
       
    86 	var ext = ["js"];
       
    87 	if (JSDOC.opt.x) {
       
    88 		ext = JSDOC.opt.x.split(",").map(function($) {return $.toLowerCase()});
       
    89 	}
       
    90 	
       
    91 	for (var i = 0; i < JSDOC.opt._.length; i++) {
       
    92 		JSDOC.JsDoc.srcFiles = JSDOC.JsDoc.srcFiles.concat(
       
    93 			IO.ls(JSDOC.opt._[i], JSDOC.opt.r).filter(
       
    94 				function($) {
       
    95 					var thisExt = $.split(".").pop().toLowerCase();
       
    96 					
       
    97 					if (JSDOC.opt.E) {
       
    98 						for(var n = 0; n < JSDOC.opt.E.length; n++) {
       
    99 							if ($.match(new RegExp(JSDOC.opt.E[n]))) {
       
   100 								LOG.inform("Excluding " + $);
       
   101 								return false; // if the file matches the regex then it's excluded.
       
   102 							}
       
   103 						}
       
   104 					}
       
   105 					
       
   106 					return (ext.indexOf(thisExt) > -1); // we're only interested in files with certain extensions
       
   107 				}
       
   108 			)
       
   109 		);
       
   110 	}
       
   111 	
       
   112 	return JSDOC.JsDoc.srcFiles;
       
   113 }
       
   114 
       
   115 JSDOC.JsDoc._parseSrcFiles = function() {
       
   116 	JSDOC.Parser.init();
       
   117 	for (var i = 0, l = JSDOC.JsDoc.srcFiles.length; i < l; i++) {
       
   118 		var srcFile = JSDOC.JsDoc.srcFiles[i];
       
   119 		
       
   120 		if (JSDOC.opt.v) LOG.inform("Parsing file: " + srcFile);
       
   121 		
       
   122 		try {
       
   123 			var src = IO.readFile(srcFile);
       
   124 		}
       
   125 		catch(e) {
       
   126 			LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
       
   127 		}
       
   128 
       
   129 		var tr = new JSDOC.TokenReader();
       
   130 		var ts = new JSDOC.TokenStream(tr.tokenize(new JSDOC.TextStream(src)));
       
   131 
       
   132 		JSDOC.Parser.parse(ts, srcFile);
       
   133 
       
   134 	}
       
   135 	JSDOC.Parser.finish();
       
   136 
       
   137 	if (JSDOC.PluginManager) {
       
   138 		JSDOC.PluginManager.run("onFinishedParsing", JSDOC.Parser.symbols);
       
   139 	}
       
   140 }