publish.js 6.0 KB

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