UNPKG

asciidoctor-gettext

Version:

gettext/po string extraction tool for asciidoc documents

154 lines 6.53 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var commander_1 = require("commander"); var extract_1 = require("./extract"); var gettext_parser_1 = require("gettext-parser"); var adapter_1 = require("./adapter"); var fs_1 = require("fs"); var rewrite_1 = require("./rewrite"); var asciidoctor_js_1 = __importDefault(require("asciidoctor.js")); function getBlacklistRegexes(program) { var blacklistRegexes = program.ignore.map(function (pattern) { try { return RegExp(pattern); } catch (e) { if (e instanceof SyntaxError) { process.stderr.write("Error in --ignore regular expression \"" + pattern + "\"\n" + e.message); process.exit(1); } throw e; } }); return blacklistRegexes; } function getAttributes(program) { return program.attribute.reduce(function (attributes, arg) { var splitArg = arg.split('='); if (splitArg.length !== 2) { process.stderr.write("Error in --attribute \"" + arg + "\", format must be \"name=value\""); process.exit(1); } var name = splitArg[0], value = splitArg[1]; if (name.length === 0) { process.stderr.write("Error in --attribute \"" + arg + "\", missing name"); process.exit(1); } attributes[name] = value; return attributes; }, {}); } function write(contents, outputPath) { if (outputPath) { fs_1.writeFileSync(outputPath, contents, { encoding: 'utf8' }); } else { process.stdout.write(contents); } } function gettextizeAction(program) { if (!program.master) { program.help(); return; } var extractions = extract_1.extractFile(program.master, { attributeFilter: program.builtinAttrs ? undefined : extract_1.allBuiltinsAttributeFilter, }, { attributes: getAttributes(program), }); var blacklistRegexes = getBlacklistRegexes(program); var translationObject = adapter_1.translationObjectFromExtractions(extractions, { project: program.packageName, projectVersion: program.packageVersion, bugsEmailAddress: program.bugsEmailAddress, }, blacklistRegexes); var poBuffer = gettext_parser_1.po.compile(translationObject); write(poBuffer, program.po); } exports.gettextizeAction = gettextizeAction; function translateAction(program) { if (!program.master || !program.po) { program.help(); return; } var poBuffer = fs_1.readFileSync(program.po); var translationObject = gettext_parser_1.po.parse(poBuffer); var translations = translationObject.translations['']; var transformer = function (text) { var translationEntry = translations[text]; if (!translationEntry) { return text; } if (text === '') { return ''; } var msgstr = translationEntry.msgstr[0]; if (msgstr === '') { return text; } return msgstr; }; var asciidocOptions = { attributes: getAttributes(program), }; // Be prudent and check if the rewritten .html output is the same when using an identity transform: function convertToHtml(input) { var asciidoctor = asciidoctor_js_1.default(); return asciidoctor.convert(input, asciidocOptions); } var untranslatedOutput = rewrite_1.rewriteFile(program.master, function (text) { return text; }, asciidocOptions); var input = fs_1.readFileSync(program.master, { encoding: 'utf8' }); if (convertToHtml(input) !== convertToHtml(untranslatedOutput)) { process.stderr.write("Translation failed. This is probably a bug in rewrite.ts.\nPlease file a bug report and attaching the input asciidoc file.\nhttps://github.com/martijnthe/asciidoctor-gettext/issues/new"); process.exit(2); return; } var output = rewrite_1.rewriteFile(program.master, transformer, asciidocOptions); write(output, program.localized); } exports.translateAction = translateAction; function cli(argv, stdout) { if (stdout === void 0) { stdout = process.stdout; } function collect(val, memo) { memo.push(val); return memo; } var program = new commander_1.Command(); program .description('gettext/po string extraction tool for asciidoc documents'); program .command('gettextize') .description('Extracts texts from asciidoc file and generates a .pot file') .option('-a, --attribute <name=value>', 'Define an attribute.', collect, []) .option('-m, --master <path>', 'File containing the master document to translate.') .option('-p, --po <path>', "File where the message catalog should be written. If not given, the message catalog will be written to the standard output.") .option('-i, --ignore <regex>', 'RegEx pattern of lines that should be ignored.', collect, []) .option('--no-builtin-attrs', 'Do not extract asciidoctor-builtin attributes.', false) .option('--package-name <string>', 'Set the package name for the POT header.', 'PACKAGE') .option('--package-version <string>', 'Set the package version for the POT header.', 'VERSION') .option('--bugs-email-address <email>', "Set the report address for msgid bugs. By default, the created POT files have no Report-Msgid-Bugs-To fields.", '') .action(gettextizeAction); program .command('translate') .description('Writes a new asciidoc file by translating a given asciidoc file and .po file') .option('-m, --master <path>', 'File containing the master document to translate.') .option('-a, --attribute <name=value>', 'Define an attribute.', collect, []) .option('-p, --po <path>', 'File with the message catalog to use to translate the master document.') .option('-l, --localized <path>', "File where the translated document should be written. If not given, the translated document will be written to the standard output.") .action(translateAction); program .command('*') .action(function () { program.help(); }); program.parse(argv); if (program.args.length === 0) { program.help(); return; } } exports.cli = cli; //# sourceMappingURL=cli.js.map