2017-01-03 21:16:48 +00:00
|
|
|
// Copyright Joyent, Inc. and other Node contributors.
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a
|
|
|
|
// copy of this software and associated documentation files (the
|
|
|
|
// "Software"), to deal in the Software without restriction, including
|
|
|
|
// without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
|
|
|
// persons to whom the Software is furnished to do so, subject to the
|
|
|
|
// following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included
|
|
|
|
// in all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
|
|
|
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
|
|
|
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
|
|
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
|
|
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
|
|
|
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
2016-01-30 18:17:57 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-11-14 02:40:38 +00:00
|
|
|
const common = require('./common.js');
|
|
|
|
const fs = require('fs');
|
|
|
|
const marked = require('marked');
|
|
|
|
const path = require('path');
|
|
|
|
const preprocess = require('./preprocess.js');
|
|
|
|
const typeParser = require('./type-parser.js');
|
2012-02-27 18:59:01 +00:00
|
|
|
|
|
|
|
module.exports = toHTML;
|
|
|
|
|
2016-09-02 20:27:01 +00:00
|
|
|
const STABILITY_TEXT_REG_EXP = /(.*:)\s*(\d)([\s\S]*)/;
|
2017-01-23 03:16:21 +00:00
|
|
|
const DOC_CREATED_REG_EXP = /<!--\s*introduced_in\s*=\s*v([0-9]+)\.([0-9]+)\.([0-9]+)\s*-->/;
|
2016-09-02 20:27:01 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Customized heading without id attribute.
|
2017-06-19 15:55:10 +00:00
|
|
|
const renderer = new marked.Renderer();
|
2016-04-25 23:44:37 +00:00
|
|
|
renderer.heading = function(text, level) {
|
2017-10-06 17:42:22 +00:00
|
|
|
return `<h${level}>${text}</h${level}>\n`;
|
2016-04-25 23:44:37 +00:00
|
|
|
};
|
|
|
|
marked.setOptions({
|
|
|
|
renderer: renderer
|
|
|
|
});
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// TODO(chrisdickinson): never stop vomiting / fix this.
|
2017-06-19 15:55:10 +00:00
|
|
|
const gtocPath = path.resolve(path.join(
|
2016-01-30 18:17:57 +00:00
|
|
|
__dirname,
|
2016-04-26 06:32:35 +00:00
|
|
|
'..',
|
|
|
|
'..',
|
|
|
|
'doc',
|
|
|
|
'api',
|
|
|
|
'_toc.md'
|
2016-01-30 18:17:57 +00:00
|
|
|
));
|
2015-01-12 05:40:45 +00:00
|
|
|
var gtocLoading = null;
|
|
|
|
var gtocData = null;
|
2017-01-23 03:16:21 +00:00
|
|
|
var docCreated = null;
|
|
|
|
var nodeVersion = null;
|
2015-01-12 05:40:45 +00:00
|
|
|
|
2016-05-10 22:40:31 +00:00
|
|
|
/**
|
|
|
|
* opts: input, filename, template, nodeVersion.
|
|
|
|
*/
|
|
|
|
function toHTML(opts, cb) {
|
2017-06-19 15:55:10 +00:00
|
|
|
const template = opts.template;
|
2017-01-23 03:16:21 +00:00
|
|
|
|
|
|
|
nodeVersion = opts.nodeVersion || process.version;
|
|
|
|
docCreated = opts.input.match(DOC_CREATED_REG_EXP);
|
2015-11-18 22:40:03 +00:00
|
|
|
|
2015-01-12 05:40:45 +00:00
|
|
|
if (gtocData) {
|
|
|
|
return onGtocLoaded();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gtocLoading === null) {
|
|
|
|
gtocLoading = [onGtocLoaded];
|
|
|
|
return loadGtoc(function(err, data) {
|
|
|
|
if (err) throw err;
|
|
|
|
gtocData = data;
|
|
|
|
gtocLoading.forEach(function(xs) {
|
|
|
|
xs();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (gtocLoading) {
|
|
|
|
return gtocLoading.push(onGtocLoaded);
|
|
|
|
}
|
|
|
|
|
|
|
|
function onGtocLoaded() {
|
2017-06-19 15:55:10 +00:00
|
|
|
const lexed = marked.lexer(opts.input);
|
2015-01-12 05:40:45 +00:00
|
|
|
fs.readFile(template, 'utf8', function(er, template) {
|
|
|
|
if (er) return cb(er);
|
2016-05-10 22:40:31 +00:00
|
|
|
render({
|
|
|
|
lexed: lexed,
|
|
|
|
filename: opts.filename,
|
|
|
|
template: template,
|
|
|
|
nodeVersion: nodeVersion,
|
2017-01-25 20:54:34 +00:00
|
|
|
analytics: opts.analytics,
|
2016-05-10 22:40:31 +00:00
|
|
|
}, cb);
|
2015-01-12 05:40:45 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function loadGtoc(cb) {
|
|
|
|
fs.readFile(gtocPath, 'utf8', function(err, data) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
preprocess(gtocPath, data, function(err, data) {
|
|
|
|
if (err) return cb(err);
|
|
|
|
|
|
|
|
data = marked(data).replace(/<a href="(.*?)"/gm, function(a, m) {
|
2017-10-06 17:42:22 +00:00
|
|
|
return `<a class="nav-${toID(m)}" href="${m}"`;
|
2015-01-12 05:40:45 +00:00
|
|
|
});
|
|
|
|
return cb(null, data);
|
|
|
|
});
|
2012-02-27 18:59:01 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-01-12 05:40:45 +00:00
|
|
|
function toID(filename) {
|
2016-01-30 18:17:57 +00:00
|
|
|
return filename
|
|
|
|
.replace('.html', '')
|
2016-11-14 07:23:35 +00:00
|
|
|
.replace(/[^\w-]/g, '-')
|
2016-01-30 18:17:57 +00:00
|
|
|
.replace(/-+/g, '-');
|
2015-01-12 05:40:45 +00:00
|
|
|
}
|
|
|
|
|
2016-05-10 22:40:31 +00:00
|
|
|
/**
|
|
|
|
* opts: lexed, filename, template, nodeVersion.
|
|
|
|
*/
|
|
|
|
function render(opts, cb) {
|
2017-11-18 20:04:05 +00:00
|
|
|
var { lexed, filename, template } = opts;
|
2017-06-19 15:55:10 +00:00
|
|
|
const nodeVersion = opts.nodeVersion || process.version;
|
2015-11-18 22:40:03 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Get the section.
|
2017-06-19 15:55:10 +00:00
|
|
|
const section = getSection(lexed);
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2016-04-20 22:12:40 +00:00
|
|
|
filename = path.basename(filename, '.md');
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2016-02-04 12:11:17 +00:00
|
|
|
parseText(lexed);
|
2012-02-27 18:59:01 +00:00
|
|
|
lexed = parseLists(lexed);
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Generate the table of contents.
|
|
|
|
// This mutates the lexed contents in-place.
|
2012-02-27 18:59:01 +00:00
|
|
|
buildToc(lexed, filename, function(er, toc) {
|
|
|
|
if (er) return cb(er);
|
|
|
|
|
2017-06-19 15:55:10 +00:00
|
|
|
const id = toID(path.basename(filename));
|
2015-01-12 05:40:45 +00:00
|
|
|
|
|
|
|
template = template.replace(/__ID__/g, id);
|
2012-02-27 18:59:01 +00:00
|
|
|
template = template.replace(/__FILENAME__/g, filename);
|
2016-08-01 17:31:28 +00:00
|
|
|
template = template.replace(/__SECTION__/g, section || 'Index');
|
2016-05-10 18:56:56 +00:00
|
|
|
template = template.replace(/__VERSION__/g, nodeVersion);
|
2012-02-27 18:59:01 +00:00
|
|
|
template = template.replace(/__TOC__/g, toc);
|
2015-01-12 05:40:45 +00:00
|
|
|
template = template.replace(
|
|
|
|
/__GTOC__/g,
|
2017-10-06 17:42:22 +00:00
|
|
|
gtocData.replace(`class="nav-${id}`, `class="nav-${id} active`)
|
2015-01-12 05:40:45 +00:00
|
|
|
);
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2017-01-25 20:54:34 +00:00
|
|
|
if (opts.analytics) {
|
|
|
|
template = template.replace(
|
|
|
|
'<!-- __TRACKING__ -->',
|
|
|
|
analyticsScript(opts.analytics)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-01-23 03:16:21 +00:00
|
|
|
template = template.replace(/__ALTDOCS__/, altDocs(filename));
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Content has to be the last thing we do with the lexed tokens,
|
|
|
|
// because it's destructive.
|
2016-01-30 18:17:57 +00:00
|
|
|
const content = marked.parser(lexed);
|
2012-02-27 18:59:01 +00:00
|
|
|
template = template.replace(/__CONTENT__/g, content);
|
|
|
|
|
|
|
|
cb(null, template);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-01-25 20:54:34 +00:00
|
|
|
function analyticsScript(analytics) {
|
|
|
|
return `
|
|
|
|
<script src="assets/dnt_helper.js"></script>
|
|
|
|
<script>
|
|
|
|
if (!_dntEnabled()) {
|
|
|
|
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;
|
|
|
|
i[r]=i[r]||function(){(i[r].q=i[r].q||[]).push(arguments)},
|
|
|
|
i[r].l=1*new Date();a=s.createElement(o),m=s.getElementsByTagName(o)[0];
|
|
|
|
a.async=1;a.src=g;m.parentNode.insertBefore(a,m)})(window,document,
|
|
|
|
'script','//www.google-analytics.com/analytics.js','ga');
|
|
|
|
ga('create', '${analytics}', 'auto');
|
|
|
|
ga('send', 'pageview');
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Replace placeholders in text tokens.
|
2017-04-19 17:22:52 +00:00
|
|
|
function replaceInText(text) {
|
|
|
|
return linkJsTypeDocs(linkManPages(text));
|
|
|
|
}
|
|
|
|
|
2017-01-23 03:16:21 +00:00
|
|
|
function altDocs(filename) {
|
|
|
|
if (!docCreated) {
|
|
|
|
console.error(`Failed to add alternative version links to ${filename}`);
|
2017-08-28 14:57:33 +00:00
|
|
|
return '';
|
2017-01-23 03:16:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function lte(v) {
|
|
|
|
const ns = v.num.split('.');
|
|
|
|
if (docCreated[1] > +ns[0])
|
|
|
|
return false;
|
|
|
|
if (docCreated[1] < +ns[0])
|
|
|
|
return true;
|
|
|
|
return docCreated[2] <= +ns[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
const versions = [
|
2017-11-01 19:20:14 +00:00
|
|
|
{ num: '9.x' },
|
|
|
|
{ num: '8.x', lts: true },
|
2017-01-23 03:16:21 +00:00
|
|
|
{ num: '7.x' },
|
|
|
|
{ num: '6.x', lts: true },
|
|
|
|
{ num: '5.x' },
|
|
|
|
{ num: '4.x', lts: true },
|
|
|
|
{ num: '0.12.x' },
|
|
|
|
{ num: '0.10.x' }
|
|
|
|
];
|
|
|
|
|
|
|
|
const host = 'https://nodejs.org';
|
|
|
|
const href = (v) => `${host}/docs/latest-v${v.num}/api/${filename}.html`;
|
|
|
|
|
2017-11-09 14:27:12 +00:00
|
|
|
function li(v) {
|
2017-01-23 03:16:21 +00:00
|
|
|
let html = `<li><a href="${href(v)}">${v.num}`;
|
|
|
|
|
|
|
|
if (v.lts)
|
|
|
|
html += ' <b>LTS</b>';
|
|
|
|
|
|
|
|
return html + '</a></li>';
|
|
|
|
}
|
|
|
|
|
2017-09-14 00:37:32 +00:00
|
|
|
const lis = versions.filter(lte).map(li).join('\n');
|
2017-01-23 03:16:21 +00:00
|
|
|
|
2017-09-14 00:37:32 +00:00
|
|
|
if (!lis.length)
|
|
|
|
return '';
|
|
|
|
|
|
|
|
return `
|
|
|
|
<li class="version-picker">
|
|
|
|
<a href="#">View another version <span>▼</span></a>
|
|
|
|
<ol class="version-picker">${lis}</ol>
|
|
|
|
</li>
|
|
|
|
`;
|
2017-01-23 03:16:21 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Handle general body-text replacements.
|
|
|
|
// For example, link man page references to the actual page.
|
2016-02-04 12:11:17 +00:00
|
|
|
function parseText(lexed) {
|
|
|
|
lexed.forEach(function(tok) {
|
2017-04-19 17:22:52 +00:00
|
|
|
if (tok.type === 'table') {
|
|
|
|
if (tok.cells) {
|
|
|
|
tok.cells.forEach((row, x) => {
|
|
|
|
row.forEach((_, y) => {
|
|
|
|
if (tok.cells[x] && tok.cells[x][y]) {
|
|
|
|
tok.cells[x][y] = replaceInText(tok.cells[x][y]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tok.header) {
|
|
|
|
tok.header.forEach((_, i) => {
|
|
|
|
if (tok.header[i]) {
|
|
|
|
tok.header[i] = replaceInText(tok.header[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else if (tok.text && tok.type !== 'code') {
|
|
|
|
tok.text = replaceInText(tok.text);
|
2016-02-04 12:11:17 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Just update the list item text in-place.
|
|
|
|
// Lists that come right after a heading are what we're after.
|
2012-02-27 18:59:01 +00:00
|
|
|
function parseLists(input) {
|
|
|
|
var state = null;
|
2017-06-19 15:55:10 +00:00
|
|
|
const savedState = [];
|
2012-02-27 18:59:01 +00:00
|
|
|
var depth = 0;
|
2017-06-19 15:55:10 +00:00
|
|
|
const output = [];
|
2016-09-02 20:27:01 +00:00
|
|
|
let headingIndex = -1;
|
|
|
|
let heading = null;
|
|
|
|
|
2012-02-27 18:59:01 +00:00
|
|
|
output.links = input.links;
|
2016-09-02 20:27:01 +00:00
|
|
|
input.forEach(function(tok, index) {
|
2016-07-15 22:35:38 +00:00
|
|
|
if (tok.type === 'blockquote_start') {
|
|
|
|
savedState.push(state);
|
|
|
|
state = 'MAYBE_STABILITY_BQ';
|
2012-12-28 01:32:53 +00:00
|
|
|
return;
|
|
|
|
}
|
2016-07-15 22:35:38 +00:00
|
|
|
if (tok.type === 'blockquote_end' && state === 'MAYBE_STABILITY_BQ') {
|
|
|
|
state = savedState.pop();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if ((tok.type === 'paragraph' && state === 'MAYBE_STABILITY_BQ') ||
|
|
|
|
tok.type === 'code') {
|
|
|
|
if (tok.text.match(/Stability:.*/g)) {
|
2016-09-02 20:27:01 +00:00
|
|
|
const stabilityMatch = tok.text.match(STABILITY_TEXT_REG_EXP);
|
|
|
|
const stability = Number(stabilityMatch[2]);
|
|
|
|
const isStabilityIndex =
|
2018-03-30 10:38:45 +00:00
|
|
|
index - 2 === headingIndex || // General.
|
|
|
|
index - 3 === headingIndex; // With api_metadata block.
|
2016-09-02 20:27:01 +00:00
|
|
|
|
|
|
|
if (heading && isStabilityIndex) {
|
|
|
|
heading.stability = stability;
|
|
|
|
headingIndex = -1;
|
|
|
|
heading = null;
|
|
|
|
}
|
2017-01-30 18:44:59 +00:00
|
|
|
tok.text = parseAPIHeader(tok.text).replace(/\n/g, ' ');
|
2016-07-15 22:35:38 +00:00
|
|
|
output.push({ type: 'html', text: tok.text });
|
|
|
|
return;
|
|
|
|
} else if (state === 'MAYBE_STABILITY_BQ') {
|
|
|
|
output.push({ type: 'blockquote_start' });
|
|
|
|
state = savedState.pop();
|
|
|
|
}
|
|
|
|
}
|
2016-01-19 15:59:55 +00:00
|
|
|
if (state === null ||
|
|
|
|
(state === 'AFTERHEADING' && tok.type === 'heading')) {
|
2012-02-27 18:59:01 +00:00
|
|
|
if (tok.type === 'heading') {
|
2016-09-02 20:27:01 +00:00
|
|
|
headingIndex = index;
|
|
|
|
heading = tok;
|
2012-02-27 18:59:01 +00:00
|
|
|
state = 'AFTERHEADING';
|
|
|
|
}
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state === 'AFTERHEADING') {
|
|
|
|
if (tok.type === 'list_start') {
|
|
|
|
state = 'LIST';
|
|
|
|
if (depth === 0) {
|
2016-05-05 05:20:27 +00:00
|
|
|
output.push({ type: 'html', text: '<div class="signature">' });
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
depth++;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
2015-11-14 02:40:38 +00:00
|
|
|
if (tok.type === 'html' && common.isYAMLBlock(tok.text)) {
|
|
|
|
tok.text = parseYAML(tok.text);
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
state = null;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (state === 'LIST') {
|
|
|
|
if (tok.type === 'list_start') {
|
|
|
|
depth++;
|
|
|
|
output.push(tok);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (tok.type === 'list_end') {
|
|
|
|
depth--;
|
2016-03-03 08:18:09 +00:00
|
|
|
output.push(tok);
|
2012-02-27 18:59:01 +00:00
|
|
|
if (depth === 0) {
|
|
|
|
state = null;
|
2016-05-05 05:20:27 +00:00
|
|
|
output.push({ type: 'html', text: '</div>' });
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
output.push(tok);
|
|
|
|
});
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2015-11-14 02:40:38 +00:00
|
|
|
function parseYAML(text) {
|
|
|
|
const meta = common.extractAndParseYAML(text);
|
2016-04-30 23:51:38 +00:00
|
|
|
const html = ['<div class="api_metadata">'];
|
2015-11-14 02:40:38 +00:00
|
|
|
|
2017-01-10 19:43:57 +00:00
|
|
|
const added = { description: '' };
|
|
|
|
const deprecated = { description: '' };
|
|
|
|
|
2016-04-30 23:51:38 +00:00
|
|
|
if (meta.added) {
|
2017-01-10 19:43:57 +00:00
|
|
|
added.version = meta.added.join(', ');
|
|
|
|
added.description = `<span>Added in: ${added.version}</span>`;
|
2016-04-30 23:51:38 +00:00
|
|
|
}
|
2015-11-14 02:40:38 +00:00
|
|
|
|
2016-04-30 23:51:38 +00:00
|
|
|
if (meta.deprecated) {
|
2017-01-10 19:43:57 +00:00
|
|
|
deprecated.version = meta.deprecated.join(', ');
|
|
|
|
deprecated.description =
|
|
|
|
`<span>Deprecated since: ${deprecated.version}</span>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (meta.changes.length > 0) {
|
|
|
|
let changes = meta.changes.slice();
|
|
|
|
if (added.description) changes.push(added);
|
|
|
|
if (deprecated.description) changes.push(deprecated);
|
|
|
|
|
|
|
|
changes = changes.sort((a, b) => versionSort(a.version, b.version));
|
|
|
|
|
|
|
|
html.push('<details class="changelog"><summary>History</summary>');
|
|
|
|
html.push('<table>');
|
|
|
|
html.push('<tr><th>Version</th><th>Changes</th></tr>');
|
|
|
|
|
|
|
|
changes.forEach((change) => {
|
|
|
|
html.push(`<tr><td>${change.version}</td>`);
|
|
|
|
html.push(`<td>${marked(change.description)}</td></tr>`);
|
|
|
|
});
|
|
|
|
|
|
|
|
html.push('</table>');
|
|
|
|
html.push('</details>');
|
|
|
|
} else {
|
|
|
|
html.push(`${added.description}${deprecated.description}`);
|
2015-11-14 02:40:38 +00:00
|
|
|
}
|
|
|
|
|
2016-04-30 23:51:38 +00:00
|
|
|
html.push('</div>');
|
|
|
|
return html.join('\n');
|
2015-11-14 02:40:38 +00:00
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Syscalls which appear in the docs, but which only exist in BSD / macOS.
|
2017-06-19 15:55:10 +00:00
|
|
|
const BSD_ONLY_SYSCALLS = new Set(['lchmod']);
|
2016-02-04 12:11:17 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Handle references to man pages, eg "open(2)" or "lchmod(2)".
|
|
|
|
// Returns modified text, with such refs replaced with HTML links, for example
|
|
|
|
// '<a href="http://man7.org/linux/man-pages/man2/open.2.html">open(2)</a>'.
|
2016-02-04 12:11:17 +00:00
|
|
|
function linkManPages(text) {
|
2017-02-22 14:42:46 +00:00
|
|
|
return text.replace(
|
2017-12-18 02:46:37 +00:00
|
|
|
/(^|\s)([a-z.]+)\((\d)([a-z]?)\)/gm,
|
|
|
|
(match, beginning, name, number, optionalCharacter) => {
|
2018-03-30 10:38:45 +00:00
|
|
|
// Name consists of lowercase letters, number is a single digit.
|
2017-06-19 15:55:10 +00:00
|
|
|
const displayAs = `${name}(${number}${optionalCharacter})`;
|
2017-02-22 14:42:46 +00:00
|
|
|
if (BSD_ONLY_SYSCALLS.has(name)) {
|
2017-12-18 02:46:37 +00:00
|
|
|
return `${beginning}<a href="https://www.freebsd.org/cgi/man.cgi?query=${name}` +
|
2017-02-22 14:42:46 +00:00
|
|
|
`&sektion=${number}">${displayAs}</a>`;
|
|
|
|
} else {
|
2017-12-18 02:46:37 +00:00
|
|
|
return `${beginning}<a href="http://man7.org/linux/man-pages/man${number}` +
|
2017-02-22 14:42:46 +00:00
|
|
|
`/${name}.${number}${optionalCharacter}.html">${displayAs}</a>`;
|
|
|
|
}
|
|
|
|
});
|
2016-02-04 12:11:17 +00:00
|
|
|
}
|
|
|
|
|
2016-02-20 00:07:00 +00:00
|
|
|
function linkJsTypeDocs(text) {
|
2017-06-19 15:55:10 +00:00
|
|
|
const parts = text.split('`');
|
2013-12-25 15:59:25 +00:00
|
|
|
var i;
|
2016-01-19 15:59:55 +00:00
|
|
|
var typeMatches;
|
2013-12-25 15:59:25 +00:00
|
|
|
|
2016-02-04 12:11:17 +00:00
|
|
|
// Handle types, for example the source Markdown might say
|
2018-03-30 10:38:45 +00:00
|
|
|
// "This argument should be a {Number} or {String}".
|
2013-12-25 15:59:25 +00:00
|
|
|
for (i = 0; i < parts.length; i += 2) {
|
2016-11-14 07:23:35 +00:00
|
|
|
typeMatches = parts[i].match(/\{([^}]+)\}/g);
|
2016-01-19 15:59:55 +00:00
|
|
|
if (typeMatches) {
|
|
|
|
typeMatches.forEach(function(typeMatch) {
|
|
|
|
parts[i] = parts[i].replace(typeMatch, typeParser.toLink(typeMatch));
|
|
|
|
});
|
|
|
|
}
|
2013-12-25 15:59:25 +00:00
|
|
|
}
|
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// TODO: maybe put more stuff here?
|
2013-12-25 15:59:25 +00:00
|
|
|
return parts.join('`');
|
2012-02-27 18:59:01 +00:00
|
|
|
}
|
|
|
|
|
2012-12-28 01:32:53 +00:00
|
|
|
function parseAPIHeader(text) {
|
2017-03-02 20:58:26 +00:00
|
|
|
const classNames = 'api_stability api_stability_$2';
|
|
|
|
const docsUrl = 'documentation.html#documentation_stability_index';
|
|
|
|
|
2016-02-11 19:11:09 +00:00
|
|
|
text = text.replace(
|
2016-09-02 20:27:01 +00:00
|
|
|
STABILITY_TEXT_REG_EXP,
|
2017-08-13 16:20:17 +00:00
|
|
|
`<div class="${classNames}"><a href="${docsUrl}">$1 $2</a>$3</div>`
|
2016-02-11 19:11:09 +00:00
|
|
|
);
|
2012-12-28 01:32:53 +00:00
|
|
|
return text;
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
2018-03-30 10:38:45 +00:00
|
|
|
// Section is just the first heading.
|
2012-02-27 18:59:01 +00:00
|
|
|
function getSection(lexed) {
|
|
|
|
for (var i = 0, l = lexed.length; i < l; i++) {
|
|
|
|
var tok = lexed[i];
|
|
|
|
if (tok.type === 'heading') return tok.text;
|
|
|
|
}
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2017-11-05 17:36:50 +00:00
|
|
|
function getMark(anchor) {
|
|
|
|
return `<span><a class="mark" href="#${anchor}" id="${anchor}">#</a></span>`;
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
|
|
|
|
function buildToc(lexed, filename, cb) {
|
|
|
|
var toc = [];
|
|
|
|
var depth = 0;
|
2016-05-23 22:06:09 +00:00
|
|
|
|
|
|
|
const startIncludeRefRE = /^\s*<!-- \[start-include:(.+)\] -->\s*$/;
|
|
|
|
const endIncludeRefRE = /^\s*<!-- \[end-include:(.+)\] -->\s*$/;
|
|
|
|
const realFilenames = [filename];
|
|
|
|
|
2012-02-27 18:59:01 +00:00
|
|
|
lexed.forEach(function(tok) {
|
2016-05-23 22:06:09 +00:00
|
|
|
// Keep track of the current filename along @include directives.
|
|
|
|
if (tok.type === 'html') {
|
|
|
|
let match;
|
|
|
|
if ((match = tok.text.match(startIncludeRefRE)) !== null)
|
|
|
|
realFilenames.unshift(match[1]);
|
|
|
|
else if (tok.text.match(endIncludeRefRE))
|
|
|
|
realFilenames.shift();
|
|
|
|
}
|
|
|
|
|
2012-02-27 18:59:01 +00:00
|
|
|
if (tok.type !== 'heading') return;
|
|
|
|
if (tok.depth - depth > 1) {
|
|
|
|
return cb(new Error('Inappropriate heading level\n' +
|
|
|
|
JSON.stringify(tok)));
|
|
|
|
}
|
|
|
|
|
|
|
|
depth = tok.depth;
|
2016-05-23 22:06:09 +00:00
|
|
|
const realFilename = path.basename(realFilenames[0], '.md');
|
2017-11-05 17:36:50 +00:00
|
|
|
const apiName = tok.text.trim();
|
|
|
|
const id = getId(`${realFilename}_${apiName}`);
|
2012-02-27 18:59:01 +00:00
|
|
|
toc.push(new Array((depth - 1) * 2 + 1).join(' ') +
|
2017-10-06 17:42:22 +00:00
|
|
|
`* <span class="stability_${tok.stability}">` +
|
|
|
|
`<a href="#${id}">${tok.text}</a></span>`);
|
2017-11-05 17:36:50 +00:00
|
|
|
tok.text += getMark(id);
|
|
|
|
if (realFilename === 'errors' && apiName.startsWith('ERR_')) {
|
|
|
|
tok.text += getMark(apiName);
|
|
|
|
}
|
2012-02-27 18:59:01 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
toc = marked.parse(toc.join('\n'));
|
|
|
|
cb(null, toc);
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:55:10 +00:00
|
|
|
const idCounters = {};
|
2012-02-27 18:59:01 +00:00
|
|
|
function getId(text) {
|
|
|
|
text = text.toLowerCase();
|
|
|
|
text = text.replace(/[^a-z0-9]+/g, '_');
|
|
|
|
text = text.replace(/^_+|_+$/, '');
|
|
|
|
text = text.replace(/^([^a-z])/, '_$1');
|
|
|
|
if (idCounters.hasOwnProperty(text)) {
|
2017-11-06 18:46:06 +00:00
|
|
|
text += `_${++idCounters[text]}`;
|
2012-02-27 18:59:01 +00:00
|
|
|
} else {
|
|
|
|
idCounters[text] = 0;
|
|
|
|
}
|
|
|
|
return text;
|
|
|
|
}
|
2017-01-10 19:43:57 +00:00
|
|
|
|
|
|
|
const numberRe = /^(\d*)/;
|
|
|
|
function versionSort(a, b) {
|
|
|
|
a = a.trim();
|
|
|
|
b = b.trim();
|
2018-03-30 10:38:45 +00:00
|
|
|
let i = 0; // Common prefix length.
|
2017-01-10 19:43:57 +00:00
|
|
|
while (i < a.length && i < b.length && a[i] === b[i]) i++;
|
|
|
|
a = a.substr(i);
|
|
|
|
b = b.substr(i);
|
|
|
|
return +b.match(numberRe)[1] - +a.match(numberRe)[1];
|
|
|
|
}
|