publish.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /** Called automatically by JsDoc Toolkit. */
  2. function publish(symbolSet) {
  3. publish.conf = { // trailing slash expected for dirs
  4. ext: ".html",
  5. outDir: JSDOC.opt.d || SYS.pwd+"../out/jsdoc/",
  6. templatesDir: JSDOC.opt.t || SYS.pwd+"../templates/jsdoc/",
  7. cssDir: "css/",
  8. symbolsDir: "symbols/",
  9. srcDir: "symbols/src/"
  10. };
  11. // is source output is suppressed, just display the links to the source file
  12. if (JSDOC.opt.s && defined(Link) && Link.prototype._makeSrcLink) {
  13. Link.prototype._makeSrcLink = function(srcFilePath) {
  14. return "<"+srcFilePath+">";
  15. }
  16. }
  17. // create the folders and subfolders to hold the output
  18. IO.mkPath((publish.conf.outDir+publish.conf.cssDir));
  19. IO.mkPath((publish.conf.outDir+"symbols/src").split("/"));
  20. // used to allow Link to check the details of things being linked to
  21. Link.symbolSet = symbolSet;
  22. // create the required templates
  23. try {
  24. //var styleTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+publish.conf.cssDir+"default.css");
  25. var classTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"class.tmpl");
  26. var classesTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allclasses.tmpl");
  27. }
  28. catch(e) {
  29. print("Couldn't create the required templates: "+e);
  30. quit();
  31. }
  32. // some utility filters
  33. function hasNoParent($) {return ($.memberOf == "")}
  34. function isaFile($) {return ($.is("FILE"))}
  35. function isaClass($) {return ($.is("CONSTRUCTOR") || $.isNamespace)}
  36. // get an array version of the symbolset, useful for filtering
  37. var symbols = symbolSet.toArray();
  38. // create the hilited source code files
  39. var files = JSDOC.opt.srcFiles;
  40. for (var i = 0, l = files.length; i < l; i++) {
  41. var file = files[i];
  42. var srcDir = publish.conf.outDir + "symbols/src/";
  43. makeSrcFile(file, srcDir);
  44. }
  45. // get a list of all the classes in the symbolset
  46. var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
  47. // create a filemap in which outfiles must be to be named uniquely, ignoring case
  48. if (JSDOC.opt.u) {
  49. var filemapCounts = {};
  50. Link.filemap = {};
  51. for (var i = 0, l = classes.length; i < l; i++) {
  52. var lcAlias = classes[i].alias.toLowerCase();
  53. if (!filemapCounts[lcAlias]) filemapCounts[lcAlias] = 1;
  54. else filemapCounts[lcAlias]++;
  55. Link.filemap[classes[i].alias] =
  56. (filemapCounts[lcAlias] > 1)?
  57. lcAlias+"_"+filemapCounts[lcAlias] : lcAlias;
  58. }
  59. }
  60. // create a class index, displayed in the left-hand column of every class page
  61. Link.base = "../";
  62. publish.classesIndex = classesTemplate.process(classes); // kept in memory
  63. // create each of the class pages
  64. for (var i = 0, l = classes.length; i < l; i++) {
  65. var symbol = classes[i];
  66. symbol.events = symbol.getEvents(); // 1 order matters
  67. symbol.methods = symbol.getMethods(); // 2
  68. var output = "";
  69. output = classTemplate.process(symbol);
  70. IO.saveFile(publish.conf.outDir+"symbols/", ((JSDOC.opt.u)? Link.filemap[symbol.alias] : symbol.alias) + publish.conf.ext, output);
  71. }
  72. // regenerate the index with different relative links, used in the index pages
  73. Link.base = "";
  74. publish.classesIndex = classesTemplate.process(classes);
  75. // create the class index page
  76. try {
  77. var classesindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"index.tmpl");
  78. }
  79. catch(e) { print(e.message); quit(); }
  80. var classesIndex = classesindexTemplate.process(classes);
  81. IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
  82. classesindexTemplate = classesIndex = classes = null;
  83. // create the file index page
  84. try {
  85. var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
  86. }
  87. catch(e) { print(e.message); quit(); }
  88. var documentedFiles = symbols.filter(isaFile); // files that have file-level docs
  89. var allFiles = []; // not all files have file-level docs, but we need to list every one
  90. for (var i = 0; i < files.length; i++) {
  91. allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
  92. }
  93. for (var i = 0; i < documentedFiles.length; i++) {
  94. var offset = files.indexOf(documentedFiles[i].alias);
  95. allFiles[offset] = documentedFiles[i];
  96. }
  97. allFiles = allFiles.sort(makeSortby("name"));
  98. // output the file index page
  99. var filesIndex = fileindexTemplate.process(allFiles);
  100. IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
  101. fileindexTemplate = filesIndex = files = null;
  102. // copy files
  103. IO.copyFile(publish.conf.templatesDir+"/"+publish.conf.cssDir+"default.css", publish.conf.outDir+"/"+publish.conf.cssDir);
  104. }
  105. /** Just the first sentence (up to a full stop). Should not break on dotted variable names. */
  106. function summarize(desc) {
  107. if (typeof desc != "undefined")
  108. return desc.match(/([\w\W]+?\.)[^a-z0-9_$]/i)? RegExp.$1 : desc;
  109. }
  110. /** Make a symbol sorter by some attribute. */
  111. function makeSortby(attribute) {
  112. return function(a, b) {
  113. if (a[attribute] != undefined && b[attribute] != undefined) {
  114. a = a[attribute].toLowerCase();
  115. b = b[attribute].toLowerCase();
  116. if (a < b) return -1;
  117. if (a > b) return 1;
  118. return 0;
  119. }
  120. }
  121. }
  122. /** Pull in the contents of an external file at the given path. */
  123. function include(path) {
  124. var path = publish.conf.templatesDir+path;
  125. return IO.readFile(path);
  126. }
  127. /** Turn a raw source file into a code-hilited page in the docs. */
  128. function makeSrcFile(path, srcDir, name) {
  129. if (JSDOC.opt.s) return;
  130. if (!name) {
  131. name = path.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
  132. name = name.replace(/\:/g, "_");
  133. }
  134. var src = {path: path, name:name, charset: IO.encoding, hilited: ""};
  135. if (defined(JSDOC.PluginManager)) {
  136. JSDOC.PluginManager.run("onPublishSrc", src);
  137. }
  138. if (src.hilited) {
  139. IO.saveFile(srcDir, name+publish.conf.ext, src.hilited);
  140. }
  141. }
  142. /** Build output for displaying function parameters. */
  143. function makeSignature(params) {
  144. if (!params) return "()";
  145. var signature = "("
  146. +
  147. params.filter(
  148. function($) {
  149. return $.name.indexOf(".") == -1; // don't show config params in signature
  150. }
  151. ).map(
  152. function($) {
  153. return $.name;
  154. }
  155. ).join(", ")
  156. +
  157. ")";
  158. return signature;
  159. }
  160. /** Find symbol {@link ...} strings in text and turn into html links */
  161. function resolveLinks(str, from) {
  162. str = str.replace(/\{@link ([^} ]+) ?\}/gi,
  163. function(match, symbolName) {
  164. return new Link().toSymbol(symbolName);
  165. }
  166. );
  167. return str;
  168. }