Skip to content

Commit

Permalink
Compile to markdown when output is markdown file.
Browse files Browse the repository at this point in the history
Render or compile to markdown depending on the output file extension, or through the -c option.
  • Loading branch information
maximeaubaret authored and danielgtaylor committed Jun 9, 2015
1 parent bcf5971 commit 1e2e139
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ lib
test-js
coverage.html
example.html
example-compiled.md
28 changes: 18 additions & 10 deletions src/bin.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ parser = require('yargs')
.options('h', alias: 'host', describe: 'Address to bind local preview server to', default: '127.0.0.1')
.options('p', alias: 'port', describe: 'Port for local preview server', default: 3000)
.options('v', alias: 'version', describe: 'Display version number', default: false)
.options('c', alias: 'compile', describe: 'Compile the markdown file')
.epilog('See https://github.com/danielgtaylor/aglio#readme for more information')

# Console color settings for error/warnings
Expand Down Expand Up @@ -119,20 +120,27 @@ exports.run = (argv=parser.argv, done=->) ->

done()
else
# Render API Blueprint, requires input/output files
# Render or Compile API Blueprint, requires input/output files
if not argv.i or not argv.o
parser.showHelp()
return done 'Invalid arguments'

aglio.renderFile argv.i, argv.o, argv, (err, warnings) ->
if err
lineNo = getLineNo err.input, err
if lineNo?
console.error cErr(">> Line #{lineNo}:") + " #{err.message} (error code #{err.code})"
else
if argv.c or argv.o.match /\.apib$/ or argv.o.match /\.md$/
aglio.compileFile argv.i, argv.o, (err) ->
if (err)
console.error cErr('>>') + " #{JSON.stringify(err)}"
return done err

logWarnings warnings
done()
else
aglio.renderFile argv.i, argv.o, argv, (err, warnings) ->
if err
lineNo = getLineNo err.input, err
if lineNo?
console.error cErr(">> Line #{lineNo}:") + " #{err.message} (error code #{err.code})"
else
console.error cErr('>>') + " #{JSON.stringify(err)}"
return done err

logWarnings warnings

done()
done()
24 changes: 24 additions & 0 deletions src/main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,27 @@ exports.renderFile = (inputFile, outputFile, options, done) ->
chunk = process.stdin.read()
if chunk?
render chunk

# Compile markdown from/to files
exports.compileFile = (inputFile, outputFile, done) ->
compile = (input) ->
compiled = includeDirective path.dirname(inputFile), input

if outputFile isnt '-'
fs.writeFile outputFile, compiled, (err) ->
done err
else
console.log compiled
done null

if inputFile isnt '-'
fs.readFile inputFile, encoding: 'utf-8', (err, input) ->
if err then return done(err)
compile input.toString()
else
process.stdin.setEncoding 'utf-8'
process.stdin.on 'readable', ->
chunk = process.stdin.read()
if chunk?
compile chunk

43 changes: 42 additions & 1 deletion test/basic.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ describe 'API Blueprint Renderer', ->

assert process.stdin.read.called
process.stdin.read.restore()
process.stdin.removeAllListeners()

done()

Expand All @@ -110,11 +111,43 @@ describe 'API Blueprint Renderer', ->

done()

it 'Should compile from/to files', (done) ->
src = path.join root, 'example.apib'
dest = path.join root, 'example-compiled.md'
aglio.compileFile src, dest, done

it 'Should compile from stdin', (done) ->
sinon.stub process.stdin, 'read', -> '# Hello\n'

setTimeout -> process.stdin.emit 'readable', 1

aglio.compileFile '-', 'example-compiled.md', (err) ->
if err then return done(err)

assert process.stdin.read.called
process.stdin.read.restore()
process.stdin.removeAllListeners()

done()

it 'Should compile to stdout', (done) ->
sinon.stub console, 'log'

aglio.compileFile path.join(root, 'example.apib'), '-', (err) ->
if err then return done(err)

assert console.log.called
console.log.restore()

done()

it 'Should error on missing input file', (done) ->
aglio.renderFile 'missing', 'output.html', 'default', (err, html) ->
assert err

done()
aglio.compileFile 'missing', 'output.md', (err) ->
assert err
done()

it 'Should error on bad template', (done) ->
aglio.render blueprint, 'bad', (err, html) ->
Expand Down Expand Up @@ -194,6 +227,14 @@ describe 'Executable', ->
aglio.renderFile.restore()
done()

it 'Should compile a file', (done) ->
sinon.stub aglio, 'compileFile', (i, o, callback) ->
callback null

bin.run c: 1, i: path.join(root, 'example.md'), o: '-', ->
aglio.compileFile.restore()
done()

it 'Should start a live preview server', (done) ->
@timeout 5000

Expand Down

0 comments on commit 1e2e139

Please sign in to comment.