Skip to content

Instantly share code, notes, and snippets.

@vsemozhetbyt
Last active January 17, 2016 09:04
Show Gist options
  • Save vsemozhetbyt/2e009fc94dd510005ca4 to your computer and use it in GitHub Desktop.
Save vsemozhetbyt/2e009fc94dd510005ca4 to your computer and use it in GitHub Desktop.
'use strict';
/******************************************************************************/
const path = process.argv[2];
if (!path) {
console.log('Add the file path, please.');
process.exit();
}
/******************************************************************************/
const fs = require('fs');
const rl = require('readline').createInterface({
input: fs.createReadStream(path, {encoding: 'utf16le'}), //ascii utf8 utf16le
terminal: false,
historySize: 0
});
const outfile = fs.openSync(path.replace(/(\.[^.]+)?$/, '.new$1'), 'w');
const isDirective = /^#/;
const isHeadword = /^[^#\s]/;
const isBody = /^\s+\S/;
const directiveRE = /^#ICON_FILE.+/;
const headwordRE = /\w+/g;
const bodyRE = /\[m(\d)\]/g;
const exceptions = ['ABBYY'];
/******************************************************************************/
rl.on('line', line => {
if (isDirective.test(line)) {
line = line.replace(directiveRE, '');
} else if (isHeadword.test(line)) {
line = line.replace(headwordRE, mtch => {
if (exceptions.indexOf(mtch) === -1) {
return mtch.toLowerCase();
} else {
return mtch;
}
});
} else if (isBody.test(line)) {
line = line.replace(bodyRE, (mtch, num) => {
return `[m${++num}]`;
});
}
fs.writeSync(outfile, `${line}\n`, null, 'utf16le');
});
/******************************************************************************/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment