link to individual line numbers in source files (#316)

This commit is contained in:
Jeff Williams 2013-04-20 09:19:10 -07:00
parent 655dfa0f5f
commit 070903fb88
5 changed files with 29 additions and 3 deletions

View File

@ -133,12 +133,15 @@ function hasUrlPrefix(text) {
* @param {string=} linktext - The text to display for the link, or `longname` if no text is
* provided.
* @param {string=} cssClass - The CSS class (or classes) to include in the link's `<a>` tag.
* @param {string=} fragmentId - The fragment identifier (for example, `name` in `foo.html#name`) to
* append to the link target.
* @return {string} The HTML link, or a plain-text string if the link is not available.
*/
var linkto = exports.linkto = function(longname, linktext, cssClass) {
var linkto = exports.linkto = function(longname, linktext, cssClass, fragmentId) {
var catharsis = require('catharsis');
var classString = cssClass ? util.format(' class="%s"', cssClass) : '';
var fragmentString = fragmentId ? '#' + fragmentId : '';
var text = linktext || longname;
var url = hasOwnProp.call(longnameToUrl, longname) && longnameToUrl[longname];
var parsedType;
@ -168,7 +171,7 @@ var linkto = exports.linkto = function(longname, linktext, cssClass) {
return text;
}
else {
return util.format('<a href="%s"%s>%s</a>', url, classString, text);
return util.format('<a href="%s%s"%s>%s</a>', url, fragmentString, classString, text);
}
};

View File

@ -0,0 +1,17 @@
(function() {
var counter = 0;
var numbered;
var source = document.getElementsByClassName('prettyprint source');
if (source && source[0]) {
source = source[0].getElementsByTagName('code')[0];
numbered = source.innerHTML.split('\n');
numbered = numbered.map(function(item) {
counter++;
return '<span id="line' + counter + '"></span>' + item;
});
source.innerHTML = numbered.join('\n');
}
})();

View File

@ -65,7 +65,7 @@ var self = this;
<?js if (data.meta) {?>
<dt class="tag-source">Source:</dt>
<dd class="tag-source"><ul class="dummy"><li>
<?js= self.linkto(meta.filename) ?>, line <?js= meta.lineno ?>
<?js= self.linkto(meta.filename) ?>, <?js= self.linkto(meta.filename, 'line ' + meta.lineno, null, 'line' + meta.lineno) ?>
</li></ul></dd>
<?js } ?>

View File

@ -33,5 +33,6 @@
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>

View File

@ -314,6 +314,11 @@ describe("jsdoc/util/templateHelper", function() {
var link = helper.linkto('http://example.com', 'text');
expect(link).toBe('<a href="http://example.com">text</a>');
});
it('returns a link with a fragment ID if a URL and fragment ID are specified', function() {
var link = helper.linkto('LinktoFakeClass', null, null, 'fragment');
expect(link).toBe('<a href="fakeclass.html#fragment">LinktoFakeClass</a>');
});
});
describe("htmlsafe", function() {