Jelajahi Sumber

Merge branch 'cli' of https://github.com/rheber/showdown into feature/cli

Estevão Soares dos Santos 10 tahun lalu
induk
melakukan
ba7eb7ebaf
2 mengubah file dengan 26 tambahan dan 0 penghapusan
  1. 3 0
      package.json
  2. 23 0
      src/cli.js

+ 3 - 0
package.json

@@ -35,6 +35,9 @@
   "scripts": {
     "test": "grunt test"
   },
+  "bin": {
+    "showdown": "src/cli.js"
+  },
   "devDependencies": {
     "chai": "^1.10.0",
     "grunt": "^0.4.5",

+ 23 - 0
src/cli.js

@@ -0,0 +1,23 @@
+var Showdown = require('./showdown');
+var cli = require('cli');
+var fs = require('fs');
+
+var converter = new Showdown.converter();
+
+/*
+If an argument is given, treat it as the file to be read.
+Otherwise, read from stdin.
+*/
+if(process.argv.length > 2) {
+    fs.readFile(process.argv[2], function(err, data) {
+        if(err) {
+            console.error("Error: Invalid file");
+        } else {
+            console.log(converter.makeHtml(data.toString()));
+        }
+    })
+} else {
+    cli.withStdin(function(data) {
+        this.output(converter.makeHtml(data.toString()));
+    })
+}