|
|
|
'use strict'; |
|
|
|
var template = require('jsdoc/template'), |
|
fs = require('jsdoc/fs'), |
|
path = require('jsdoc/path'), |
|
taffy = require('taffydb').taffy, |
|
logger = require('jsdoc/util/logger'), |
|
helper = require('jsdoc/util/templateHelper'), |
|
htmlsafe = helper.htmlsafe, |
|
linkto = helper.linkto, |
|
resolveAuthorLinks = helper.resolveAuthorLinks, |
|
scopeToPunc = helper.scopeToPunc, |
|
hasOwnProp = Object.prototype.hasOwnProperty, |
|
data, |
|
view, |
|
outdir = env.opts.destination; |
|
|
|
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 getCategoryLink(className, cat) { |
|
return '<a href="' + className + '.html#' + cat.toLowerCase().replace(/[^a-z0-9]/gi, '-') + '-methods">' + cat + ' methods</a>'; |
|
} |
|
|
|
function hashToLink(doclet, hash) { |
|
if ( !/^(#.+)/.test(hash) ) { return hash; } |
|
|
|
var url = helper.createLink(doclet); |
|
|
|
url = url.replace(/(#.+|$)/, hash); |
|
return '<a href="' + url + '">' + hash + '</a>'; |
|
} |
|
|
|
function needsSignature(doclet) { |
|
var needsSig = false; |
|
|
|
|
|
if (doclet.kind === 'function' || doclet.kind === 'class') { |
|
needsSig = true; |
|
} |
|
|
|
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; |
|
} |
|
|
|
function addSignatureParams(f) { |
|
var params = helper.getSignatureParams(f, 'optional'); |
|
|
|
f.signature = (f.signature || '') + '('+params.join(', ')+')'; |
|
} |
|
|
|
function addSignatureReturns(f) { |
|
var returnTypes = helper.getSignatureReturns(f); |
|
|
|
f.signature = '<span class="signature">' + (f.signature || '') + '</span>' + |
|
'<span class="type-signature">' + |
|
(returnTypes && returnTypes.length ? ' → {' + returnTypes.join('|') + '}' : '') + |
|
'</span>'; |
|
} |
|
|
|
function addSignatureTypes(f) { |
|
var types = helper.getSignatureTypes(f); |
|
|
|
f.signature = (f.signature || '') + '<span class="type-signature">'+(types.length? ' :'+types.join('|') : '')+'</span>'; |
|
} |
|
|
|
function addAttribs(f) { |
|
var attribs = helper.getAttribs(f); |
|
|
|
f.attribs = '<span class="type-signature">' + htmlsafe(attribs.length ? |
|
|
|
'<' + attribs.join(', ').replace('virtual', 'abstract') + '> ' : '') + '</span>'; |
|
} |
|
|
|
function shortenPaths(files, commonPrefix) { |
|
Object.keys(files).forEach(function(file) { |
|
files[file].shortened = files[file].resolved.replace(commonPrefix, '') |
|
|
|
.replace(/\\/g, '/'); |
|
}); |
|
|
|
return files; |
|
} |
|
|
|
function getPathFromDoclet(doclet) { |
|
if (!doclet.meta) { |
|
return; |
|
} |
|
|
|
return doclet.meta.path && doclet.meta.path !== 'null' ? |
|
path.join(doclet.meta.path, doclet.meta.filename) : |
|
doclet.meta.filename; |
|
} |
|
|
|
function generate(title, docs, filename, resolveLinks) { |
|
resolveLinks = resolveLinks === false ? false : true; |
|
|
|
var docData = { |
|
title: title, |
|
docs: docs |
|
}; |
|
|
|
var outpath = path.join(outdir, filename), |
|
html = view.render('container.tmpl', docData); |
|
|
|
if (resolveLinks) { |
|
html = helper.resolveLinks(html); |
|
} |
|
|
|
|
|
html = html.replace(/<pre>/g, '<pre class="prettyprint">'); |
|
|
|
fs.writeFileSync(outpath, html, 'utf8'); |
|
} |
|
|
|
function generateSourceFiles(sourceFiles, encoding) { |
|
encoding = encoding || 'utf8'; |
|
Object.keys(sourceFiles).forEach(function(file) { |
|
var source; |
|
|
|
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) { |
|
logger.error('Error while generating source file %s: %s', file, e.message); |
|
} |
|
|
|
generate('Source: ' + sourceFiles[file].shortened, [source], sourceOutfile, |
|
false); |
|
}); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function attachModuleSymbols(doclets, modules) { |
|
var symbols = {}; |
|
|
|
|
|
doclets.forEach(function(symbol) { |
|
symbols[symbol.longname] = symbol; |
|
}); |
|
|
|
return modules.map(function(module) { |
|
if (symbols[module.longname]) { |
|
module.module = symbols[module.longname]; |
|
module.module.name = module.module.name.replace('module:', 'require("') + '")'; |
|
} |
|
}); |
|
} |
|
|
|
function buildReadmeNav(readme) { |
|
var nav = ''; |
|
|
|
var prevLevel = '0'; |
|
nav += '<ul>'; |
|
|
|
readme = readme.replace(/<h([23])>([^<]*)<\/h[23]>/g, function(match, level, title) { |
|
if (title.trim().length > 0) { |
|
var titlelink = title.toLowerCase().replace(/[^a-z]/g, '-'); |
|
|
|
if (level === '2') { |
|
if (prevLevel === '2' || prevLevel === '3') { |
|
nav += '</ul>'; |
|
} |
|
|
|
nav += '<li><a href="index.html#' + titlelink + '">' + title + '</a></li><ul>'; |
|
} else { |
|
nav += '<li><a href="index.html#' + titlelink + '">' + title + '</a></li>'; |
|
} |
|
|
|
prevLevel = level; |
|
match = '<a name="' + titlelink + '"></a>' + match; |
|
} |
|
|
|
return match; |
|
}); |
|
|
|
nav += '</ul></ul>'; |
|
return { nav: nav, readme: readme }; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function buildNav(readmeNav, members) { |
|
var nav = '<h2><a href="index.html">Index</a></h2>' + readmeNav, |
|
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) ) { |
|
nav += '<li>'+linkto(m.longname, m.name)+'</li>'; |
|
} |
|
seen[m.longname] = true; |
|
}); |
|
|
|
nav += '</ul>'; |
|
} |
|
|
|
if (members.externals.length) { |
|
nav += '<h3>Externals</h3><ul>'; |
|
members.externals.forEach(function(e) { |
|
if ( !hasOwnProp.call(seen, e.longname) ) { |
|
nav += '<li>'+linkto( e.longname, e.name.replace(/(^"|"$)/g, '') )+'</li>'; |
|
} |
|
seen[e.longname] = true; |
|
}); |
|
|
|
nav += '</ul>'; |
|
} |
|
|
|
if (members.classes.length) { |
|
members.classes.forEach(function(c) { |
|
if ( !hasOwnProp.call(seen, c.longname) ) { |
|
classNav += '<li>'+linkto(c.longname, c.name)+'</li>'; |
|
if (c.longname in members.categories) { |
|
classNav += '<ul>' + members.categories[c.longname].reduce(function(nav, cat) { |
|
return nav + '<li> ' + getCategoryLink(c.longname, cat) + '</li>'; |
|
}, '') + '</ul>'; |
|
} |
|
} |
|
seen[c.longname] = true; |
|
}); |
|
|
|
if (classNav !== '') { |
|
nav += '<h3>Classes</h3><ul>'; |
|
nav += classNav; |
|
nav += '</ul>'; |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (members.namespaces.length) { |
|
nav += '<h3>Namespaces</h3><ul>'; |
|
members.namespaces.forEach(function(n) { |
|
if ( !hasOwnProp.call(seen, n.longname) ) { |
|
nav += '<li>'+linkto(n.longname, n.name)+'</li>'; |
|
} |
|
seen[n.longname] = true; |
|
}); |
|
|
|
nav += '</ul>'; |
|
} |
|
|
|
if (members.mixins.length) { |
|
nav += '<h3>Mixins</h3><ul>'; |
|
members.mixins.forEach(function(m) { |
|
if ( !hasOwnProp.call(seen, m.longname) ) { |
|
nav += '<li>'+linkto(m.longname, m.name)+'</li>'; |
|
} |
|
seen[m.longname] = true; |
|
}); |
|
|
|
nav += '</ul>'; |
|
} |
|
|
|
if (members.tutorials.length) { |
|
nav += '<h3>Tutorials</h3><ul>'; |
|
members.tutorials.forEach(function(t) { |
|
nav += '<li>'+tutoriallink(t.name)+'</li>'; |
|
}); |
|
|
|
nav += '</ul>'; |
|
} |
|
|
|
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; |
|
}); |
|
|
|
if (!globalNav) { |
|
|
|
nav += '<h3>' + linkto('global', 'Global') + '</h3>'; |
|
} |
|
else { |
|
nav += '<h3>Global</h3><ul>' + globalNav + '</ul>'; |
|
} |
|
} |
|
|
|
return nav; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
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'); |
|
|
|
|
|
|
|
var indexUrl = helper.getUniqueFilename('index'); |
|
|
|
|
|
var globalUrl = helper.getUniqueFilename('global'); |
|
helper.registerLink('global', globalUrl); |
|
|
|
|
|
view.layout = conf['default'].layoutFile ? |
|
path.getResourcePath(path.dirname(conf['default'].layoutFile), |
|
path.basename(conf['default'].layoutFile) ) : |
|
'layout.tmpl'; |
|
|
|
|
|
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 = ''; |
|
|
|
if (doclet.examples) { |
|
doclet.examples = doclet.examples.map(function(example) { |
|
var caption, code; |
|
|
|
if (example.match(/^\s*<caption>([\s\S]+?)<\/caption>(\s*[\n\r])([\s\S]+)$/i)) { |
|
caption = RegExp.$1; |
|
code = RegExp.$3; |
|
} |
|
|
|
return { |
|
caption: caption || '', |
|
code: code || example |
|
}; |
|
}); |
|
} |
|
if (doclet.see) { |
|
doclet.see.forEach(function(seeItem, i) { |
|
doclet.see[i] = hashToLink(doclet, seeItem); |
|
}); |
|
} |
|
|
|
|
|
var sourcePath; |
|
if (doclet.meta) { |
|
sourcePath = getPathFromDoclet(doclet); |
|
sourceFiles[sourcePath] = { |
|
resolved: sourcePath, |
|
shortened: null |
|
}; |
|
if (sourceFilePaths.indexOf(sourcePath) === -1) { |
|
sourceFilePaths.push(sourcePath); |
|
} |
|
} |
|
}); |
|
|
|
|
|
var packageInfo = ( find({kind: 'package'}) || [] ) [0]; |
|
if (packageInfo && packageInfo.name) { |
|
outdir = path.join(outdir, packageInfo.name, packageInfo.version); |
|
} |
|
fs.mkPath(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); |
|
}); |
|
|
|
|
|
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) { |
|
var sourcePath = fs.toDir(filePath); |
|
var toDir = fs.toDir( fileName.replace(sourcePath, outdir) ); |
|
fs.mkPath(toDir); |
|
fs.copyFileSync(fileName, toDir); |
|
}); |
|
}); |
|
} |
|
|
|
if (sourceFilePaths.length) { |
|
sourceFiles = shortenPaths( sourceFiles, path.commonPrefix(sourceFilePaths) ); |
|
} |
|
data().each(function(doclet) { |
|
var url = helper.createLink(doclet); |
|
helper.registerLink(doclet.longname, url); |
|
|
|
|
|
var docletPath; |
|
if (doclet.meta) { |
|
docletPath = getPathFromDoclet(doclet); |
|
docletPath = sourceFiles[docletPath].shortened; |
|
if (docletPath) { |
|
doclet.meta.shortpath = docletPath; |
|
} |
|
} |
|
}); |
|
|
|
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; |
|
} |
|
|
|
if ( needsSignature(doclet) ) { |
|
addSignatureParams(doclet); |
|
addSignatureReturns(doclet); |
|
addAttribs(doclet); |
|
} |
|
}); |
|
|
|
|
|
data().each(function(doclet) { |
|
doclet.ancestors = getAncestorLinks(doclet); |
|
|
|
if (doclet.kind === 'member') { |
|
addSignatureTypes(doclet); |
|
addAttribs(doclet); |
|
} |
|
|
|
if (doclet.kind === 'constant') { |
|
addSignatureTypes(doclet); |
|
addAttribs(doclet); |
|
doclet.kind = 'member'; |
|
} |
|
}); |
|
|
|
var members = helper.getMembers(data); |
|
members.tutorials = tutorials.children; |
|
members.categories = data('method').get().reduce(function(cats, method) { |
|
if (!(method.memberof in cats)) { |
|
cats[method.memberof] = []; |
|
} |
|
|
|
var cat = method.category || 'Other'; |
|
if (cats[method.memberof].indexOf(cat) === -1) { |
|
cats[method.memberof].push(cat); |
|
cats[method.memberof] = cats[method.memberof].sort(); |
|
} |
|
|
|
return cats; |
|
}, {}); |
|
|
|
|
|
var outputSourceFiles = conf['default'] && conf['default'].outputSourceFiles !== false ? true : |
|
false; |
|
|
|
|
|
view.find = find; |
|
view.linkto = linkto; |
|
view.resolveAuthorLinks = resolveAuthorLinks; |
|
view.tutoriallink = tutoriallink; |
|
view.htmlsafe = htmlsafe; |
|
view.outputSourceFiles = outputSourceFiles; |
|
|
|
|
|
var readmeNav = buildReadmeNav(opts.readme); |
|
opts.readme = readmeNav.readme; |
|
|
|
|
|
view.nav = buildNav(readmeNav.nav, members); |
|
attachModuleSymbols( find({ kind: ['class', 'function'], longname: {left: 'module:'} }), |
|
members.modules ); |
|
|
|
|
|
if (outputSourceFiles) { |
|
generateSourceFiles(sourceFiles, opts.encoding); |
|
} |
|
|
|
if (members.globals.length) { generate('Global', [{kind: 'globalobj'}], globalUrl); } |
|
|
|
|
|
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); |
|
|
|
|
|
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); |
|
|
|
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]); |
|
} |
|
|
|
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]); |
|
} |
|
|
|
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]); |
|
} |
|
}); |
|
|
|
|
|
function generateTutorial(title, tutorial, filename) { |
|
var tutorialData = { |
|
title: title, |
|
header: tutorial.title, |
|
content: tutorial.parse(), |
|
children: tutorial.children |
|
}; |
|
|
|
var tutorialPath = path.join(outdir, filename), |
|
html = view.render('tutorial.tmpl', tutorialData); |
|
|
|
|
|
html = helper.resolveLinks(html); |
|
|
|
fs.writeFileSync(tutorialPath, html, 'utf8'); |
|
} |
|
|
|
|
|
function saveChildren(node) { |
|
node.children.forEach(function(child) { |
|
generateTutorial('Tutorial: ' + child.title, child, helper.tutorialToUrl(child.name)); |
|
saveChildren(child); |
|
}); |
|
} |
|
saveChildren(tutorials); |
|
}; |
|
|