Permalink
Browse files

Merge pull request #7 from arikon/nodejs

Refactored to use coa 0.1.1 API
  • Loading branch information...
2 parents 133c964 + 48173cb commit 605c021aa07dfa2a4105f717ab8354c3722e9394 @veged committed Sep 30, 2011
Showing with 63 additions and 54 deletions.
  1. +57 −48 lib/cli.js
  2. +2 −2 package.json
  3. +1 −1 tests/bla.html
  4. +1 −1 tests/bla.html.stdout
  5. +1 −1 tests/test.html
  6. +1 −1 tests/test.html.stdout
View
@@ -12,34 +12,40 @@ exports.main = function () {
.title('Show version')
.long('version')
.flag()
+ .only()
.act(function(opts) {
- this.exit(
- JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'))
- .version);
+ return JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'))
+ .version;
})
.end()
.opt()
.name('input')
.title('Input file. Defaults to stdin')
.short('i')
.long('input')
+ .input()
.end()
.opt()
.name('output')
.title('Output file. Defaults to stdout')
.short('o')
.long('output')
+ .output()
.end()
.opt()
.name('lang')
.title('Language: js, xml')
.short('l')
.long('lang')
+ .req()
.val(function(value) {
// FIXME: don't use private API _usage()
- !value && this.end().errorExit("Missing required option value\n" + this._usage());
- (['js', 'xml'].indexOf(value) == -1) && this.end()
- .errorExit('Wrong language "' + value + '" specified, must be one of "js" or "xml"');
+ if (!value) {
+ return this.reject("Missing required option value\n" + this._usage());
+ }
+ if (['js', 'xml'].indexOf(value) == -1) {
+ return this.reject('Wrong language "' + value + '" specified, must be one of "js" or "xml"');
+ }
return value;
})
.end()
@@ -51,9 +57,12 @@ exports.main = function () {
.def('html')
.val(function(value) {
// FIXME: don't use private API _usage()
- !value && this.end().errorExit("Missing required option value\n" + this._usage());
- (['html', 'bemjson'].indexOf(value) == -1) && this.end()
- .errorExit('Wrong output format "' + value + '" specified, must be one of "html" or "bemjson"');
+ if (!value) {
+ return this.reject("Missing required option value\n" + this._usage());
+ }
+ if (['html', 'bemjson'].indexOf(value) == -1) {
+ return this.reject('Wrong output format "' + value + '" specified, must be one of "html" or "bemjson"');
+ }
return value;
})
.end()
@@ -65,45 +74,45 @@ exports.main = function () {
.flag()
.end()
.act(function(opts) {
- // FIXME: don't use private API _optsByKey
- !opts.lang && (this.errorExit(this._optsByKey['--lang']._requiredText()));
- (opts.input ?
- // if input file
- function(inputFn) {
- fs.readFile(opts.input, 'utf8', function(err, input){
- if (err) throw err;
- inputFn(input);
- });
- } :
- // if STDIN
- function(inputFn) {
- var input = '';
- process.openStdin()
- .on('data', function(s) { input += s })
- .on('end', function() { inputFn(input) });
- })(function(input){
- try {
- var ohl = require('./ometa-highlighter'),
- ast = ohl.OmetaHighlighter.matchAll(input, opts.lang),
- result = (opts.format == 'html' ?
- ohl.OmetaHighlighterToHtml.match(ast, 'topLevel') :
- JSON.stringify(ohl.OmetaHighlighterToBemjson.match(ast, 'topLevel'), null, 4) + '\n');
- opts.output ?
- fs.writeFile(opts.output, result, 'utf8', function(err) {
- if (err) throw err;
- opts.verbose && sys.error(' create : ' + opts.output);
- }) :
- process.stdout.write(result);
- } catch (e) {
- e.errorPos != undefined &&
- sys.error(
- input.slice(0, e.errorPos) +
- "\n--- Parse error ->" +
- input.slice(e.errorPos) + '\n');
- throw e;
- }
- });
+ readStream(opts.input, function(err, input) {
+ if (err) throw err;
+
+ // remove BOM if present
+ if (input.charCodeAt(0) === 0xFEFF) input = input.slice(1);
+
+ try {
+ var ohl = require('./ometa-highlighter'),
+ ast = ohl.OmetaHighlighter.matchAll(input, opts.lang),
+ result = (opts.format == 'html' ?
+ ohl.OmetaHighlighterToHtml.match(ast, 'topLevel') :
+ JSON.stringify(ohl.OmetaHighlighterToBemjson.match(ast, 'topLevel'), null, 4)) + '\n';
+ opts.output.end(result);
+ } catch (e) {
+ e.errorPos != undefined &&
+ sys.error(
+ input.slice(0, e.errorPos) +
+ "\n--- Parse error ->" +
+ input.slice(e.errorPos) + '\n');
+ throw e;
+ }
+ });
+ opts.input.resume();
+
})
- .parse(process.argv.slice(2));
+ .run();
};
+
+function readStream(stream, callback) {
+ var data = '';
+ stream
+ .on('data', function(chunk) {
+ data += chunk;
+ })
+ .on('error', function(err) {
+ callback(err);
+ })
+ .on('end', function() {
+ callback(null, data);
+ });
+}
View
@@ -1,7 +1,7 @@
{
"name" : "ometa-highlighter",
"description" : "Code highlighter based on Ometa/JS (little inspired by Pygments)",
- "version" : "0.2.0",
+ "version" : "0.2.2",
"homepage": "http://github.com/veged/ometa-highlighter",
"author" : "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)",
"repository" : {
@@ -13,7 +13,7 @@
"bin" : { "ohl" : "./bin/ohl" },
"dependencies" : {
"ometajs" : ">= 2.0.1",
- "coa" : ">= 0.0.5"
+ "coa" : ">= 0.1.1"
},
"engines" : { "node" : ">=0.4.0" },
"licenses" : [ { "type" : "AS IS" } ]
View
@@ -2,4 +2,4 @@
<span class="ohl-keyword ohl-keyword-declaration">var</span> <span class="ohl-name">b</span> <span class="ohl-operator">=</span> <span class="ohl-name">a</span> <span class="ohl-operator">+</span> <span class="ohl-string ohl-string-single"><span class="ohl-punctuation">&apos;</span>c<span class="ohl-punctuation">&apos;</span></span><span class="ohl-punctuation">;</span>
<span class="ohl-keyword">return</span> <span class="ohl-name">b</span><span class="ohl-punctuation">;</span>
<span class="ohl-punctuation">}</span>
-</code></pre>
+</code></pre>
View
@@ -2,4 +2,4 @@
<span class="ohl-keyword ohl-keyword-declaration">var</span> <span class="ohl-name">b</span> <span class="ohl-operator">=</span> <span class="ohl-name">a</span> <span class="ohl-operator">+</span> <span class="ohl-string ohl-string-single"><span class="ohl-punctuation">&apos;</span>c<span class="ohl-punctuation">&apos;</span></span><span class="ohl-punctuation">;</span>
<span class="ohl-keyword">return</span> <span class="ohl-name">b</span><span class="ohl-punctuation">;</span>
<span class="ohl-punctuation">}</span>
-</code></pre>
+</code></pre>
View
@@ -10,4 +10,4 @@
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>branch<span class="ohl-punctuation">&gt;</span></span>
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>tree<span class="ohl-punctuation">&gt;</span></span>
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>root<span class="ohl-punctuation">&gt;</span></span>
-</code></pre>
+</code></pre>
View
@@ -10,4 +10,4 @@
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>branch<span class="ohl-punctuation">&gt;</span></span>
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>tree<span class="ohl-punctuation">&gt;</span></span>
<span class="ohl-name ohl-name-tag"><span class="ohl-punctuation">&lt;/</span>root<span class="ohl-punctuation">&gt;</span></span>
-</code></pre>
+</code></pre>

0 comments on commit 605c021

Please sign in to comment.