UNPKG

asciidoctor-gettext

Version:

gettext/po string extraction tool for asciidoc documents

123 lines 5.55 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); // NOTE: The extractPreprocessor attempts to strip out any ifeval, ifdef, ifndef and // endif directives using a bunch of regular expressions. This is not a proper // parser so there may be edge cases where this will fail. // NOTE: The rewritePreprocessor will rewrite all *if*::[] macros to be pass-through blocks // with a "magic" role assigned to it. the parameters of the original if::[] encoded in // a JSON string, which is put into the pass-through text. This way, the if::[]s // get become nodes in the Document. Finally, ifBlockRewriterOpen() will rewrite the // pass-through blocks back into (localized) if::[]s... It's a hacky abuse of the API, but // unfortunately asciidoctor.js doesn't provide a real AST that reflects the input // completely (if::[]s are gone in the final tree). // NOTE: This hack is also used for include::[] macros. The reason is to enable "assigning" // id's and title's and attributes to a block that gets included through a macro. By // using a pass-through block to represent the include:[], asciidoctor's parser will assign the // id, title and attributes to the generated block. var ifBlockRole = '__asciiDoctorGettextIfBlockHack__'; function getBlockLines(type, conditionOrContent, def) { if (conditionOrContent === void 0) { conditionOrContent = ''; } if (def === void 0) { def = ''; } var data = { type: type, conditionOrContent: conditionOrContent, def: def, }; return ["[pass, role=\"" + ifBlockRole + "\"]", JSON.stringify(data), '']; } var replacements = [ { regex: /^ifeval::\[([^\]]+)\]$/, extractSubstitute: function (match) { return null; }, rewriteSubstitute: function (match) { return getBlockLines('ifeval', match[1]); }, }, { regex: /^(ifn?def)::([^\[]+)\[(.*)\]$/, extractSubstitute: function (match) { return match[3] !== '' ? [match[3]] : null; }, rewriteSubstitute: function (match) { return getBlockLines(match[1], match[3], match[2]); }, }, { regex: /^endif::/, extractSubstitute: function (match) { return null; }, rewriteSubstitute: function (match) { return getBlockLines('endif'); }, }, { regex: /^include::([^\[]+)\[\]$/, extractSubstitute: function (match) { return null; }, rewriteSubstitute: function (match) { return getBlockLines('include', '', match[1]); }, }, ]; function makePreprocessor(type) { return function extractPreprocessor() { this.process(function (document, reader) { var lines = reader.lines; var preprocessedLines = []; for (var idx = 0; idx < lines.length; ++idx) { var line = lines[idx]; var preprocessedLine = [line]; for (var _i = 0, replacements_1 = replacements; _i < replacements_1.length; _i++) { var replacement = replacements_1[_i]; var match = line.match(replacement.regex); if (match) { var substitute = type === 'extract' ? replacement.extractSubstitute : replacement.rewriteSubstitute; preprocessedLine = substitute(match); break; } } if (null !== preprocessedLine) { preprocessedLines.push.apply(preprocessedLines, preprocessedLine); } } reader.lines = preprocessedLines; return reader; }); }; } exports.extractPreprocessor = makePreprocessor('extract'); exports.rewritePreprocessor = makePreprocessor('rewrite'); function macroSourceFromIfData(jsonString, transformer) { var data = JSON.parse(jsonString); var shouldLocalize = ['ifdef', 'ifndef'].includes(data.type); var conditionOrContent = shouldLocalize ? transformer(data.conditionOrContent) : data.conditionOrContent; return "\n" + data.type + "::" + data.def + "[" + conditionOrContent + "]\n\n"; } function ifBlockRewriterOpen(ifBlock, transformer, write) { if (ifBlock.getRole() !== ifBlockRole) { return false; } var macroSource = macroSourceFromIfData(ifBlock.getSource(), transformer); write(macroSource); return true; } exports.ifBlockRewriterOpen = ifBlockRewriterOpen; function rewriteIncludeProcessor() { this.process(function (document, reader, target, attributes) { var lines = getBlockLines('include', '', target); reader.pushInclude(lines, target, target, 1, attributes); }); this.handles(function (target) { return true; }); } exports.rewriteIncludeProcessor = rewriteIncludeProcessor; function ifBlockRewriteCellText(text, writeCellContent, write, transformer) { var filterRegEx = /\[pass, role="__asciiDoctorGettextIfBlockHack__"\]\n([^\n]+)/g; var cursor = 0; while (true) { var matches = filterRegEx.exec(text); if (matches === null) { writeCellContent(text.substr(cursor, text.length)); return; } var matchStart = filterRegEx.lastIndex - matches[0].length; if (cursor < filterRegEx.lastIndex) { writeCellContent(text.substring(cursor, matchStart)); } write(macroSourceFromIfData(matches[1], transformer)); cursor = filterRegEx.lastIndex; } } exports.ifBlockRewriteCellText = ifBlockRewriteCellText; //# sourceMappingURL=conditionals.js.map