2017-12-21 17:40:13 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
import * as ent from 'ent';
|
|
|
|
import * as utils from './utils';
|
|
|
|
import { Document, Category, Command } from './doc-types';
|
|
|
|
|
|
|
|
export function renderCommand(command: Command) {
|
2018-03-15 10:26:50 +00:00
|
|
|
let result = `## <a name="${getAnchor(command)}"></a>${ent.encode(
|
|
|
|
command.signature,
|
|
|
|
)}\n\n${command.help}\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
if (!_.isEmpty(command.options)) {
|
|
|
|
result += '\n### Options';
|
|
|
|
|
|
|
|
for (let option of command.options!) {
|
2018-01-09 15:05:24 +00:00
|
|
|
result += `\n\n#### ${utils.parseSignature(option)}\n\n${
|
|
|
|
option.description
|
|
|
|
}`;
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
result += '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
export function renderCategory(category: Category) {
|
2018-01-04 15:19:44 +00:00
|
|
|
let result = `# ${category.title}\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
for (let command of category.commands) {
|
|
|
|
result += `\n${renderCommand(command)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
function getAnchor(command: Command) {
|
2018-01-09 15:05:24 +00:00
|
|
|
return (
|
|
|
|
'#' +
|
|
|
|
command.signature
|
|
|
|
.replace(/\s/g, '-')
|
2018-03-15 10:26:50 +00:00
|
|
|
.replace(/</g, '-')
|
|
|
|
.replace(/>/g, '-')
|
|
|
|
.replace(/\[/g, '-')
|
2018-01-09 15:05:24 +00:00
|
|
|
.replace(/\]/g, '-')
|
|
|
|
.replace(/--/g, '-')
|
2018-03-15 10:26:50 +00:00
|
|
|
.replace(/-$/, '')
|
2018-01-09 15:05:24 +00:00
|
|
|
.replace(/\.\.\./g, '')
|
|
|
|
.replace(/\|/g, '')
|
|
|
|
.toLowerCase()
|
|
|
|
);
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function renderToc(categories: Category[]) {
|
2018-01-04 15:19:44 +00:00
|
|
|
let result = `# Table of contents\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
for (let category of categories) {
|
2018-01-04 15:19:44 +00:00
|
|
|
result += `\n- ${category.title}\n\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
for (let command of category.commands) {
|
2018-01-09 15:05:24 +00:00
|
|
|
result += `\t- [${ent.encode(command.signature)}](${getAnchor(
|
|
|
|
command,
|
|
|
|
)})\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
export function render(doc: Document) {
|
2018-01-09 15:05:24 +00:00
|
|
|
let result = `# ${doc.title}\n\n${doc.introduction}\n\n${renderToc(
|
|
|
|
doc.categories,
|
|
|
|
)}`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
for (let category of doc.categories) {
|
|
|
|
result += `\n${renderCategory(category)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|