2019-03-12 22:07:57 +00:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright 2019 Balena Ltd.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2017-12-21 17:40:13 +00:00
|
|
|
import * as ent from 'ent';
|
2019-03-12 22:07:57 +00:00
|
|
|
import * as _ from 'lodash';
|
|
|
|
|
|
|
|
import { Category, Command, Document } from './doc-types';
|
2017-12-21 17:40:13 +00:00
|
|
|
import * as utils from './utils';
|
|
|
|
|
|
|
|
export function renderCommand(command: Command) {
|
2018-03-20 17:43:26 +00:00
|
|
|
let result = `## ${ent.encode(command.signature)}\n\n${command.help}\n`;
|
2017-12-21 17:40:13 +00:00
|
|
|
|
|
|
|
if (!_.isEmpty(command.options)) {
|
|
|
|
result += '\n### Options';
|
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const 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
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const command of category.commands) {
|
2017-12-21 17:40:13 +00:00
|
|
|
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, '-')
|
2018-03-20 17:43:26 +00:00
|
|
|
.replace(/-+/g, '-')
|
2019-02-22 16:37:57 +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
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const 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
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const 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
|
|
|
|
2019-03-12 22:07:57 +00:00
|
|
|
for (const category of doc.categories) {
|
2017-12-21 17:40:13 +00:00
|
|
|
result += `\n${renderCategory(category)}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
2018-01-04 14:07:55 +00:00
|
|
|
}
|