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

Compatibility with vim-markdownfmt #44

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions lib/cli.coffee
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{ArgumentParser} = require 'argparse'

fs = require 'fs'
packageInfo = require '../package'
tidyMarkdown = require './'

Expand All @@ -10,6 +11,16 @@ argparser = new ArgumentParser(
version: packageInfo.version
)

argparser.addArgument(
['path'],
action: 'store',
type: 'string',
help: 'Filename to read (default: STDIN)'
defaultValue: null,
nargs: '?',
required: false
)

argparser.addArgument(
['--no-ensure-first-header-is-h1']
action: 'storeFalse'
Expand All @@ -20,13 +31,33 @@ argparser.addArgument(
dest: 'ensureFirstHeaderIsH1'
)

argparser.addArgument(
['-w', '--write']
action: 'storeTrue',
help: 'Write result to (source) file instead of stdout',
dest: 'write'
)

argv = argparser.parseArgs()

process.stdin.setEncoding 'utf8'
process.stdin.on 'readable', ->

if argv.path is null
input = process.stdin
input.setEncoding 'utf8'
else
input = fs.createReadStream(argv.path, {encoding: 'utf8'})

input.on 'readable', ->
buffer = ''
while (chunk = process.stdin.read()) isnt null
while (chunk = input.read()) isnt null
buffer += chunk
input.close()

if argv.write
output = fs.createWriteStream(argv.path, {encoding: 'utf8'})
else
output = process.stdout

if buffer isnt ''
process.stdout.write tidyMarkdown(buffer, argv)
output.write tidyMarkdown(buffer, argv)
return