UNPKG

taraskevizer

Version:

Канвэртацыя акадэмічнага правапісу ў клясычны

207 lines (194 loc) 4.63 kB
#!/usr/bin/env node import { dicts, TaraskConfig, pipelines, htmlConfigOptions, wrappers } from './index.js'; const printWithPrefix = (msg) => { process.stdout.write("[taraskevizer]" + ' ' + msg + '\n'); }; process.argv.splice(0, 2); const firstArg = process.argv[0]; if (firstArg) { if (firstArg === '-v' || firstArg === '--version') { printWithPrefix("10.4.6"); process.exit(0); } if (firstArg === '-h' || firstArg === '--help') { printWithPrefix(`Usage: tarask [options] text If text is not passed, interactive mode will be enabled EXAMPLES Convert and latinize a word tarask --latin 'планета' Will print "planeta" Read from one file and write converted text to another tarask < ./cyr-text.txt > ./lat-text.txt Enter interactive mode tarask Will print "[taraskevizer] Enter the text:" and wait until you press Enter OPTIONS General: -h --help -v --version Alphabet: -l --latin -lj --latin-ji -a --arabic When to replace і(i) by й(j) after vowels: -jr --jrandom -ja --jalways Replace ґ(g) by г(h) in cyrillic alphabet: --h Variations: -nv --no-variations -fv --first-variation Mode (only one can be used): -html --html -abc --alphabet-only Other: -nec --not-escape-caps -nc --no-color `); process.exit(0); } } let cfg = { g: true, variations: 'all', wrappers: wrappers.ansiColor, }; let mode = 'tarask'; const toHashTable = (dict) => { const result = {}; for (const { 0: options, 1: callback } of dict) for (const option of options) result[option] = callback; return result; }; let isHtml = false; const optionDict = toHashTable([ [ ['--latin', '-l'], () => { cfg.abc = dicts.alphabets.latin; }, ], [ ['--latin-ji', '-lj'], () => { cfg.abc = dicts.alphabets.latinJi; }, ], [ ['--arabic', '-a'], () => { cfg.abc = dicts.alphabets.arabic; }, ], [ ['--jrandom', '-jr'], () => { cfg.j = 'random'; }, ], [ ['--jalways', '-ja'], () => { cfg.j = 'always'; }, ], [ ['--no-escape-caps', '-nec'], () => { cfg.doEscapeCapitalized = false; }, ], [ ['--h'], () => { cfg.g = false; }, ], [ ['--no-variations', '-nv'], () => { cfg.variations = 'no'; }, ], [ ['--first-variation', '-fv'], () => { cfg.variations = 'first'; }, ], [ ['--no-color', '-nc'], () => { cfg.wrappers = null; }, ], [ ['--html', '-html'], () => { isHtml = true; cfg.wrappers = htmlConfigOptions.wrappers; }, ], [ ['--alphabet-only', '-abc'], () => { mode = 'alphabetic'; }, ], [ ['--phonetic', '-ph'], () => { mode = 'phonetic'; }, ], ]); let currOption; process.argv.reverse(); while ((currOption = process.argv.pop())) { if (currOption in optionDict) { optionDict[currOption](); } else { process.argv.push(currOption); break; } } let text = ''; if (process.argv.length) { text = process.argv.reverse().join(' '); } else { const chunks = []; let length = 0; if (process.stdin.isTTY) { printWithPrefix('Enter the text'); for await (const chunk of process.stdin) { chunks.push(chunk); length += chunk.length; if (chunk.includes('\n')) break; } } else { for await (const chunk of process.stdin) { chunks.push(chunk); length += chunk.length; } } text = Buffer.concat(chunks, length).toString(); } cfg = new TaraskConfig(isHtml ? { ...htmlConfigOptions, ...cfg, } : cfg); if (process.stdout.write(pipelines[mode](text, cfg) + '\n')) { process.exit(0); } else { process.stdout.once('drain', () => { process.exit(0); }); }