thirdparty/jsdoctoolkit/app/lib/JSDOC/JsDoc.js
changeset 3041 c8f47f0b6697
equal deleted inserted replaced
3040:8f9580309846 3041:c8f47f0b6697
       
     1 /**
       
     2 	@constructor
       
     3 	@param [opt] Used to override the commandline options. Useful for testing.
       
     4 	@version $Id: JsDoc.js 773 2009-01-24 09:42:04Z 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 defineParts = JSDOC.opt.D[i].split(":", 2);
       
    44 			if (defineParts) D[defineParts[0]] = defineParts[1];
       
    45 		}
       
    46 	}
       
    47 	JSDOC.opt.D = D;
       
    48 	// combine any conf file D options with the commandline D options
       
    49 	if (defined(JSDOC.conf)) for (var c in JSDOC.conf.D) {
       
    50  		if (!defined(JSDOC.opt.D[c])) {
       
    51  			JSDOC.opt.D[c] = JSDOC.conf.D[c];
       
    52  		}
       
    53  	}
       
    54 	
       
    55 	// Give plugins a chance to initialize
       
    56 	if (defined(JSDOC.PluginManager)) {
       
    57 		JSDOC.PluginManager.run("onInit", JSDOC.opt);
       
    58 	}
       
    59 
       
    60 	JSDOC.opt.srcFiles = JSDOC.JsDoc._getSrcFiles();
       
    61 	JSDOC.JsDoc._parseSrcFiles();
       
    62 	JSDOC.JsDoc.symbolSet = JSDOC.Parser.symbols;
       
    63 }
       
    64 
       
    65 /**
       
    66 	Retrieve source file list.
       
    67 	@returns {String[]} The pathnames of the files to be parsed.
       
    68  */
       
    69 JSDOC.JsDoc._getSrcFiles = function() {
       
    70 	JSDOC.JsDoc.srcFiles = [];
       
    71 	
       
    72 	var ext = ["js"];
       
    73 	if (JSDOC.opt.x) {
       
    74 		ext = JSDOC.opt.x.split(",").map(function($) {return $.toLowerCase()});
       
    75 	}
       
    76 	
       
    77 	for (var i = 0; i < JSDOC.opt._.length; i++) {
       
    78 		JSDOC.JsDoc.srcFiles = JSDOC.JsDoc.srcFiles.concat(
       
    79 			IO.ls(JSDOC.opt._[i], JSDOC.opt.r).filter(
       
    80 				function($) {
       
    81 					var thisExt = $.split(".").pop().toLowerCase();
       
    82 					
       
    83 					if (JSDOC.opt.E) {
       
    84 						for(var n = 0; n < JSDOC.opt.E.length; n++) {
       
    85 							if ($.match(new RegExp(JSDOC.opt.E[n]))) {
       
    86 								LOG.inform("Excluding " + $);
       
    87 								return false; // if the file matches the regex then it's excluded.
       
    88 							}
       
    89 						}
       
    90 					}
       
    91 					
       
    92 					return (ext.indexOf(thisExt) > -1); // we're only interested in files with certain extensions
       
    93 				}
       
    94 			)
       
    95 		);
       
    96 	}
       
    97 	
       
    98 	return JSDOC.JsDoc.srcFiles;
       
    99 }
       
   100 
       
   101 JSDOC.JsDoc._parseSrcFiles = function() {
       
   102 	JSDOC.Parser.init();
       
   103 	for (var i = 0, l = JSDOC.JsDoc.srcFiles.length; i < l; i++) {
       
   104 		var srcFile = JSDOC.JsDoc.srcFiles[i];
       
   105 		
       
   106 		if (JSDOC.opt.v) LOG.inform("Parsing file: " + srcFile);
       
   107 		
       
   108 		try {
       
   109 			var src = IO.readFile(srcFile);
       
   110 		}
       
   111 		catch(e) {
       
   112 			LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
       
   113 		}
       
   114 
       
   115 		var tr = new JSDOC.TokenReader();
       
   116 		var ts = new JSDOC.TokenStream(tr.tokenize(new JSDOC.TextStream(src)));
       
   117 
       
   118 		JSDOC.Parser.parse(ts, srcFile);
       
   119 
       
   120 	}
       
   121 	JSDOC.Parser.finish();
       
   122 
       
   123 	if (JSDOC.PluginManager) {
       
   124 		JSDOC.PluginManager.run("onFinishedParsing", JSDOC.Parser.symbols);
       
   125 	}
       
   126 }