@awesome-fe/translate
Version:
Translation utils
128 lines • 6.44 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 __());
};
})();
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.renderContent = exports.BlockNodeRenderer = void 0;
var base_node_renderer_1 = require("./base-node-renderer");
var add_quotes_1 = require("./utils/add-quotes");
var split_attribute_value_1 = require("./utils/split-attribute-value");
var BlockNodeRenderer = /** @class */ (function (_super) {
__extends(BlockNodeRenderer, _super);
function BlockNodeRenderer() {
return _super !== null && _super.apply(this, arguments) || this;
}
BlockNodeRenderer.prototype.render = function (node) {
var header = this.renderHeader(node);
var body = this.renderBody(node);
return [header, body].filter(function (it) { return !!it; }).join('\n');
};
BlockNodeRenderer.prototype.renderHeader = function (node) {
var _a;
var attributes = this.renderAttributes(this.getBlockAttributes(node));
return [
attributes ? "[".concat(attributes, "]") : '',
this.buildBlockTitle((_a = this.getBlockTitle(node)) === null || _a === void 0 ? void 0 : _a.toString()),
].filter(function (it) { return !!it; }).join('\n');
};
BlockNodeRenderer.prototype.getBlockTitle = function (node) {
return node.getAttribute('title');
};
BlockNodeRenderer.prototype.renderBody = function (node) {
return "".concat(this.renderChildren(node), "\n");
};
BlockNodeRenderer.prototype.buildBlockTitle = function (title) {
var blockTitle = title === null || title === void 0 ? void 0 : title.trim();
return blockTitle && ".".concat(blockTitle);
};
BlockNodeRenderer.prototype.renderChildren = function (node) {
var _a;
var content = (_a = node.getContent) === null || _a === void 0 ? void 0 : _a.call(node);
return renderContent(content);
};
BlockNodeRenderer.prototype.renderAttributes = function (attributes) {
var _this = this;
var shortenAttributes = this.shortenAttributes(attributes);
var content = shortenAttributes
.map(function (it) { return _this.renderAttribute(it, shortenAttributes); })
.filter(function (it) { return !!it; }).join(',');
return content !== null && content !== void 0 ? content : '';
};
BlockNodeRenderer.prototype.renderAttribute = function (attr, attributes) {
if (attributes === void 0) { attributes = []; }
var value = (0, add_quotes_1.addQuotes)(attr.value);
if (attr.name === 'id') {
return attributes.length > 1 ? "#".concat(value) : "[".concat(value, "]");
}
else if (optionNamePattern.test(attr.name)) {
var optionName = attr.name.replace(optionNamePattern, '$1');
return "%".concat((0, add_quotes_1.addQuotes)(optionName));
}
else if (attr.name === 'role') {
return (0, split_attribute_value_1.splitAttributeValue)(attr.value).map(function (it) { return ".".concat((0, add_quotes_1.addQuotes)(it)); }).join('');
}
else if (attr.position) {
return value.toString();
}
else {
return "".concat(attr.name, "=").concat(value);
}
};
// 简写 id 和 options 属性,把它们添加特定的前缀,然后追加到第一个位置参数后面
BlockNodeRenderer.prototype.shortenAttributes = function (attributes) {
var id = attributes.find(function (it) { return it.name === 'id'; });
var options = attributes.find(function (it) { return optionNamePattern.test(it.name); });
var role = attributes.find(function (it) { return it.name === 'role'; });
var firstPositionalAttribute = attributes.find(function (it) { return it.position === 1; });
var idText = id && this.renderAttribute(id, attributes);
var optionsText = options && this.renderAttribute(options, attributes);
var roleText = role && this.renderAttribute(role, attributes);
return attributes
.filter(function (it) { return !!it; })
.filter(function (it) { return !(firstPositionalAttribute === null || firstPositionalAttribute === void 0 ? void 0 : firstPositionalAttribute.value) || ![id, options, role].includes(it); })
.map(function (it) { return it.name === (firstPositionalAttribute === null || firstPositionalAttribute === void 0 ? void 0 : firstPositionalAttribute.name) ? __assign(__assign({}, it), { value: [it.value, idText, optionsText, roleText].filter(function (it) { return !!it; }).join('') }) : it; });
};
return BlockNodeRenderer;
}(base_node_renderer_1.BaseNodeRenderer));
exports.BlockNodeRenderer = BlockNodeRenderer;
function renderContent(content) {
if (!content || content.constructor.name === '$NilClass') {
return '';
}
if (typeof content === 'string') {
return content;
}
if ('convert' in content) {
return content.convert();
}
else if (content instanceof Array) {
return content.map(function (it) { return renderContent(it); })
.filter(function (it) { return !!it; }).join('\n');
}
}
exports.renderContent = renderContent;
var optionNamePattern = /^(.*?)-option$/;
//# sourceMappingURL=block-node-renderer.js.map