UNPKG

asciidoctor-gettext

Version:

gettext/po string extraction tool for asciidoc documents

398 lines 16.5 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var asciidoctor_js_1 = __importDefault(require("asciidoctor.js")); var opal_utils_1 = require("./opal-utils"); var common_1 = require("./common"); var conditionals_1 = require("./conditionals"); var attributes_1 = require("./attributes"); function rewriteCommon(text, isPath, transformer, asciidocOptions) { if (asciidocOptions === void 0) { asciidocOptions = {}; } var outputText = ''; function write(text) { outputText += text; } var rewriteAsciidoctor = asciidoctor_js_1.default(); rewriteAsciidoctor.Extensions.register('rewrite', function () { this.preprocessor(conditionals_1.rewritePreprocessor); this.includeProcessor(conditionals_1.rewriteIncludeProcessor); }); var document = isPath ? rewriteAsciidoctor.loadFile(text, asciidocOptions) : rewriteAsciidoctor.load(text, asciidocOptions); var state = { listStack: [], }; rewriteBlock(document, transformer, write, state); rewriteAsciidoctor.Extensions.unregister(['rewrite']); return outputText; } function rewrite(text, transformer, asciidocOptions) { if (asciidocOptions === void 0) { asciidocOptions = {}; } return rewriteCommon(text, false, transformer, asciidocOptions); } exports.rewrite = rewrite; function rewriteFile(inputPath, transformer, asciidocOptions) { if (asciidocOptions === void 0) { asciidocOptions = {}; } return rewriteCommon(inputPath, true, transformer, asciidocOptions); } exports.rewriteFile = rewriteFile; var attributeQuoteNeededRegex = / |,|"|'/; var doubleQuotesRegex = /"/g; function quoteAttributeValueIfNeeded(value) { // See https://asciidoctor.org/docs/user-manual/#named-attribute // If the value contains a space, comma, or quote character, it must be enclosed in double or single quotes // (e.g., name="value with space"). In all other cases, the surrounding quotes are optional. If present, // the enclosing quotes are dropped from the parsed value. if (!attributeQuoteNeededRegex.test(value)) { return value; } var escapedValue = value.replace(doubleQuotesRegex, '\\"'); return "\"" + escapedValue + "\""; } exports.quoteAttributeValueIfNeeded = quoteAttributeValueIfNeeded; function getAttributesString(block, transformer, localizableKeys, excludingKeys, extraAttributes) { if (localizableKeys === void 0) { localizableKeys = []; } if (excludingKeys === void 0) { excludingKeys = []; } if (extraAttributes === void 0) { extraAttributes = {}; } var blockAttributes = block.getAttributes(); var attributesString = ''; for (var _i = 0, _a = [blockAttributes, extraAttributes]; _i < _a.length; _i++) { var attributes = _a[_i]; for (var attributeKey in attributes) { if (excludingKeys.includes(attributeKey)) { continue; } var origValue = attributes[attributeKey]; if (attributeKey === 'attribute_entries') { continue; } var isLocalizable = localizableKeys.includes(attributeKey); var value = quoteAttributeValueIfNeeded(isLocalizable ? transformer(origValue) : origValue); var separator = (attributesString !== '') ? ',' : ''; attributesString += "" + separator + attributeKey + "=" + value; } } return attributesString; } var pipeRegEx = /\|/g; function escapePipes(input) { return input.replace(pipeRegEx, '\\|'); } exports.escapePipes = escapePipes; function processCustomAttributes(block, transformer, write, state) { var attributes = block.getAttributes(); var attributeEntries = attributes.attribute_entries; if (attributeEntries === undefined) { return; } for (var _i = 0, _a = attributeEntries; _i < _a.length; _i++) { var attributeEntry = _a[_i]; if (block.node_name === 'image' && attributeEntry.name === 'figure-number') { continue; } write("\n:" + attributeEntry.name + ": " + transformer(attributeEntry.value) + "\n"); } } function rewriteBlock(block, transformer, write, state) { var listRewrite = { open: function () { var listBlock = block; updateListStack('push', listBlock, state, write); var attributesString = getAttributesString(listBlock, transformer, [], ['id', 'style']); if (attributesString !== '') { var style = listBlock.getStyle(); var styleStr = opal_utils_1.isNil(style) ? '' : style + ", "; write("[" + styleStr + attributesString + "]\n"); } }, close: function () { updateListStack('pop', block, state, write); }, }; var rewriteMap = { admonition: { open: function () { var admonitionBlock = block; write("[" + admonitionBlock.getStyle() + "]\n====\n"); }, close: function () { write('====\n'); }, }, dlist: listRewrite, document: { open: function () { var document = block; if (!opal_utils_1.isNil(document.header)) { var doctitle = transformer(document.header.title); write("= " + doctitle + "\n"); } var attributes = document.getAttributes(); if (attributes.authors && attributes.authors !== '') { write(attributes.authors + "\n\n"); } for (var _i = 0, _a = document.attributes_modified.hash.$$keys; _i < _a.length; _i++) { var key = _a[_i]; var isNonLocalizable = attributes_1.nonLocalizableBuiltinAttributeKeys.includes(key); var originalValue = attributes[key]; if (originalValue === undefined) { continue; } var value = isNonLocalizable ? originalValue : transformer(originalValue); write(":" + key + ": " + value + "\n"); } }, }, floating_title: { open: function () { var section = block; write("[float]\n" + transformer(section.title) + "\n----------\n"); }, }, image: { open: function () { var imageBlock = block; var attributes = imageBlock.getAttributes(); var localizableKeys = ['alt', 'default-alt', 'caption']; var attributesString = getAttributesString(imageBlock, transformer, localizableKeys); write("image::" + transformer(attributes.target) + "[" + attributesString + "]\n"); }, }, list_item: { open: function () { var li = block; var text = li.text; if (opal_utils_1.isNil(text) || text === '') { return; } var transformedText = transformer(text); var topOfStack = state.listStack[state.listStack.length - 1]; var indent = ' '.repeat(topOfStack.level * 2); switch (li.parent.node_name) { case 'dlist': var colons = ':'.repeat(1 + topOfStack.level); write("" + indent + transformedText + colons + "\n"); break; case 'olist': var periods = '.'.repeat(topOfStack.level); write("" + indent + periods + " " + transformedText + "\n"); break; case 'ulist': var asterisks = '*'.repeat(topOfStack.level); write("" + indent + asterisks + " " + transformedText + "\n"); break; } }, }, listing: { open: function () { var listing = block; var attributesString = getAttributesString(listing, transformer, [], ['style']); var text = transformer(listing.getSource()); write("[" + listing.getStyle() + "," + attributesString + "]\n" + text + "\n"); }, }, literal: { open: function () { var literal = block; var text = transformer(literal.getSource()); write("....\n" + text + "\n...."); }, }, olist: listRewrite, page_break: { open: function () { write('<<<\n'); }, }, pass: { open: function () { if (conditionals_1.ifBlockRewriterOpen(block, transformer, write)) { return; } var literal = block; var text = transformer(literal.getSource()); write("++++\n" + text + "\n++++"); }, }, quote: { open: function () { var localizableKeys = ['title', 'citetitle']; var excludingKeys = ['style', 'title']; var attributesString = getAttributesString(block, transformer, localizableKeys, excludingKeys); write("[quote, " + attributesString + "]\n____\n"); }, close: function () { write('____\n'); }, }, section: { open: function () { var section = block; var attributes = section.getAttributes(); var attributesString = getAttributesString(section, transformer); write("[" + attributesString + "]\n"); var prefix = '='.repeat(section.level + 1); write(prefix + " " + transformer(section.title) + "\n"); }, }, table: { open: function () { var table = block; var localizableKeys = ['']; var excludingKeys = ['rowcount', 'colcount', 'tablepcwidth']; var extraAttributes = {}; if (!opal_utils_1.isNil(table.caption)) { extraAttributes.caption = table.caption; } var attrs = table.getAttributes(); if (attrs.cols === undefined) { // Synthesize 'cols' attribute if it's missing. For some reason, if the table starts with // colspans, the colcount is sometimes otherwise incorrectly calculated. Not sure why. Asciidoctor bug? extraAttributes.cols = attrs.colcount + "*"; } var attributesString = getAttributesString(table, transformer, localizableKeys, excludingKeys, extraAttributes); if (attributesString !== '') { write("[" + attributesString + "]\n"); } write('|===\n'); var rowsKeys = ['head', 'body', 'foot']; for (var _i = 0, rowsKeys_1 = rowsKeys; _i < rowsKeys_1.length; _i++) { var rowKey = rowsKeys_1[_i]; for (var _a = 0, _b = table.rows[rowKey]; _a < _b.length; _a++) { var row = _b[_a]; var _loop_1 = function (cell) { var rowspan = opal_utils_1.isNil(cell.rowspan) ? 1 : cell.rowspan; var colspan = opal_utils_1.isNil(cell.colspan) ? 1 : cell.colspan; var first = true; var writeCellContent = function (text) { if (first) { write(colspan + "." + rowspan + "+|"); first = false; } write(escapePipes(transformer(text.trim()))); }; conditionals_1.ifBlockRewriteCellText(cell.text, writeCellContent, write, transformer); write('\n'); }; for (var _c = 0, row_1 = row; _c < row_1.length; _c++) { var cell = row_1[_c]; _loop_1(cell); } } } write("|===\n"); }, }, thematic_break: { open: function () { write('---\n'); }, }, toc: { open: function () { write('toc::[]\n'); }, }, paragraph: { open: function () { var paragraphBlock = block; var attributesString = getAttributesString(paragraphBlock, transformer); write("[" + attributesString + "]\n"); write(transformer(paragraphBlock.getSource()) + "\n\n"); }, }, preamble: {}, sidebar: { open: function () { write('****\n'); }, close: function () { write('****\n'); }, }, ulist: listRewrite, verse: { open: function () { var verse = block; var localizableKeys = ['title', 'citetitle']; var excludingKeys = ['style', 'title']; var attributesString = getAttributesString(block, transformer, localizableKeys, excludingKeys); var text = transformer(verse.getSource()); write("[verse, " + attributesString + "]\n____\n" + text + "\n____\n"); }, }, }; var rewrite = rewriteMap[block.node_name]; if (!rewrite) { console.error("Unknown node name: '" + block.node_name + "'"); return; } processCustomAttributes(block, transformer, write, state); if (!opal_utils_1.isNil(block.id)) { write("[[" + block.id + "]]\n"); } if (block.node_name !== 'section' && !opal_utils_1.isNil(block.title)) { write("." + transformer(block.title) + "\n"); } if (rewrite.open) { rewrite.open(); } rewriteBlocks(block.getBlocks(), transformer, write, state); if (rewrite.close) { rewrite.close(); } } function nodeNameIsList(nodeName) { return ['dlist', 'olist', 'ulist'].includes(nodeName); } function updateListStack(pushOrPop, block, state, write) { var name = block.node_name; var blockIsList = nodeNameIsList(name); if (!blockIsList) { throw new Error('Block is expected to be dlist, olist or ulist'); } if (state.listStack.length > 0) { var topOfStack = state.listStack[state.listStack.length - 1]; if (pushOrPop === 'push') { if (topOfStack.type === name) { topOfStack.level++; } else { state.listStack.push({ type: name, level: 1 }); } } else { topOfStack.level--; if (topOfStack.level === 0) { state.listStack.pop(); if (state.listStack.length === 0) { // See https://asciidoctor.org/docs/user-manual/#separating-lists // If you have adjacent lists, they have the tendency to want to fuse together. To force lists apart, // insert a line comment (//) surrounded by blank lines between the two lists. write('\n\n//-\n\n'); } } } } else { if (pushOrPop === 'pop') { throw new Error('listStack contains no items!'); } state.listStack.push({ type: name, level: 1 }); } } function rewriteBlocks(blocks, transformer, write, state) { blocks.forEach(function (block) { // Oddly, the blocks of a dlist are arrays of arrays of blocks... if (common_1.isArrayOfBlocks(block)) { rewriteBlocks(block, transformer, write, state); } else { rewriteBlock(block, transformer, write, state); } }); } //# sourceMappingURL=rewrite.js.map