2014-09-15 15:06:37 -06:00
|
|
|
###
|
|
|
|
Requires http: //johnmacfarlane.net/pandoc/
|
|
|
|
###
|
2015-01-01 21:53:55 -07:00
|
|
|
fs = null
|
|
|
|
yaml = null
|
|
|
|
allowUnsafeNewFunction = null
|
2014-09-15 15:06:37 -06:00
|
|
|
|
2015-01-01 21:53:55 -07:00
|
|
|
getCmd = (inputPath, outputPath, options, cb) ->
|
|
|
|
optionsStr = " --read markdown --write markdown --output \"" + outputPath + "\" \"" + inputPath + "\""
|
2014-09-15 15:06:37 -06:00
|
|
|
pandocPath = options.markdown_beautifier_path # jshint ignore: line
|
2015-01-01 21:53:55 -07:00
|
|
|
yamlFrontMatter = options.yaml_front_matter # jshint ignore: line
|
|
|
|
cmd = ""
|
|
|
|
if pandocPath?
|
2014-09-15 15:06:37 -06:00
|
|
|
# Use absolute path
|
2015-01-01 21:53:55 -07:00
|
|
|
cmd = pandocPath + optionsStr
|
2014-09-15 15:06:37 -06:00
|
|
|
else
|
|
|
|
# Use command available in $PATH
|
2015-01-01 21:53:55 -07:00
|
|
|
cmd = "pandoc" + optionsStr
|
|
|
|
|
|
|
|
if yamlFrontMatter?
|
|
|
|
# console.log("YAML Front Matter!")
|
|
|
|
fs ?= require "fs"
|
|
|
|
fs.readFile(inputPath, (err, contents) ->
|
|
|
|
# console.log('readFile', err, contents)
|
|
|
|
return cb(err) if err
|
|
|
|
|
|
|
|
# Parse with YAML front Matter
|
2015-01-01 22:30:24 -07:00
|
|
|
yaml ?= require "yaml-front-matter"
|
2015-01-01 21:53:55 -07:00
|
|
|
# console.log('Parse YAML Front Matter')
|
|
|
|
allowUnsafeNewFunction ?= require("loophole").allowUnsafeNewFunction
|
|
|
|
results = null
|
|
|
|
try
|
|
|
|
allowUnsafeNewFunction ->
|
2015-01-01 22:30:24 -07:00
|
|
|
results = yaml.loadFront(contents)
|
2015-01-01 21:53:55 -07:00
|
|
|
catch e
|
|
|
|
return cb(e)
|
|
|
|
newContents = results.__content # jshint ignore: line
|
|
|
|
delete results.__content # jshint ignore: line
|
|
|
|
# console.log('newContents', newContents)
|
|
|
|
# Write out new contents to input file
|
|
|
|
fs.writeFile(inputPath, newContents, (err) ->
|
|
|
|
# console.log('writeFile', err)
|
|
|
|
return cb(err) if err
|
|
|
|
|
|
|
|
# Completetion callback to combine YAML Front Matter and Markdown
|
|
|
|
completionCallback = (output, callback) ->
|
|
|
|
# console.log('Completion callback!')
|
|
|
|
try
|
|
|
|
# Pre-pend YAML Front Matter to top of Markdown output
|
|
|
|
front = yaml.dump(results)
|
2015-01-01 22:30:24 -07:00
|
|
|
# Check if there is valid `front` to prepend
|
|
|
|
if front isnt "{}\n"
|
|
|
|
output = "---\n#{front}---\n\n#{output}"
|
2015-01-01 21:53:55 -07:00
|
|
|
# console.log('final output!', output)
|
|
|
|
return callback(output)
|
|
|
|
catch e
|
|
|
|
return callback(e)
|
|
|
|
# Run it all together now!
|
|
|
|
# console.log('Run!')
|
|
|
|
return cb(cmd, completionCallback)
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return # Use Callback
|
|
|
|
else
|
|
|
|
return cmd # Return cmd synchronously
|
2014-09-15 15:06:37 -06:00
|
|
|
"use strict"
|
|
|
|
cliBeautify = require("./cli-beautify")
|
|
|
|
module.exports = cliBeautify(getCmd)
|