Link.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /** Handle the creation of HTML links to documented symbols.
  2. @constructor
  3. */
  4. function Link() {
  5. this.alias = "";
  6. this.src = "";
  7. this.file = "";
  8. this.text = "";
  9. this.innerName = "";
  10. this.classLink = false;
  11. this.targetName = "";
  12. this.target = function(targetName) {
  13. if (defined(targetName)) this.targetName = targetName;
  14. return this;
  15. }
  16. this.inner = function(inner) {
  17. if (defined(inner)) this.innerName = inner;
  18. return this;
  19. }
  20. this.withText = function(text) {
  21. if (defined(text)) this.text = text;
  22. return this;
  23. }
  24. this.toSrc = function(filename) {
  25. if (defined(filename)) this.src = filename;
  26. return this;
  27. }
  28. this.toSymbol = function(alias) {
  29. if (defined(alias)) this.alias = new String(alias);
  30. return this;
  31. }
  32. this.toClass = function(alias) {
  33. this.classLink = true;
  34. return this.toSymbol(alias);
  35. }
  36. this.toFile = function(file) {
  37. if (defined(file)) this.file = file;
  38. return this;
  39. }
  40. this.toString = function() {
  41. var linkString;
  42. var thisLink = this;
  43. if (this.alias) {
  44. linkString = this.alias.replace(/(^|[^a-z$0-9_#.:^-])([|a-z$0-9_#.:^-]+)($|[^a-z$0-9_#.:^-])/i,
  45. function(match, prematch, symbolName, postmatch) {
  46. var symbolNames = symbolName.split("|");
  47. var links = [];
  48. for (var i = 0, l = symbolNames.length; i < l; i++) {
  49. thisLink.alias = symbolNames[i];
  50. links.push(thisLink._makeSymbolLink(symbolNames[i]));
  51. }
  52. return prematch+links.join("|")+postmatch;
  53. }
  54. );
  55. }
  56. else if (this.src) {
  57. linkString = thisLink._makeSrcLink(this.src);
  58. }
  59. else if (this.file) {
  60. linkString = thisLink._makeFileLink(this.file);
  61. }
  62. return linkString;
  63. }
  64. }
  65. /** prefixed for hashes */
  66. Link.hashPrefix = "";
  67. /** Appended to the front of relative link paths. */
  68. Link.base = "";
  69. Link.symbolNameToLinkName = function(symbol) {
  70. var linker = "";
  71. if (symbol.isStatic) linker = ".";
  72. else if (symbol.isInner) linker = "-";
  73. return Link.hashPrefix+linker+symbol.name;
  74. }
  75. Link.getSymbol= function(alias) {
  76. var symbol= Link.symbolSet.getSymbol(alias);
  77. if (symbol)
  78. return symbol;
  79. if ('#'!==alias.charAt(0) || !Link.currentSymbol)
  80. return null;
  81. // resolve relative name
  82. var container= Link.currentSymbol;
  83. while (container)
  84. {
  85. symbol= Link.symbolSet.getSymbol(container.alias + alias);
  86. if (symbol)
  87. return symbol;
  88. // No superclass
  89. if (!container.augments.length)
  90. return null;
  91. container= Link.symbolSet.getSymbol(container.augments[0].desc);
  92. }
  93. return null;
  94. }
  95. /** Create a link to another symbol. */
  96. Link.prototype._makeSymbolLink = function(alias) {
  97. var linkBase = Link.base+publish.conf.symbolsDir;
  98. var linkTo = Link.getSymbol(alias);
  99. var linkPath;
  100. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  101. // if there is no symbol by that name just return the name unaltered
  102. if (!linkTo)
  103. return this.text || alias;
  104. // it's a symbol in another file
  105. else {
  106. if (!linkTo.is("CONSTRUCTOR") && !linkTo.isNamespace) { // it's a method or property
  107. linkPath= (Link.filemap) ? Link.filemap[linkTo.memberOf] :
  108. escape(linkTo.memberOf) || "_global_";
  109. if (linkTo.isEvent)
  110. linkPath += publish.conf.ext + "#event:" + Link.symbolNameToLinkName(linkTo);
  111. else
  112. linkPath += publish.conf.ext + "#" + Link.symbolNameToLinkName(linkTo);
  113. }
  114. else {
  115. linkPath = (Link.filemap)? Link.filemap[linkTo.alias] : escape(linkTo.alias);
  116. linkPath += publish.conf.ext;// + (this.classLink? "":"#" + Link.hashPrefix + "constructor");
  117. }
  118. linkPath = linkBase + linkPath
  119. }
  120. var linkText= this.text || alias;
  121. var link = {linkPath: linkPath, linkText: linkText, linkInner: (this.innerName? "#"+this.innerName : "")};
  122. if (typeof JSDOC.PluginManager != "undefined") {
  123. JSDOC.PluginManager.run("onSymbolLink", link);
  124. }
  125. return "<a href=\""+link.linkPath+link.linkInner+"\""+target+">"+link.linkText+"</a>";
  126. }
  127. /** Create a link to a source file. */
  128. Link.prototype._makeSrcLink = function(srcFilePath) {
  129. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  130. // transform filepath into a filename
  131. var srcFile = srcFilePath.replace(/\.\.?[\\\/]/g, "").replace(/[:\\\/]/g, "_");
  132. var outFilePath = Link.base + publish.conf.srcDir + srcFile + publish.conf.ext;
  133. if (!this.text) this.text = FilePath.fileName(srcFilePath);
  134. return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
  135. }
  136. /** Create a link to a source file. */
  137. Link.prototype._makeFileLink = function(filePath) {
  138. var target = (this.targetName)? " target=\""+this.targetName+"\"" : "";
  139. var outFilePath = Link.base + filePath;
  140. if (!this.text) this.text = filePath;
  141. return "<a href=\""+outFilePath+"\""+target+">"+this.text+"</a>";
  142. }