node-query/node_modules/jsdoc/templates/default/publish.js

662 lines
20 KiB
JavaScript
Raw Normal View History

2014-10-20 16:56:45 -04:00
/*global env: true */
2014-10-22 10:11:40 -04:00
'use strict';
2014-10-20 16:56:45 -04:00
2014-10-22 10:11:40 -04:00
var fs = require('jsdoc/fs');
var helper = require('jsdoc/util/templateHelper');
var logger = require('jsdoc/util/logger');
var path = require('jsdoc/path');
var taffy = require('taffydb').taffy;
var template = require('jsdoc/template');
var util = require('util');
var htmlsafe = helper.htmlsafe;
var linkto = helper.linkto;
var resolveAuthorLinks = helper.resolveAuthorLinks;
var scopeToPunc = helper.scopeToPunc;
var hasOwnProp = Object.prototype.hasOwnProperty;
var data;
var view;
var outdir = env.opts.destination;
2014-10-20 16:56:45 -04:00
function find(spec) {
return helper.find(data, spec);
}
function tutoriallink(tutorial) {
return helper.toTutorial(tutorial, null, { tag: 'em', classname: 'disabled', prefix: 'Tutorial: ' });
}
function getAncestorLinks(doclet) {
return helper.getAncestorLinks(data, doclet);
}
function hashToLink(doclet, hash) {
if ( !/^(#.+)/.test(hash) ) { return hash; }
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var url = helper.createLink(doclet);
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
url = url.replace(/(#.+|$)/, hash);
return '<a href="' + url + '">' + hash + '</a>';
}
function needsSignature(doclet) {
var needsSig = false;
// function and class definitions always get a signature
if (doclet.kind === 'function' || doclet.kind === 'class') {
needsSig = true;
}
// typedefs that contain functions get a signature, too
else if (doclet.kind === 'typedef' && doclet.type && doclet.type.names &&
doclet.type.names.length) {
for (var i = 0, l = doclet.type.names.length; i < l; i++) {
if (doclet.type.names[i].toLowerCase() === 'function') {
needsSig = true;
break;
}
}
}
return needsSig;
}
2014-10-22 10:11:40 -04:00
function getSignatureAttributes(item) {
var attributes = [];
if (item.optional) {
attributes.push('opt');
}
if (item.nullable === true) {
attributes.push('nullable');
}
else if (item.nullable === false) {
attributes.push('non-null');
}
return attributes;
}
function updateItemName(item) {
var attributes = getSignatureAttributes(item);
var itemName = item.name || '';
if (item.variable) {
itemName = '&hellip;' + itemName;
}
if (attributes && attributes.length) {
itemName = util.format( '%s<span class="signature-attributes">%s</span>', itemName,
attributes.join(', ') );
}
return itemName;
}
function addParamAttributes(params) {
return params.filter(function(param) {
return param.name && param.name.indexOf('.') === -1;
}).map(updateItemName);
}
function buildItemTypeStrings(item) {
var types = [];
if (item.type && item.type.names) {
item.type.names.forEach(function(name) {
types.push( linkto(name, htmlsafe(name)) );
});
}
return types;
}
function buildAttribsString(attribs) {
var attribsString = '';
if (attribs && attribs.length) {
attribsString = htmlsafe( util.format('(%s) ', attribs.join(', ')) );
}
return attribsString;
}
function addNonParamAttributes(items) {
var types = [];
items.forEach(function(item) {
types = types.concat( buildItemTypeStrings(item) );
});
return types;
}
2014-10-20 16:56:45 -04:00
function addSignatureParams(f) {
2014-10-22 10:11:40 -04:00
var params = f.params ? addParamAttributes(f.params) : [];
f.signature = util.format( '%s(%s)', (f.signature || ''), params.join(', ') );
2014-10-20 16:56:45 -04:00
}
function addSignatureReturns(f) {
2014-10-22 10:11:40 -04:00
var attribs = [];
var attribsString = '';
var returnTypes = [];
var returnTypesString = '';
// jam all the return-type attributes into an array. this could create odd results (for example,
// if there are both nullable and non-nullable return types), but let's assume that most people
// who use multiple @return tags aren't using Closure Compiler type annotations, and vice-versa.
if (f.returns) {
f.returns.forEach(function(item) {
helper.getAttribs(item).forEach(function(attrib) {
if (attribs.indexOf(attrib) === -1) {
attribs.push(attrib);
}
});
});
attribsString = buildAttribsString(attribs);
}
if (f.returns) {
returnTypes = addNonParamAttributes(f.returns);
}
if (returnTypes.length) {
returnTypesString = util.format( ' &rarr; %s{%s}', attribsString, returnTypes.join('|') );
}
2014-10-20 16:56:45 -04:00
f.signature = '<span class="signature">' + (f.signature || '') + '</span>' +
2014-10-22 10:11:40 -04:00
'<span class="type-signature">' + returnTypesString + '</span>';
2014-10-20 16:56:45 -04:00
}
function addSignatureTypes(f) {
2014-10-22 10:11:40 -04:00
var types = f.type ? buildItemTypeStrings(f) : [];
f.signature = (f.signature || '') + '<span class="type-signature">' +
(types.length ? ' :' + types.join('|') : '') + '</span>';
2014-10-20 16:56:45 -04:00
}
function addAttribs(f) {
var attribs = helper.getAttribs(f);
2014-10-22 10:11:40 -04:00
var attribsString = buildAttribsString(attribs);
f.attribs = util.format('<span class="type-signature">%s</span>', attribsString);
2014-10-20 16:56:45 -04:00
}
function shortenPaths(files, commonPrefix) {
Object.keys(files).forEach(function(file) {
files[file].shortened = files[file].resolved.replace(commonPrefix, '')
2014-10-22 10:11:40 -04:00
// always use forward slashes
.replace(/\\/g, '/');
2014-10-20 16:56:45 -04:00
});
return files;
}
function getPathFromDoclet(doclet) {
if (!doclet.meta) {
2014-10-22 10:11:40 -04:00
return null;
2014-10-20 16:56:45 -04:00
}
2014-10-22 10:11:40 -04:00
return doclet.meta.path && doclet.meta.path !== 'null' ?
path.join(doclet.meta.path, doclet.meta.filename) :
2014-10-20 16:56:45 -04:00
doclet.meta.filename;
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
function generate(title, docs, filename, resolveLinks) {
resolveLinks = resolveLinks === false ? false : true;
var docData = {
title: title,
docs: docs
};
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var outpath = path.join(outdir, filename),
html = view.render('container.tmpl', docData);
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (resolveLinks) {
html = helper.resolveLinks(html); // turn {@link foo} into <a href="foodoc.html">foo</a>
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
fs.writeFileSync(outpath, html, 'utf8');
}
function generateSourceFiles(sourceFiles, encoding) {
encoding = encoding || 'utf8';
Object.keys(sourceFiles).forEach(function(file) {
var source;
2014-10-22 10:11:40 -04:00
// links are keyed to the shortened path in each doclet's `meta.shortpath` property
2014-10-20 16:56:45 -04:00
var sourceOutfile = helper.getUniqueFilename(sourceFiles[file].shortened);
helper.registerLink(sourceFiles[file].shortened, sourceOutfile);
try {
source = {
kind: 'source',
code: helper.htmlsafe( fs.readFileSync(sourceFiles[file].resolved, encoding) )
};
}
catch(e) {
2014-10-22 10:11:40 -04:00
logger.error('Error while generating source file %s: %s', file, e.message);
2014-10-20 16:56:45 -04:00
}
generate('Source: ' + sourceFiles[file].shortened, [source], sourceOutfile,
false);
});
}
/**
* Look for classes or functions with the same name as modules (which indicates that the module
* exports only that class or function), then attach the classes or functions to the `module`
* property of the appropriate module doclets. The name of each class or function is also updated
* for display purposes. This function mutates the original arrays.
2014-10-22 10:11:40 -04:00
*
2014-10-20 16:56:45 -04:00
* @private
* @param {Array.<module:jsdoc/doclet.Doclet>} doclets - The array of classes and functions to
* check.
* @param {Array.<module:jsdoc/doclet.Doclet>} modules - The array of module doclets to search.
*/
function attachModuleSymbols(doclets, modules) {
var symbols = {};
// build a lookup table
doclets.forEach(function(symbol) {
symbols[symbol.longname] = symbol;
});
return modules.map(function(module) {
if (symbols[module.longname]) {
module.module = symbols[module.longname];
2014-10-22 10:11:40 -04:00
module.module.name = module.module.name.replace('module:', '(require("') + '"))';
2014-10-20 16:56:45 -04:00
}
});
}
/**
* Create the navigation sidebar.
* @param {object} members The members that will be used to create the sidebar.
* @param {array<object>} members.classes
* @param {array<object>} members.externals
* @param {array<object>} members.globals
* @param {array<object>} members.mixins
* @param {array<object>} members.modules
* @param {array<object>} members.namespaces
* @param {array<object>} members.tutorials
* @param {array<object>} members.events
* @return {string} The HTML for the navigation sidebar.
*/
function buildNav(members) {
var nav = '<h2><a href="index.html">Index</a></h2>',
seen = {},
hasClassList = false,
classNav = '',
globalNav = '';
if (members.modules.length) {
nav += '<h3>Modules</h3><ul>';
members.modules.forEach(function(m) {
if ( !hasOwnProp.call(seen, m.longname) ) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + linkto(m.longname, m.name) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[m.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (members.externals.length) {
nav += '<h3>Externals</h3><ul>';
members.externals.forEach(function(e) {
if ( !hasOwnProp.call(seen, e.longname) ) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + linkto( e.longname, e.name.replace(/(^"|"$)/g, '') ) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[e.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
if (members.classes.length) {
members.classes.forEach(function(c) {
if ( !hasOwnProp.call(seen, c.longname) ) {
2014-10-22 10:11:40 -04:00
classNav += '<li>' + linkto(c.longname, c.name) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[c.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (classNav !== '') {
nav += '<h3>Classes</h3><ul>';
nav += classNav;
nav += '</ul>';
}
}
if (members.events.length) {
nav += '<h3>Events</h3><ul>';
members.events.forEach(function(e) {
if ( !hasOwnProp.call(seen, e.longname) ) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + linkto(e.longname, e.name) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[e.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (members.namespaces.length) {
nav += '<h3>Namespaces</h3><ul>';
members.namespaces.forEach(function(n) {
if ( !hasOwnProp.call(seen, n.longname) ) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + linkto(n.longname, n.name) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[n.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (members.mixins.length) {
nav += '<h3>Mixins</h3><ul>';
members.mixins.forEach(function(m) {
if ( !hasOwnProp.call(seen, m.longname) ) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + linkto(m.longname, m.name) + '</li>';
2014-10-20 16:56:45 -04:00
}
seen[m.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
if (members.tutorials.length) {
nav += '<h3>Tutorials</h3><ul>';
members.tutorials.forEach(function(t) {
2014-10-22 10:11:40 -04:00
nav += '<li>' + tutoriallink(t.name) + '</li>';
2014-10-20 16:56:45 -04:00
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
nav += '</ul>';
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (members.globals.length) {
members.globals.forEach(function(g) {
if ( g.kind !== 'typedef' && !hasOwnProp.call(seen, g.longname) ) {
globalNav += '<li>' + linkto(g.longname, g.name) + '</li>';
}
seen[g.longname] = true;
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (!globalNav) {
// turn the heading into a link so you can actually get to the global page
nav += '<h3>' + linkto('global', 'Global') + '</h3>';
}
else {
nav += '<h3>Global</h3><ul>' + globalNav + '</ul>';
}
}
return nav;
}
/**
@param {TAFFY} taffyData See <http://taffydb.com/>.
@param {object} opts
@param {Tutorial} tutorials
*/
exports.publish = function(taffyData, opts, tutorials) {
data = taffyData;
var conf = env.conf.templates || {};
conf['default'] = conf['default'] || {};
var templatePath = opts.template;
view = new template.Template(templatePath + '/tmpl');
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// claim some special filenames in advance, so the All-Powerful Overseer of Filename Uniqueness
// doesn't try to hand them out later
var indexUrl = helper.getUniqueFilename('index');
// don't call registerLink() on this one! 'index' is also a valid longname
var globalUrl = helper.getUniqueFilename('global');
helper.registerLink('global', globalUrl);
// set up templating
2014-10-22 10:11:40 -04:00
view.layout = conf['default'].layoutFile ?
path.getResourcePath(path.dirname(conf['default'].layoutFile),
path.basename(conf['default'].layoutFile) ) :
'layout.tmpl';
2014-10-20 16:56:45 -04:00
// set up tutorials for helper
helper.setTutorials(tutorials);
data = helper.prune(data);
data.sort('longname, version, since');
helper.addEventListeners(data);
var sourceFiles = {};
var sourceFilePaths = [];
data().each(function(doclet) {
doclet.attribs = '';
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (doclet.examples) {
doclet.examples = doclet.examples.map(function(example) {
var caption, code;
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (example.match(/^\s*<caption>([\s\S]+?)<\/caption>(\s*[\n\r])([\s\S]+)$/i)) {
caption = RegExp.$1;
code = RegExp.$3;
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
return {
caption: caption || '',
code: code || example
};
});
}
if (doclet.see) {
doclet.see.forEach(function(seeItem, i) {
doclet.see[i] = hashToLink(doclet, seeItem);
});
}
// build a list of source files
var sourcePath;
if (doclet.meta) {
sourcePath = getPathFromDoclet(doclet);
sourceFiles[sourcePath] = {
2014-10-22 10:11:40 -04:00
resolved: sourcePath,
2014-10-20 16:56:45 -04:00
shortened: null
};
2014-10-22 10:11:40 -04:00
if (sourceFilePaths.indexOf(sourcePath) === -1) {
sourceFilePaths.push(sourcePath);
}
2014-10-20 16:56:45 -04:00
}
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// update outdir if necessary, then create outdir
var packageInfo = ( find({kind: 'package'}) || [] ) [0];
if (packageInfo && packageInfo.name) {
outdir = path.join(outdir, packageInfo.name, packageInfo.version);
}
fs.mkPath(outdir);
// copy the template's static files to outdir
var fromDir = path.join(templatePath, 'static');
var staticFiles = fs.ls(fromDir, 3);
staticFiles.forEach(function(fileName) {
var toDir = fs.toDir( fileName.replace(fromDir, outdir) );
fs.mkPath(toDir);
fs.copyFileSync(fileName, toDir);
});
// copy user-specified static files to outdir
var staticFilePaths;
var staticFileFilter;
var staticFileScanner;
if (conf['default'].staticFiles) {
staticFilePaths = conf['default'].staticFiles.paths || [];
staticFileFilter = new (require('jsdoc/src/filter')).Filter(conf['default'].staticFiles);
staticFileScanner = new (require('jsdoc/src/scanner')).Scanner();
staticFilePaths.forEach(function(filePath) {
var extraStaticFiles = staticFileScanner.scan([filePath], 10, staticFileFilter);
extraStaticFiles.forEach(function(fileName) {
2014-10-22 10:11:40 -04:00
var sourcePath = fs.toDir(filePath);
2014-10-20 16:56:45 -04:00
var toDir = fs.toDir( fileName.replace(sourcePath, outdir) );
fs.mkPath(toDir);
fs.copyFileSync(fileName, toDir);
});
});
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (sourceFilePaths.length) {
sourceFiles = shortenPaths( sourceFiles, path.commonPrefix(sourceFilePaths) );
}
data().each(function(doclet) {
var url = helper.createLink(doclet);
helper.registerLink(doclet.longname, url);
2014-10-22 10:11:40 -04:00
// add a shortened version of the full path
2014-10-20 16:56:45 -04:00
var docletPath;
if (doclet.meta) {
docletPath = getPathFromDoclet(doclet);
docletPath = sourceFiles[docletPath].shortened;
if (docletPath) {
2014-10-22 10:11:40 -04:00
doclet.meta.shortpath = docletPath;
2014-10-20 16:56:45 -04:00
}
}
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
data().each(function(doclet) {
var url = helper.longnameToUrl[doclet.longname];
if (url.indexOf('#') > -1) {
doclet.id = helper.longnameToUrl[doclet.longname].split(/#/).pop();
}
else {
doclet.id = doclet.name;
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if ( needsSignature(doclet) ) {
addSignatureParams(doclet);
addSignatureReturns(doclet);
addAttribs(doclet);
}
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// do this after the urls have all been generated
data().each(function(doclet) {
doclet.ancestors = getAncestorLinks(doclet);
if (doclet.kind === 'member') {
addSignatureTypes(doclet);
addAttribs(doclet);
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
if (doclet.kind === 'constant') {
addSignatureTypes(doclet);
addAttribs(doclet);
doclet.kind = 'member';
}
});
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var members = helper.getMembers(data);
members.tutorials = tutorials.children;
2014-10-22 10:11:40 -04:00
// output pretty-printed source files by default
var outputSourceFiles = conf['default'] && conf['default'].outputSourceFiles !== false ? true :
false;
2014-10-20 16:56:45 -04:00
// add template helpers
view.find = find;
view.linkto = linkto;
view.resolveAuthorLinks = resolveAuthorLinks;
view.tutoriallink = tutoriallink;
view.htmlsafe = htmlsafe;
2014-10-22 10:11:40 -04:00
view.outputSourceFiles = outputSourceFiles;
2014-10-20 16:56:45 -04:00
// once for all
view.nav = buildNav(members);
attachModuleSymbols( find({ kind: ['class', 'function'], longname: {left: 'module:'} }),
members.modules );
2014-10-22 10:11:40 -04:00
// generate the pretty-printed source files first so other pages can link to them
if (outputSourceFiles) {
2014-10-20 16:56:45 -04:00
generateSourceFiles(sourceFiles, opts.encoding);
}
if (members.globals.length) { generate('Global', [{kind: 'globalobj'}], globalUrl); }
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// index page displays information from package.json and lists files
var files = find({kind: 'file'}),
packages = find({kind: 'package'});
generate('Index',
packages.concat(
[{kind: 'mainpage', readme: opts.readme, longname: (opts.mainpagetitle) ? opts.mainpagetitle : 'Main Page'}]
).concat(files),
indexUrl);
// set up the lists that we'll use to generate pages
var classes = taffy(members.classes);
var modules = taffy(members.modules);
var namespaces = taffy(members.namespaces);
var mixins = taffy(members.mixins);
var externals = taffy(members.externals);
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
Object.keys(helper.longnameToUrl).forEach(function(longname) {
var myClasses = helper.find(classes, {longname: longname});
if (myClasses.length) {
generate('Class: ' + myClasses[0].name, myClasses, helper.longnameToUrl[longname]);
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var myModules = helper.find(modules, {longname: longname});
if (myModules.length) {
generate('Module: ' + myModules[0].name, myModules, helper.longnameToUrl[longname]);
}
var myNamespaces = helper.find(namespaces, {longname: longname});
if (myNamespaces.length) {
generate('Namespace: ' + myNamespaces[0].name, myNamespaces, helper.longnameToUrl[longname]);
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var myMixins = helper.find(mixins, {longname: longname});
if (myMixins.length) {
generate('Mixin: ' + myMixins[0].name, myMixins, helper.longnameToUrl[longname]);
}
var myExternals = helper.find(externals, {longname: longname});
if (myExternals.length) {
generate('External: ' + myExternals[0].name, myExternals, helper.longnameToUrl[longname]);
}
});
// TODO: move the tutorial functions to templateHelper.js
function generateTutorial(title, tutorial, filename) {
var tutorialData = {
title: title,
header: tutorial.title,
content: tutorial.parse(),
children: tutorial.children
};
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
var tutorialPath = path.join(outdir, filename),
html = view.render('tutorial.tmpl', tutorialData);
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// yes, you can use {@link} in tutorials too!
html = helper.resolveLinks(html); // turn {@link foo} into <a href="foodoc.html">foo</a>
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
fs.writeFileSync(tutorialPath, html, 'utf8');
}
2014-10-22 10:11:40 -04:00
2014-10-20 16:56:45 -04:00
// tutorials can have only one parent so there is no risk for loops
function saveChildren(node) {
node.children.forEach(function(child) {
generateTutorial('Tutorial: ' + child.title, child, helper.tutorialToUrl(child.name));
saveChildren(child);
});
}
saveChildren(tutorials);
};