Permalink
Browse files

Merge pull request #5 from arikon/nodejs

Translator OmetaHighlighterToBemjson
  • Loading branch information...
2 parents ca577b8 + 9be2450 commit 9a3eeed012d60004b308435e2135db2d752c5257 @veged committed Sep 1, 2011
View
@@ -14,12 +14,22 @@ lib/ometa-highlighter.js: src
ometa-highlighter.js \
ometa-highlighter.ometajs.js \
ometa-highlighter2html.ometajs.js \
+ ometa-highlighter2bemjson.ometajs.js \
; do \
cat $</$$i >> $@ \
; done
tests: FORCE
- ./bin/ohl2html -i $@/bla.js -o $@/bla.html
- cat $@/bla.js | ./bin/ohl2html > $@/bla.html.stdout
+ ./bin/ohl -i $@/bla.js -o $@/bla.html -l js -f html
+ cat $@/bla.js | ./bin/ohl -l js -f html > $@/bla.html.stdout
+
+ ./bin/ohl -i $@/test.xml -o $@/test.html -l xml -f html
+ cat $@/test.xml | ./bin/ohl -l xml -f html > $@/test.html.stdout
+
+ ./bin/ohl -i $@/bla.js -o $@/bla.bemjson.js -l js -f bemjson
+ cat $@/bla.js | ./bin/ohl -l js -f bemjson > $@/bla.bemjson.js.stdout
+
+ ./bin/ohl -i $@/test.xml -o $@/test.bemjson.js -l xml -f bemjson
+ cat $@/test.xml | ./bin/ohl -l xml -f bemjson > $@/test.bemjson.js.stdout
.PHONY: all FORCE
View
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../lib/cli').main();
View
@@ -1,3 +0,0 @@
-#!/usr/bin/env node
-
-require('../lib/2html').main();
View
@@ -1,72 +0,0 @@
-exports.main = function() {
-
- var sys = require('sys'),
- fs = require('fs'),
- args = process.argv.slice(2),
- arg,
- options = {};
-
- while(args.length) {
- arg = args.shift();
- switch (arg) {
- case '-h':
- case '--help':
- sys.puts([
- 'Usage: ohl2html [options]',
- '',
- 'Options:',
- ' -i, --input : pecifies filename to read the input source, if omit use STDIN',
- ' -o, --output : specifies filename to write the output, if omit use STDOUT',
- ' -h, --help : Output help information'
- ].join('\n'));
- process.exit(1);
- break;
- case '-i':
- case '--input':
- options.input = args.shift();
- break;
- case '-o':
- case '--output':
- options.output = args.shift();
- break;
- }
- }
-
- (options.input ?
- // if input file
- function(inputFn) {
- fs.readFile(options.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'),
- result = ohl.OmetaHighlighterToHtml.match(
- ohl.OmetaHighlighter.matchAll(input, 'js'), // TODO: use format from options
- 'topLevel'
- );
- options.output ?
- fs.writeFile(options.output, result, 'utf8', function(err) {
- if (err) throw err;
- sys.error(' create : ' + options.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
- }
- });
-
-};
View
@@ -0,0 +1,109 @@
+exports.main = function () {
+
+ var sys = require('sys'),
+ fs = require('fs');
+
+ require('coa').Cmd()
+ .name(process.argv[1])
+ .title('OMeta Highlighter command line utility')
+ .helpful()
+ .opt()
+ .name('version')
+ .title('Show version')
+ .long('version')
+ .flag()
+ .act(function(opts) {
+ this.exit(
+ JSON.parse(require('fs').readFileSync(__dirname + '/../package.json'))
+ .version);
+ })
+ .end()
+ .opt()
+ .name('input')
+ .title('Input file. Defaults to stdin')
+ .short('i')
+ .long('input')
+ .end()
+ .opt()
+ .name('output')
+ .title('Output file. Defaults to stdout')
+ .short('o')
+ .long('output')
+ .end()
+ .opt()
+ .name('lang')
+ .title('Language: js, xml')
+ .short('l')
+ .long('lang')
+ .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"');
+ return value;
+ })
+ .end()
+ .opt()
+ .name('format')
+ .title('Output format: html, bemjson. Defaults to html')
+ .short('f')
+ .long('format')
+ .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"');
+ return value;
+ })
+ .end()
+ .opt()
+ .name('verbose')
+ .title('Show verbose output')
+ .long('verbose')
+ .short('v')
+ .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;
+ }
+ });
+ })
+ .parse(process.argv.slice(2));
+};
View

Some generated files are not rendered by default. Learn more.

Oops, something went wrong.
View
@@ -1,7 +1,7 @@
{
"name" : "ometa-highlighter",
"description" : "Code highlighter based on Ometa/JS (little inspired by Pygments)",
- "version" : "0.1.5",
+ "version" : "0.2.0",
"homepage": "http://github.com/veged/ometa-highlighter",
"author" : "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)",
"repository" : {
@@ -10,10 +10,10 @@
},
"main" : "./lib/ometa-highlighter",
"directories" : { "lib" : "./lib" },
- "bin" : { "ohl2html" : "./bin/ohl2html" },
+ "bin" : { "ohl" : "./bin/ohl" },
"dependencies" : {
"ometajs" : ">= 2.0.1"
},
- "engines" : { "node" : ">=0.2.0" },
+ "engines" : { "node" : ">=0.4.0" },
"licenses" : [ { "type" : "AS IS" } ]
}
@@ -41,18 +41,22 @@ ometa OmetaHighlighterJs {
| ``instanceof'' | ``void'' | ``this''):c -> [`keyword, c],
+ // TODO: unicode char, octal char
escapeChar = '\\':e char:c -> ['string.escape', [e, c]],
nonEscapeChar :q = ~seq(q) char,
quote :q = seq(q):c -> [`punctuation, c],
anyString :q :n = quote(q):qp
(escapeChar | nonEscapeChar(q))*:cs
quote(q) -> { cs.unshift(qp); cs.push(qp); [`string + '.' + n, cs] },
+ // TODO: modifiers (ff y or any)
+ // https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions#Advanced_Searching_With_Flags
string = anyString('\'', `single)
| anyString('"', `double)
| (anyString('/', `regex):r (`g | `i | `m)*:f -> { r[1] = r[1].concat(f); r }),
+ // TODO: exponent
integer = digit+:c -> ['number.integer', c],
float = digit*:ws '.' digit*:fs ?(ws.length + fs.length != 0) -> ['number.float', ws.concat([[`punctuation, '.']]).concat(fs)],
hex = '0' 'x' (digit | `a | `b | `c | `d | `e | `f)*:c -> ['number.hex', ['0'].concat([[`punctuation, 'x']]).concat(c)],
@@ -85,7 +89,7 @@ ometa OmetaHighlighterXml {
(~seq(q) char)*:cs
quote(q) -> { cs.unshift(qp); cs.push(qp); [`string + '.' + n, cs] },
- preproc = (fromTo('<?', '?>') | fromTo('<![CDATA[', ']]>') | fromTo('<![CDATA[', ']]>')):c
+ preproc = (fromTo('<?', '?>') | fromTo('<![CDATA[', ']]>')):c
-> ['comment.preproc', [[`punctuation, c[0]]].concat(c[1]).concat([[`punctuation, c[2]]])],
string = anyString('\'', `single)
Oops, something went wrong.

0 comments on commit 9a3eeed

Please sign in to comment.