@awesome-fe/translate
Version:
Translation utils
149 lines • 6.43 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 __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.TableRenderer = void 0;
var block_node_renderer_1 = require("./block-node-renderer");
var TableRenderer = /** @class */ (function (_super) {
__extends(TableRenderer, _super);
function TableRenderer() {
var _this = _super !== null && _super.apply(this, arguments) || this;
_this.ignoredAttributeNames = ['colcount', 'rowcount', 'tablepcwidth'];
return _this;
}
TableRenderer.prototype.getDefaultAttributes = function (node) {
return { tablepcwidth: 100, style: 'table', options: 'header' };
};
TableRenderer.prototype.getBlockTitle = function (node) {
return node.getTitle();
};
TableRenderer.prototype.renderBody = function (node) {
var _a;
var separator = (_a = node.getAttribute('separator')) !== null && _a !== void 0 ? _a : '|';
var content = renderRows(node);
var delimiter = "|===";
if (content) {
return [delimiter, content, delimiter].join('\n') + '\n';
}
else {
return delimiter;
}
function columnOf(cell) {
return cell.getColumn();
}
function styleCharOf(cell) {
var _a;
var styleCharMap = {
asciidoc: 'a',
emphasis: 'e',
header: 'h',
literal: 'l',
monospaced: 'm',
default: 'd',
strong: 's',
};
if (cell.getStyle() === columnOf(cell).getStyle()) {
return '';
}
return (_a = styleCharMap[cell.getStyle()]) !== null && _a !== void 0 ? _a : '';
}
function escapeSeparator(text) {
var _a;
var separator = (_a = node.getAttribute('separator')) !== null && _a !== void 0 ? _a : '|';
return text.replace(new RegExp("[".concat(separator, "]"), 'g'), '\\' + separator);
}
function renderHeaderRows(rows) {
var text = rows.map(function (it) { return it.map(function (it) { return escapeSeparator(it.getText()); }).join(" ".concat(separator)); }).filter(function (it) { return !!it; }).join('\n');
return text && separator + text;
}
function renderBodyRows(rows) {
var horizontalAlignmentChars = {
left: '',
right: '>',
center: '^',
};
var verticalAlignmentChars = {
top: '',
bottom: '.>',
middle: '.^',
};
function hAlignOf(cell) {
var _a;
var cellAttributes = cell.getAttributes();
var columnAttributes = columnOf(cell).getAttributes();
if (cellAttributes.halign === columnAttributes.halign) {
return '';
}
return (_a = horizontalAlignmentChars[cellAttributes.halign]) !== null && _a !== void 0 ? _a : '';
}
function vAlignOf(cell) {
var _a;
var cellAttributes = cell.getAttributes();
var columnAttributes = columnOf(cell).getAttributes();
if (cellAttributes.valign === columnAttributes.valign) {
return '';
}
return (_a = verticalAlignmentChars[cellAttributes.valign]) !== null && _a !== void 0 ? _a : '';
}
function addEmptyLine(text) {
return text.replace(/\n\n(.*[^\n])$/gs, '\n\n$1\n');
}
function spanOf(node) {
var colSpan = node.getColumnSpan() > 1 ? "".concat(node.getColumnSpan()) : '';
var rowSpan = node.getRowSpan() > 1 ? ".".concat(node.getRowSpan()) : '';
if (colSpan || rowSpan) {
return "".concat(colSpan).concat(rowSpan, "+");
}
else {
return '';
}
}
function renderCell(node) {
return [
spanOf(node),
hAlignOf(node),
vAlignOf(node),
styleCharOf(node),
separator,
addEmptyLine(escapeSeparator(node.getText())),
].join('');
}
function renderRow(row, lastRow) {
var rowText = row.map(function (it) { return renderCell(it); }).join('\n');
return lastRow ? rowText : rowText.trim();
}
return rows.map(function (row, rowIndex) { return renderRow(row, rowIndex === rows.length - 1); }).filter(function (it) { return !!it; }).join('\n\n');
}
function renderRows(node) {
return [
renderHeaderRows(node.getRows().head),
renderBodyRows(__spreadArray(__spreadArray([], node.getRows().body, true), node.getRows().foot, true)),
].filter(function (it) { return !!it; }).join('\n\n');
}
};
return TableRenderer;
}(block_node_renderer_1.BlockNodeRenderer));
exports.TableRenderer = TableRenderer;
//# sourceMappingURL=table-renderer.js.map