Introduce JSON-only output

For a specific use case where I want to arrange the output otherwise,
I require the ability to manipulate the output as JSON. So I added
an option for that.
merge-requests/1/head
Olivier Tremblay 4 years ago
parent 3756985dd4
commit c8461326a1

@ -55,6 +55,7 @@ Options:
-U --is-url Interpret SOURCE as a URL rather than file name -U --is-url Interpret SOURCE as a URL rather than file name
-q --quiet Don't output extra information to stderr -q --quiet Don't output extra information to stderr
-l --low-confidence MODE What to do if Readability.js is uncertain about what the core content actually is -l --low-confidence MODE What to do if Readability.js is uncertain about what the core content actually is
-j --json Output properties as a JSON payload
The --low-confidence option determines what should be done for documents where Readability can't tell what the core content is: The --low-confidence option determines what should be done for documents where Readability can't tell what the core content is:
@ -82,7 +83,7 @@ Default value is "html-title,html-content".`);
const stringArgParams = ['_', "--", "low-confidence", "output", "properties", "url", "base"]; const stringArgParams = ['_', "--", "low-confidence", "output", "properties", "url", "base"];
const boolArgParams = ["quiet", "help", "version", "is-url"]; const boolArgParams = ["quiet", "help", "version", "is-url", "json"];
const alias = { const alias = {
"url": 'u', "url": 'u',
"output": 'o', "output": 'o',
@ -92,7 +93,8 @@ const alias = {
"is-url": 'U', "is-url": 'U',
"quiet": 'q', "quiet": 'q',
"low-confidence": 'l', "low-confidence": 'l',
"help": 'h' "help": 'h',
"json": 'j'
} }
let args = parseArgs(process.argv.slice(2), { let args = parseArgs(process.argv.slice(2), {
@ -185,7 +187,7 @@ delete args['--'];
const outputArg = args['output']; const outputArg = args['output'];
const documentURL = args["base"] || inputURL; const documentURL = args["base"] || inputURL;
const outputJSON = args['json'];
const Properties = { const Properties = {
@ -332,6 +334,10 @@ function onLoadDOM(dom) {
setErrored(ExitCodes.dataError); setErrored(ExitCodes.dataError);
return; return;
} }
if (outputJSON){
writeStream.write(JSON.stringify(article));
return;
}
if (wantedProperties.includes(Properties.title)) { if (wantedProperties.includes(Properties.title)) {
writeStream.write(`Title: ${article.title}\n`); writeStream.write(`Title: ${article.title}\n`);

Loading…
Cancel
Save