Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Command Line Interface #209

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

[WIP] Command Line Interface #209

wants to merge 3 commits into from

Conversation

domchristie
Copy link
Collaborator

Closes #29
Closes #110

I’m not experienced in designing CLIs—any thoughts @asimjalis @academyofzhuang @michaelandrepearce?

@michaelandrepearce
Copy link

michaelandrepearce commented Dec 19, 2017

@domchristie if it helps i actually ended up finding https://github.com/fabianmoronzirfas/to-markdown-cli which is based on old version of turndown (when was to-markdown), but with minor mods, i got it running with latest.

#!/usr/bin/env node

/* eslint no-undefined: "off" */

const fs = require('fs');
const path = require('path');

const program = require('commander');
const toMarkdown = require('turndown');
const clipboardy = require('clipboardy');

const pkg = require('./package.json');

let inPath = null;
let outPath = null;
let toStdout = true;
let data = '';

function parseInPath (val) {
  if(fs.existsSync(path.resolve(process.cwd(), val))) {
    inPath = val;
  }else{
    console.log('the specified file path for the input file does not exist');
    // process.exit(1);
  }
}

function parseOutPath(val) {
  outPath = val;
}


program
  .version(pkg.version)
  .option('-i, --input <input>', 'path to the input file', parseInPath)
  .option('-o, --output <output>', 'path to the output file', parseOutPath)
  .option('-c, --clipboard', 'use only the clipboard for input and output');

program.on('--help', ()=>{
  console.log('');
  console.log('    html2md turns html into markdown');
  console.log('    if no input file is given it ueses the clipboard content');
  console.log('    if no output file is given it logs the result to stdout');
  console.log('');
  console.log('    Optional Options:');
  console.log('        -i, --input <input>', 'path to the input file');
  console.log('        -o, --output <output>', 'path to the output file');
  console.log('        -c, --clipboard', 'use the clipboard only. All other options will be dismissed');
  console.log('');
  console.log('    Examples:');
  console.log('        $ html2md -i ./foo.html <= output to stdout');
  console.log('        $ html2md -i ./foo.html -o out.md <= output to out.md');
  console.log('        $ html2md -o out.md <= clipboard to out.md');
  console.log('        $ html2md -c <= clipboard to clipboard');
  console.log('        $ html2md <= clipboard to stdout');

  console.log('');
  console.log('    Acknowledgments:');
  console.log('        Build on these great modules:');
  console.log('        - https://github.com/domchristie/to-markdown');
  console.log('        - https://github.com/sindresorhus/clipboardy');
  console.log('        - https://github.com/tj/commander.js');
  console.log('');

});
program.parse(process.argv);

var TurndownService = require('turndown')

var turndownService = new TurndownService()

if(program.clipboard !== undefined) {
  data = clipboardy.readSync();
  data = turndownService.turndown(data);
  clipboardy.writeSync(data);
  process.exit(0);
}
if(program.input !== undefined) {
  data = fs.readFileSync(inPath, 'utf8');
}else{
  data = clipboardy.readSync();
}

if(program.output !== undefined) {
  fs.writeFile(outPath, turndownService.turndown(data), 'utf8', (error, md) => {
    if(error) {
      throw error;
    }
    console.log(`wrote to ${outPath}`);
  });
}else{
  console.log(turndownService.turndown(data));
}

@GerkinDev
Copy link

Would be nice to have this merged.

@mals14
Copy link

mals14 commented Jan 8, 2019

It appears that it should be possible to fun turndown from command line, but I could not. I even tried to copy the above code into a turndown.js file and tried to run it after making it executable. but it appears that alone was not enough.

any tips on how turndown can be run from the command line?

@gpotter2
Copy link

gpotter2 commented May 21, 2019

Hi, I made #284 which is a remix of the code posted above, and configured it in package.json.

You may want to add all the options this PR contains (I did not), but I would advice to merge it first, as I probably won't have the time required to add support for all options.

Feel free to have a look

@mals14
Copy link

mals14 commented May 24, 2019

Hi, I made #284 which is a remix of the code posted above, and configured it in package.json.

You may want to add all the options this PR contains (I did not), but I would advice to merge it first, as I probably won't have the time required to add support for all options.

Feel free to have a look

Thank you @gpotter2. Thank you - #284 looks good, except I do not know how to use it. Meaning whether I should download the current version of Turndown and expect it to work or does something else has to be done - sounded like the latter with your reference to package.json etc.

@ff6347
Copy link

ff6347 commented Dec 18, 2019

Just saw this here today.

  1. I update the cli to accept stdin
  2. If you need help with the cli let me know

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Use from command line
6 participants