@awesome-fe/translate
Version:
Translation utils
83 lines • 2.81 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentRenderer = void 0;
const base_node_renderer_1 = require("./base-node-renderer");
const core_1 = require("@asciidoctor/core");
function buildAuthorLine(author) {
if (author.getEmail()) {
return `${author.getName()} <${author.getEmail()}>`;
}
else {
return author.getName();
}
}
function getLinesBeforeTitle(node, id) {
if (!id) {
return '';
}
const idText = `[[${id}]]`;
const lines = node.getSourceLines();
const index = lines.indexOf(idText);
if (index === -1) {
return idText;
}
else {
return lines.slice(0, index + 1).join('\n');
}
}
class DocumentRenderer extends base_node_renderer_1.BaseNodeRenderer {
helperAdoc = (0, core_1.default)();
getDefaultAttributes(node) {
return this.helperAdoc.load('', { backend: 'adoc' }).getAttributes();
}
ignoredAttributeNames = [
'compat-mode',
'sectnums',
'doctitle',
/^firstname\w*$/,
/^authorinitials\w*$/,
/^middlename\w*$/,
/^lastname\w*$/,
/^author\w*$/,
/^email\w*$/,
'authors',
'authorcount',
'localtime',
'localdatetime',
'doctime',
'docdatetime',
'outfilesuffix',
'filetype',
'revdate',
'revnumber',
'filetype-adoc',
/doctype(-[-\w]+)?/,
/backend(-[-\w]+)?/,
/basebackend(-[-\w]+)?/,
];
render(node) {
const attributes = node.getAttributes();
const doctitle = attributes.doctitle;
const nonDefaultAttributes = this.getNonDefaultAttributes(node);
const children = node.getContent();
const id = node.getId();
// 作者这一行也可能是一句 include:: 指令,但是现在用的转换机制无法正确识别 include:: 指令,因此只能把它替换回去
const authors = node.getAuthors().map(author => buildAuthorLine(author)).join('; ');
const revisionNumber = node.getRevisionNumber();
const revisionDate = node.getRevisionDate();
const rev = [revisionNumber && `v${revisionNumber}`, revisionDate].filter(it => !!it).join(', ');
const body = [
getLinesBeforeTitle(node, id),
doctitle && `= ${doctitle}`,
authors,
rev,
nonDefaultAttributes.map(it => renderAttribute(it.name, it.value)).join('\n'),
].filter(it => !!it).join('\n');
return [body, children].filter(it => !!it).join('\n\n');
}
}
exports.DocumentRenderer = DocumentRenderer;
function renderAttribute(key, value) {
return [`:${key}:`, value].filter(it => !!it).join(' ');
}
//# sourceMappingURL=document-renderer.js.map