showdown/bin/showdown.js

2 lines
5.4 KiB
JavaScript
Raw Normal View History

2015-07-13 12:09:03 +08:00
#!/usr/bin/env node
var showdown,fs=require("fs"),json=JSON.parse(fs.readFileSync("package.json","utf8")),version=json.version,Command=require("commander").Command,program=new Command;function Messenger(o,t,n){"use strict";t=!!t||!!n,n=!!n,this._print="stdout"===(o=o||"stderr")?console.log:console.error,this.errorExit=function(o){n||(console.error("ERROR: "+o.message),console.error("Run 'showdown <command> -h' for help")),process.exit(1)},this.okExit=function(){n||(this._print("\n"),this._print("DONE!")),process.exit(0)},this.printMsg=function(o){t||n||!o||this._print(o)},this.printError=function(o){n||console.error(o)}}function showShowdownOptions(){"use strict";var o,t=showdown.getDefaultOptions(!1);for(o in console.log("\nshowdown makehtml config options:"),t)t.hasOwnProperty(o)&&console.log(" "+o+":","[default="+t[o].defaultValue+"]",t[o].describe);console.log('\n\nExample: showdown makehtml -c openLinksInNewWindow ghMentions ghMentionsLink="https://google.com"')}function parseShowdownOptions(o,t){"use strict";var n=t;if(o)for(var e=0;e<o.length;++e){var r=o[e],i=o[e],s=!0;/=/.test(r)&&(i=r.split("=")[0],s=r.split("=")[1]),n[i]=s}return n}function readFromStdIn(o){"use strict";var t=fs.fstatSync(process.stdin.fd).size;if(t<=0)throw new Error("Could not read from stdin, reason: stdin is empty");o=o||"utf8";try{return 0<t?fs.readFileSync(process.stdin.fd,o).toString():""}catch(o){throw new Error("Could not read from stdin, reason: "+o.message)}}function readFromFile(t,o){"use strict";try{return fs.readFileSync(t,o)}catch(o){throw new Error("Could not read from file "+t+", reason: "+o.message)}}function writeToStdOut(o){"use strict";if(!process.stdout.write(o))throw new Error("Could not write to StdOut")}function writeToFile(o,t,n){"use strict";n=n?fs.appendFileSync:fs.writeFileSync;try{n(t,o)}catch(o){throw new Error("Could not write to file "+t+", readon: "+o.message)}}function makehtmlCommand(t,o){"use strict";if(t.configHelp)showShowdownOptions();else{var n,e,r,i=!!o.parent._optionValues.quiet,o=!!o.parent._optionValues.mute,s=t.input&&""!==t.input&&!0!==t.input?"file":"stdin",a=t.output&&""!==t.output&&!0!==t.output?"file":"stdout",d=new Messenger("file"==a?"stdout":"stderr",i,o),i=showdown.getDefaultOptions(!0);if(t.flavor){if(d.printMsg("Enabling flavor "+t.flavor+"..."),!(i=showdown.getFlavorOptions(t.flavor)))return void d.errorExit(new Error("Flavor "+t.flavor+" is not recognised"));d.printMsg("OK!")}for(e in t.config=parseShowdownOptions(t.config,i),t.config)t.config.hasOwnProperty(e)&&!0===t.config[e]&&d.printMsg("Enabling option "+e);d.printMsg("\nInitializing converter...");try{r=new showdown.Converter(t.config)}catch(o){return void d.errorExit(o)}if(d.printMsg("OK!"),t.extensions){d.printMsg("\nLoading extensions...");for(var p=0;p<t.extensions.length;++p)try{d.printMsg(t.extensions[p]);var u=require(t.extensions[p]);r.addExtension(u,t.extensions[p]),d.printMsg(t.extensions[p]+" loaded...")}catch(o){d.printError("Could not load extension "+t.extensions[p]+". Reason:"),d.errorExit(o)}}if(d.printMsg("..."),d.printMsg("Reading data from "+s+"..."),"stdin"==s)try{n=readFromStdIn(t.encoding)}catch(o){return void d.errorExit(o)}else try{n=readFromFile(t.input,t.encoding)}catch(o){return void d.errorExit(o)}if(d.printMsg("Parsing markdown..."),o=r.makeHtml(n),d.printMsg("Writing data to "+a+"..."),"stdout"==a)try{writeToStdOut(o)}catch(o){return void d.errorExit(o)}else try{writeToFile(o,t.output,t.append)}catch(o){return void d.errorExit(o)}d.okExit()}}showdown=fs.existsSync("../dist/showdown.js")?require("../dist/showdown"):fs.existsSync("../../.build/showdown.js")?require("../../.build/showdown.js"):require("../../dist/showdown"),program.name("showdown").description("CLI to Showdownjs markdown parser v"+version).version(version).usage("<command> [options]").option("-q, --quiet","Quiet mode. Only print errors").option("-m, --mute","Mute mode. Does not print anything"),program.command("makehtml").description("Converts markdown into html").addHelpText("after","\n\nExamples:").addHelpText("after"," showdown makehtml -i