UNPKG

@awesome-fe/translate

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