@awesome-fe/translate
Version:
Translation utils
104 lines • 4.15 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.DocumentRenderer = void 0;
var base_node_renderer_1 = require("./base-node-renderer");
var core_1 = require("@asciidoctor/core");
function buildAuthorLine(author) {
if (author.getEmail()) {
return "".concat(author.getName(), " <").concat(author.getEmail(), ">");
}
else {
return author.getName();
}
}
function getLinesBeforeTitle(node, id) {
if (!id) {
return '';
}
var idText = "[[".concat(id, "]]");
var lines = node.getSourceLines();
var index = lines.indexOf(idText);
if (index === -1) {
return idText;
}
else {
return lines.slice(0, index + 1).join('\n');
}
}
var DocumentRenderer = /** @class */ (function (_super) {
__extends(DocumentRenderer, _super);
function DocumentRenderer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.helperAdoc = (0, core_1.default)();
_this.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]+)?/,
];
return _this;
}
DocumentRenderer.prototype.getDefaultAttributes = function (node) {
return this.helperAdoc.load('', { backend: 'adoc' }).getAttributes();
};
DocumentRenderer.prototype.render = function (node) {
var attributes = node.getAttributes();
var doctitle = attributes.doctitle;
var nonDefaultAttributes = this.getNonDefaultAttributes(node);
var children = node.getContent();
var id = node.getId();
// 作者这一行也可能是一句 include:: 指令,但是现在用的转换机制无法正确识别 include:: 指令,因此只能把它替换回去
var authors = node.getAuthors().map(function (author) { return buildAuthorLine(author); }).join('; ');
var revisionNumber = node.getRevisionNumber();
var revisionDate = node.getRevisionDate();
var rev = [revisionNumber && "v".concat(revisionNumber), revisionDate].filter(function (it) { return !!it; }).join(', ');
var body = [
getLinesBeforeTitle(node, id),
doctitle && "= ".concat(doctitle),
authors,
rev,
nonDefaultAttributes.map(function (it) { return renderAttribute(it.name, it.value); }).join('\n'),
].filter(function (it) { return !!it; }).join('\n');
return [body, children].filter(function (it) { return !!it; }).join('\n\n');
};
return DocumentRenderer;
}(base_node_renderer_1.BaseNodeRenderer));
exports.DocumentRenderer = DocumentRenderer;
function renderAttribute(key, value) {
return [":".concat(key, ":"), value].filter(function (it) { return !!it; }).join(' ');
}
//# sourceMappingURL=document-renderer.js.map