generate valid filenames for incorrectly tagged modules (#458)

This commit is contained in:
Jeff Williams 2013-10-01 09:29:08 -07:00
parent 1a8fac17f0
commit 463dd0a05b
2 changed files with 41 additions and 7 deletions

View File

@ -51,8 +51,16 @@ function makeFilenameUnique(filename, str) {
return filename;
}
// compute it here just once
var nsprefix = /^(event|module|external):/;
function cleanseFilename(str) {
// allow for namespace prefix
return str.replace(/^(event|module|external|package):/, '$1-')
// use - instead of ~ to denote 'inner'
.replace(/~/g, '-')
// use _ instead of # to denote 'instance'
.replace(/\#/g, '_')
// remove the variation, if any
.replace(/\([\s\S]*\)$/, '');
}
var htmlsafe = exports.htmlsafe = function(str) {
return str.replace(/</g, '&lt;');
@ -71,11 +79,7 @@ var htmlsafe = exports.htmlsafe = function(str) {
*/
var getUniqueFilename = exports.getUniqueFilename = function(str) {
// allow for namespace prefix
var basename = str.replace(nsprefix, '$1-')
// use - instead of ~ to denote 'inner'
.replace(/~/g, '-')
// remove the variation, if any
.replace(/\([\s\S]*\)$/, '');
var basename = cleanseFilename(str);
// if the basename includes characters that we can't use in a filepath, remove everything up to
// and including the last bad character
@ -701,10 +705,21 @@ exports.createLink = function(doclet) {
var url = '';
var longname = doclet.longname;
var filename;
var fakeContainerMatch = /(\S+):/.exec(longname);
// the doclet gets its own HTML file
if ( containers.indexOf(doclet.kind) !== -1 || isModuleFunction(doclet) ) {
url = getFilename(longname);
}
// the doclet's longname suggests that it should get its own HTML file, but doclet.kind says
// otherwise. this happens due to mistagged JSDoc (for example, a module that somehow has
// doclet.kind set to `member`). use the container's filename plus a fragment.
else if ( containers.indexOf(doclet.kind) === -1 && fakeContainerMatch &&
containers.indexOf(fakeContainerMatch[1]) !== -1 ) {
url = getFilename(longname) + '#' + doclet.name;
}
// the doclet is within another HTML file
else {
filename = getFilename(doclet.memberof || exports.globalName);
url = filename + '#' + getNamespace(doclet.kind) + doclet.name;

View File

@ -1375,6 +1375,25 @@ describe("jsdoc/util/templateHelper", function() {
expect(url).toEqual('module-bar.html');
});
it('should create a url for a doclet with the wrong kind (caused by incorrect JSDoc tags', function() {
var moduleDoclet = {
kind: 'module',
longname: 'module:bar',
name: 'module:bar'
};
var badDoclet = {
kind: 'member',
longname: 'module:bar',
name: 'module:bar'
};
var moduleDocletUrl = helper.createLink(moduleDoclet);
var badDocletUrl = helper.createLink(badDoclet);
expect(moduleDocletUrl).toBe('module-bar.html');
expect(badDocletUrl).toBe('module-bar.html#module:bar');
});
});
describe("resolveAuthorLinks", function() {