@awesome-fe/translate
Version:
Translation utils
155 lines • 6.82 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseTinyNodeRenderer = void 0;
var adoc_1 = require("../../utils/adoc");
var match_some_1 = require("../../adoc-builder/renderers/utils/match-some");
var lodash_1 = require("lodash");
function valueToText(value) {
if (typeof value === 'number' || typeof value === 'string') {
return "".concat(value);
}
else if (!value || !value.toString()) {
return '';
}
else if ('length' in value) {
return JSON.stringify(Array.from(value).map(function (it) {
if (it instanceof Object) {
return (0, lodash_1.omit)(it, '$$id');
}
else {
return it;
}
}));
}
else if (typeof value === 'object') {
return JSON.stringify((0, lodash_1.omit)(value, '$$id'));
}
}
var BaseTinyNodeRenderer = /** @class */ (function () {
function BaseTinyNodeRenderer() {
this.ignoredAttributeNames = [];
this.tagName = 'UNKNOWN';
this.selfClosingTag = false;
}
BaseTinyNodeRenderer.prototype.getTagName = function (node) {
return this.tagName;
};
BaseTinyNodeRenderer.prototype.render = function (node) {
var _a;
if (this.selfClosingTag) {
return "<".concat(this.getTagName(node)).concat(this.renderAttributes(node), "/>");
}
else {
return "<".concat(this.getTagName(node)).concat(this.renderAttributes(node), ">").concat((_a = this.getContent(node)) !== null && _a !== void 0 ? _a : '', "</").concat(this.getTagName(node), ">");
}
};
BaseTinyNodeRenderer.prototype.renderAttributes = function (node) {
function shouldIgnore(name, node) {
return name === 'style' || adoc_1.adoc.isDocument(node) && !(0, match_some_1.matchSome)(name, __spreadArray([
'doctitle',
'authorcount',
'firstname',
'authorinitials',
'middlename',
'lastname',
'author',
'email',
'authors',
/firstname_(\d+)/,
/authorinitials_(\d+)/,
/lastname_(\d+)/,
/author_(\d+)/,
/author_(\d+)/,
/authorinitials_(\d+)/,
/firstname_(\d+)/,
/middlename_(\d+)/,
/lastname_(\d+)/,
/email_(\d+)/,
'revnumber',
'revdate'
], node.attributes_modified.hash.$$keys, true));
}
var result = __spreadArray(__spreadArray([
{
name: 'name',
prefix: 'adoc',
value: node.getNodeName(),
}
], this.getAttributesViaSetter(node), true), this.getAttributesViaProperty(node).filter(function (it) { return !shouldIgnore(it.name, node); }), true).filter(function (it) { return !!it; })
.map(function (_a) {
var name = _a.name, value = _a.value, prefix = _a.prefix;
var valueText = valueToText(value).replace(/"/g, '"');
if (typeof value === 'string') {
return "".concat(prefix, "-").concat(name, "=\"").concat(valueText, "\"");
}
else if (value === undefined || value === null || typeof value === 'object' && value.toString() === '' || typeof value === 'number' && value === 0) {
return '';
}
else {
return "".concat(prefix, "-").concat(name, "=\"").concat(valueText, "\" type-").concat(prefix, "-").concat(name, "=\"").concat(typeof value, "\"");
}
}).filter(function (it) { return !!it; }).join(' ');
return result && " ".concat(result);
};
BaseTinyNodeRenderer.prototype.getAttributesViaProperty = function (node) {
var _this = this;
var normalAttributes = Object.entries(node.getAttributes())
.filter(function (_a) {
var name = _a[0];
return !(0, match_some_1.matchSome)(name, __spreadArray(['$positional'], _this.ignoredAttributeNames, true));
})
.map(function (_a) {
var name = _a[0], value = _a[1];
return ({ name: name, value: value, prefix: 'data' });
})
.filter(function (it) { return it.value !== undefined; });
if (adoc_1.adoc.isInline(node)) {
return __spreadArray(__spreadArray([], normalAttributes, true), [
{ name: 'type', value: node.getType(), prefix: 'prop' },
{ name: 'alt', value: node.getAlt(), prefix: 'prop' },
{ name: 'target', value: node.getTarget(), prefix: 'prop' },
], false).filter(function (it) { return it.value !== undefined; });
}
else {
return normalAttributes;
}
};
BaseTinyNodeRenderer.prototype.getAttributesViaSetter = function (node) {
function getSetterNames(obj) {
if (!obj) {
return [];
}
return (0, lodash_1.uniq)(__spreadArray(__spreadArray([], Object.getOwnPropertyNames(obj), true), getSetterNames(Object.getPrototypeOf(obj)), true));
}
var setters = getSetterNames(node)
.filter(function (it) { return /^set([A-Z])\w+/.test(it); })
.filter(function (it) { return !adoc_1.adoc.isDocument(node) || it !== 'setTitle'; })
.filter(function (it) { return !['setAttribute', 'setHeaderAttribute', 'setSourcemap'].includes(it); });
return setters.map(function (it) {
var _a;
var name = it.replace(/^set([A-Z]\w*)/, '$1');
var value = (_a = node["get".concat(name)]) === null || _a === void 0 ? void 0 : _a.call(node);
return { name: name, value: value, prefix: 'attr' };
}).filter(function (it) { return !!(it === null || it === void 0 ? void 0 : it.value); });
};
BaseTinyNodeRenderer.prototype.getContent = function (node) {
if (adoc_1.adoc.isAbstractBlock(node)) {
return node.getContent();
}
else if (adoc_1.adoc.isInline(node)) {
return node.getText();
}
};
return BaseTinyNodeRenderer;
}());
exports.BaseTinyNodeRenderer = BaseTinyNodeRenderer;
//# sourceMappingURL=base-tiny-node-renderer.js.map