marked/bin/marked.js

209 lines
4.1 KiB
JavaScript
Raw Normal View History

2011-08-15 20:38:13 -05:00
#!/usr/bin/env node
2011-08-10 21:51:44 -05:00
/**
2011-10-14 16:54:08 -05:00
* Marked CLI
2013-01-03 07:07:09 -06:00
* Copyright (c) 2011-2013, Christopher Jeffrey (MIT License)
2011-08-10 21:51:44 -05:00
*/
import { promises } from 'fs';
import { marked } from '../lib/marked.esm.js';
const { readFile, writeFile } = promises;
2011-10-14 16:36:14 -05:00
2012-01-28 13:09:38 -06:00
/**
* Man Page
*/
async function help() {
const { spawn } = await import('child_process');
2019-11-06 14:57:24 -06:00
const options = {
cwd: process.cwd(),
env: process.env,
setsid: false,
stdio: 'inherit'
};
const { dirname, resolve } = await import('path');
const { fileURLToPath } = await import('url');
const __dirname = dirname(fileURLToPath(import.meta.url));
spawn('man', [resolve(__dirname, '../man/marked.1')], options)
.on('error', async() => {
console.log(await readFile(resolve(__dirname, '../man/marked.1.txt'), 'utf8'));
});
2013-01-03 07:07:09 -06:00
}
2011-08-10 21:51:44 -05:00
async function version() {
const { createRequire } = await import('module');
const require = createRequire(import.meta.url);
2019-11-06 14:57:24 -06:00
const pkg = require('../package.json');
2019-03-12 09:07:26 -05:00
console.log(pkg.version);
}
2012-01-28 13:09:38 -06:00
/**
* Main
*/
async function main(argv) {
const files = [];
const options = {};
let input;
let output;
let string;
let arg;
let tokens;
let opt;
2011-10-14 16:36:14 -05:00
2019-04-26 20:58:04 -05:00
function getarg() {
2019-11-06 14:57:24 -06:00
let arg = argv.shift();
2013-01-05 07:20:50 -06:00
if (arg.indexOf('--') === 0) {
// e.g. --opt
arg = arg.split('=');
if (arg.length > 1) {
// e.g. --opt=val
argv.unshift(arg.slice(1).join('='));
}
arg = arg[0];
} else if (arg[0] === '-') {
if (arg.length > 2) {
// e.g. -abc
2019-04-26 20:58:04 -05:00
argv = arg.substring(1).split('').map(function(ch) {
2013-01-05 07:20:50 -06:00
return '-' + ch;
}).concat(argv);
arg = argv.shift();
} else {
// e.g. -a
}
} else {
// e.g. foo
2011-10-14 16:36:14 -05:00
}
2013-01-05 07:20:50 -06:00
return arg;
2013-01-03 07:07:09 -06:00
}
2011-10-14 16:36:14 -05:00
while (argv.length) {
arg = getarg();
switch (arg) {
case '-o':
case '--output':
2011-10-14 17:29:15 -05:00
output = argv.shift();
2011-10-14 16:36:14 -05:00
break;
case '-i':
case '--input':
2011-10-14 17:29:15 -05:00
input = argv.shift();
2011-10-15 02:34:08 -05:00
break;
2018-03-28 10:18:33 -05:00
case '-s':
case '--string':
string = argv.shift();
break;
2011-10-15 02:34:08 -05:00
case '-t':
case '--tokens':
tokens = true;
2011-10-14 16:36:14 -05:00
break;
case '-h':
case '--help':
return await help();
2019-03-12 09:07:26 -05:00
case '-v':
case '--version':
return await version();
2011-10-14 16:36:14 -05:00
default:
2013-01-03 07:07:09 -06:00
if (arg.indexOf('--') === 0) {
2013-01-09 20:45:47 -06:00
opt = camelize(arg.replace(/^--(no-)?/, ''));
if (!marked.defaults.hasOwnProperty(opt)) {
continue;
}
if (arg.indexOf('--no-') === 0) {
options[opt] = typeof marked.defaults[opt] !== 'boolean'
? null
: false;
2013-01-03 07:07:09 -06:00
} else {
2013-01-09 20:45:47 -06:00
options[opt] = typeof marked.defaults[opt] !== 'boolean'
? argv.shift()
: true;
2013-01-03 07:07:09 -06:00
}
} else {
files.push(arg);
}
2011-10-14 16:36:14 -05:00
break;
}
}
2011-08-10 21:51:44 -05:00
async function getData() {
2013-01-03 07:07:09 -06:00
if (!input) {
if (files.length <= 2) {
2018-03-28 10:41:02 -05:00
if (string) {
return string;
2018-03-28 10:41:02 -05:00
}
return await getStdin();
2013-01-03 07:07:09 -06:00
}
input = files.pop();
2012-01-28 13:09:38 -06:00
}
return await readFile(input, 'utf8');
2013-01-03 07:07:09 -06:00
}
2011-10-14 16:36:14 -05:00
const data = await getData();
2012-03-10 16:33:51 -06:00
const html = tokens
? JSON.stringify(marked.lexer(data, options), null, 2)
: marked(data, options);
2011-10-15 02:34:08 -05:00
if (output) {
return await writeFile(output, data);
}
2013-01-05 07:20:50 -06:00
process.stdout.write(html + '\n');
2013-01-03 07:07:09 -06:00
}
2011-08-10 21:51:44 -05:00
2013-01-05 07:20:50 -06:00
/**
* Helpers
*/
function getStdin() {
return new Promise((resolve, reject) => {
const stdin = process.stdin;
let buff = '';
2013-01-05 07:20:50 -06:00
stdin.setEncoding('utf8');
2013-01-05 07:20:50 -06:00
stdin.on('data', function(data) {
buff += data;
});
2013-01-05 07:20:50 -06:00
stdin.on('error', function(err) {
reject(err);
});
2013-01-05 07:20:50 -06:00
stdin.on('end', function() {
resolve(buff);
});
2013-01-05 07:20:50 -06:00
stdin.resume();
});
2013-01-05 07:20:50 -06:00
}
2019-04-26 20:58:04 -05:00
function camelize(text) {
return text.replace(/(\w)-(\w)/g, function(_, a, b) {
2013-01-09 20:45:47 -06:00
return a + b.toUpperCase();
});
}
2019-04-26 20:58:04 -05:00
function handleError(err) {
2018-12-20 00:12:33 -02:00
if (err.code === 'ENOENT') {
2019-04-25 13:07:58 -05:00
console.error('marked: output to ' + err.path + ': No such directory');
2018-12-20 00:12:33 -02:00
return process.exit(1);
}
throw err;
}
2013-01-05 07:20:50 -06:00
/**
* Expose / Entry Point
*/
process.title = 'marked';
main(process.argv.slice()).then(code => {
process.exit(code || 0);
}).catch(err => {
handleError(err);
});