taraskevizer
Version:
Канвэртацыя акадэмічнага правапісу ў клясычны
207 lines (194 loc) • 4.63 kB
JavaScript
import { dicts, TaraskConfig, pipelines, htmlConfigOptions, wrappers } from './index.js';
const printWithPrefix = (msg) => {
process.stdout.write("[34m[taraskevizer][0m" + ' ' + 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: [34mtarask[0m [options] text
If text is not passed, interactive mode will be enabled
EXAMPLES
Convert and latinize a word
[34mtarask[0m [35m--latin[0m 'планета'
Will print "p[32ml[0ma[32mne[0mta"
Read from one file and write converted text to another
[34mtarask[0m < ./cyr-text.txt > ./lat-text.txt
Enter interactive mode
[34mtarask[0m
Will print "[34m[taraskevizer][0m Enter the text:" and wait until you press Enter
OPTIONS
[33mGeneral[0m:
[35m-h[0m [35m--help[0m
[35m-v[0m [35m--version[0m
[33mAlphabet[0m:
[35m-l[0m [35m--latin[0m
[35m-lj[0m [35m--latin-ji[0m
[35m-a[0m [35m--arabic[0m
[33mWhen to replace і(i) by й(j) after vowels[0m:
[35m-jr[0m [35m--jrandom[0m
[35m-ja[0m [35m--jalways[0m
[33mReplace ґ(g) by г(h) in cyrillic alphabet[0m:
[35m--h[0m
[33mVariations[0m:
[35m-nv[0m [35m--no-variations[0m
[35m-fv[0m [35m--first-variation[0m
[33mMode (only one can be used)[0m:
[35m-html[0m [35m--html[0m
[35m-abc[0m [35m--alphabet-only[0m
[33mOther[0m:
[35m-nec[0m [35m--not-escape-caps[0m
[35m-nc[0m [35m--no-color[0m
`);
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);
});
}