/tags/jsdoc_toolkit-2.0.1/jsdoc-toolkit/app/lib/JSDOC/JsDoc.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 162 lines · 97 code · 20 blank · 45 comment · 33 complexity · 7bb624fa615f5d1e2f387140ca788285 MD5 · raw file

  1. /**
  2. @constructor
  3. @param [opt] Used to override the commandline options. Useful for testing.
  4. @version $Id: JsDoc.js 592 2008-05-09 07:43:33Z micmath $
  5. */
  6. JSDOC.JsDoc = function(/**object*/ opt) {
  7. if (opt) {
  8. JSDOC.opt = opt;
  9. }
  10. // the -c option: use a configuration file
  11. if (JSDOC.opt.c) {
  12. eval("JSDOC.conf = " + IO.readFile(JSDOC.opt.c));
  13. LOG.inform("Using configuration file at '"+JSDOC.opt.c+"'.");
  14. for (var c in JSDOC.conf) {
  15. if (c !== "D" && !defined(JSDOC.opt[c])) { // commandline overrules config file
  16. JSDOC.opt[c] = JSDOC.conf[c];
  17. }
  18. }
  19. if (typeof JSDOC.conf["_"] != "undefined") {
  20. JSDOC.opt["_"] = JSDOC.opt["_"].concat(JSDOC.conf["_"]);
  21. }
  22. LOG.inform("With configuration: ");
  23. for (var o in JSDOC.opt) {
  24. LOG.inform(" "+o+": "+JSDOC.opt[o]);
  25. }
  26. }
  27. if (JSDOC.opt.h) {
  28. JSDOC.usage();
  29. quit();
  30. }
  31. // defend against options that are not sane
  32. if (JSDOC.opt._.length == 0) {
  33. LOG.warn("No source files to work on. Nothing to do.");
  34. quit();
  35. }
  36. if (JSDOC.opt.t === true || JSDOC.opt.d === true) {
  37. JSDOC.usage();
  38. }
  39. if (typeof JSDOC.opt.d == "string") {
  40. if (!JSDOC.opt.d.charAt(JSDOC.opt.d.length-1).match(/[\\\/]/)) {
  41. JSDOC.opt.d = JSDOC.opt.d+"/";
  42. }
  43. LOG.inform("Output directory set to '"+JSDOC.opt.d+"'.");
  44. IO.mkPath(JSDOC.opt.d);
  45. }
  46. if (JSDOC.opt.e) IO.setEncoding(JSDOC.opt.e);
  47. // the -r option: scan source directories recursively
  48. if (typeof JSDOC.opt.r == "boolean") JSDOC.opt.r = 10;
  49. else if (!isNaN(parseInt(JSDOC.opt.r))) JSDOC.opt.r = parseInt(JSDOC.opt.r);
  50. else JSDOC.opt.r = 1;
  51. // the -D option: define user variables
  52. var D = {};
  53. if (JSDOC.opt.D) {
  54. for (var i = 0; i < JSDOC.opt.D.length; i++) {
  55. var defineParts = JSDOC.opt.D[i].split(":", 2);
  56. if (defineParts) D[defineParts[0]] = defineParts[1];
  57. }
  58. }
  59. JSDOC.opt.D = D;
  60. // combine any conf file D options with the commandline D options
  61. if (defined(JSDOC.conf)) for (var c in JSDOC.conf.D) {
  62. if (!defined(JSDOC.opt.D[c])) {
  63. JSDOC.opt.D[c] = JSDOC.conf.D[c];
  64. }
  65. }
  66. // Load additional file handlers
  67. // the -H option: filetype handlers
  68. JSDOC.handlers = {};
  69. /*
  70. if (JSDOC.opt.H) {
  71. for (var i = 0; i < JSDOC.opt.H.length; i++) {
  72. var handlerDef = JSDOC.opt.H[i].split(":");
  73. LOG.inform("Adding '." + handlerDef[0] + "' content handler from handlers/" + handlerDef[1] + ".js");
  74. IO.include("handlers/" + handlerDef[1] + ".js");
  75. if (!eval("typeof "+handlerDef[1])) {
  76. LOG.warn(handlerDef[1] + "is not defined in "+handlerDef[1] + ".js");
  77. }
  78. else {
  79. JSDOC.handlers[handlerDef[0]] = eval(handlerDef[1]);
  80. }
  81. }
  82. }
  83. */
  84. // Give plugins a chance to initialize
  85. if (defined(JSDOC.PluginManager)) {
  86. JSDOC.PluginManager.run("onInit", this);
  87. }
  88. JSDOC.opt.srcFiles = this._getSrcFiles();
  89. this._parseSrcFiles();
  90. //var handler = symbols.handler;
  91. this.symbolSet = JSDOC.Parser.symbols;
  92. //this.symbolGroup.handler = handler;
  93. }
  94. /**
  95. Retrieve source file list.
  96. @returns {String[]} The pathnames of the files to be parsed.
  97. */
  98. JSDOC.JsDoc.prototype._getSrcFiles = function() {
  99. this.srcFiles = [];
  100. var ext = ["js"];
  101. if (JSDOC.opt.x) {
  102. ext = JSDOC.opt.x.split(",").map(function($) {return $.toLowerCase()});
  103. }
  104. for (var i = 0; i < JSDOC.opt._.length; i++) {
  105. this.srcFiles = this.srcFiles.concat(
  106. IO.ls(JSDOC.opt._[i], JSDOC.opt.r).filter(
  107. function($) {
  108. var thisExt = $.split(".").pop().toLowerCase();
  109. return (ext.indexOf(thisExt) > -1 || thisExt in JSDOC.handlers); // we're only interested in files with certain extensions
  110. }
  111. )
  112. );
  113. }
  114. return this.srcFiles;
  115. }
  116. JSDOC.JsDoc.prototype._parseSrcFiles = function() {
  117. JSDOC.Parser.init();
  118. for (var i = 0, l = this.srcFiles.length; i < l; i++) {
  119. var srcFile = this.srcFiles[i];
  120. try {
  121. var src = IO.readFile(srcFile);
  122. }
  123. catch(e) {
  124. LOG.warn("Can't read source file '"+srcFile+"': "+e.message);
  125. }
  126. // Check to see if there is a handler for this file type
  127. // var ext = FilePath.fileExtension(srcFile);
  128. // if (JSDOC.handlers[ext]) {
  129. // LOG.inform(" Using external handler for '" + ext + "'");
  130. //
  131. // symbols = symbols.concat(JSDOC.handlers[ext].handle(srcFile, src));
  132. // symbols.handler = JSDOC.handlers[ext];
  133. // }
  134. // else {
  135. // The default (JSDOC) handler
  136. var tr = new JSDOC.TokenReader();
  137. var ts = new JSDOC.TokenStream(tr.tokenize(new JSDOC.TextStream(src)));
  138. JSDOC.Parser.parse(ts, srcFile);
  139. // }
  140. }
  141. JSDOC.Parser.finish();
  142. }