Merge branch 'master' into develop

This commit is contained in:
Estevao Soares dos Santos 2022-02-24 00:06:58 +00:00
commit cb2e4853b7
2 changed files with 38 additions and 28 deletions

View File

@ -34,12 +34,17 @@ var argv = yargs.argv,
command = argv._[0]; command = argv._[0];
if (command === 'makehtml') { if (command === 'makehtml') {
require('./makehtml.cmd.js').run(); require('./makehtml.cmd.js').run(function (err) {
if (err) {
console.error(err);
}
});
} else { } else {
yargs.showHelp(); yargs.showHelp();
process.exit(0);
} }
if (argv.help) { if (argv.help) {
yargs.showHelp(); yargs.showHelp();
process.exit(0);
} }
process.exit(0);

View File

@ -72,7 +72,7 @@ for (var opt in showdownOptions) {
} }
} }
function run () { function run (cb) {
'use strict'; 'use strict';
var argv = yargs.argv, var argv = yargs.argv,
readMode = (!argv.i || argv.i === '') ? 'stdin' : 'file', readMode = (!argv.i || argv.i === '') ? 'stdin' : 'file',
@ -83,14 +83,13 @@ function run () {
* @type {Messenger} * @type {Messenger}
*/ */
messenger = new Messenger(msgMode, argv.q, argv.m), messenger = new Messenger(msgMode, argv.q, argv.m),
read = (readMode === 'stdin') ? readFromStdIn : readFromFile,
write = (writeMode === 'stdout') ? writeToStdOut : writeToFile, write = (writeMode === 'stdout') ? writeToStdOut : writeToFile,
enc = argv.encoding || 'utf8', enc = argv.encoding || 'utf8',
flavor = argv.p, flavor = argv.p,
append = argv.a || false, append = argv.a || false,
options = parseOptions(flavor), options = parseOptions(flavor),
converter = new showdown.Converter(options), converter = new showdown.Converter(options),
md, html; html;
// Load extensions // Load extensions
if (argv.e) { if (argv.e) {
@ -109,7 +108,11 @@ function run () {
messenger.printMsg('...'); messenger.printMsg('...');
// read the input // read the input
messenger.printMsg('Reading data from ' + readMode + '...'); messenger.printMsg('Reading data from ' + readMode + '...');
md = read(enc);
readFrom(argv.i, enc, function (err, md) {
if (err) {
return cb(err);
}
// process the input // process the input
messenger.printMsg('Parsing markdown...'); messenger.printMsg('Parsing markdown...');
@ -119,6 +122,8 @@ function run () {
messenger.printMsg('Writing data to ' + writeMode + '...'); messenger.printMsg('Writing data to ' + writeMode + '...');
write(html, append); write(html, append);
messenger.okExit(); messenger.okExit();
cb();
});
function parseOptions (flavor) { function parseOptions (flavor) {
var options = {}, var options = {},
@ -156,22 +161,22 @@ function run () {
return options; return options;
} }
function readFromStdIn () { function readFrom (src, enc, cb) {
try { var stream = process.stdin;
var size = fs.fstatSync(process.stdin.fd).size; if (src && src.length) {
return size > 0 ? fs.readSync(process.stdin.fd, size)[0] : ''; stream = fs.createReadStream(src, {encoding: enc});
} catch (e) { } else {
var err = new Error('Could not read from stdin, reason: ' + e.message); process.stdin.setEncoding(enc);
messenger.errorExit(err); process.stdin.resume();
}
}
function readFromFile (encoding) {
try {
return fs.readFileSync(argv.i, encoding);
} catch (err) {
messenger.errorExit(err);
} }
var data = '';
stream.on('data', function (chunk) {
data += chunk.toString();
});
stream.on('end',function () {
cb(null, data);
});
stream.on('error', cb);
} }
function writeToStdOut (html) { function writeToStdOut (html) {