Skip to content

Commit

Permalink
Added ohl util, removed ohl2html and ohl2bemjson utils, bemjson block…
Browse files Browse the repository at this point in the history
… renamed to "ohl"
  • Loading branch information
arikon committed Aug 31, 2011
1 parent 20fb530 commit 2ec333b
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 189 deletions.
22 changes: 11 additions & 11 deletions GNUmakefile
Expand Up @@ -20,16 +20,16 @@ lib/ometa-highlighter.js: src
; done

tests: FORCE
./bin/ohl2html -i $@/bla.js -o $@/bla.html -f js
cat $@/bla.js | ./bin/ohl2html -f js > $@/bla.html.stdout
./bin/ohl2html -i $@/test.xml -o $@/test.html -f xml
cat $@/test.xml | ./bin/ohl2html -f xml > $@/test.html.stdout
./bin/ohl2bemjson -i $@/bla.js -o $@/bla.bemjson.js -f js
cat $@/bla.js | ./bin/ohl2bemjson -f js > $@/bla.bemjson.js.stdout
./bin/ohl2bemjson -i $@/test.xml -o $@/test.bemjson.js -f xml
cat $@/test.xml | ./bin/ohl2bemjson -f xml > $@/test.bemjson.js.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
3 changes: 3 additions & 0 deletions bin/ohl
@@ -0,0 +1,3 @@
#!/usr/bin/env node

require('../lib/cli').main();
3 changes: 0 additions & 3 deletions bin/ohl2bemjson

This file was deleted.

3 changes: 0 additions & 3 deletions bin/ohl2html

This file was deleted.

82 changes: 0 additions & 82 deletions lib/2bemjson.js

This file was deleted.

81 changes: 0 additions & 81 deletions lib/2html.js

This file was deleted.

103 changes: 103 additions & 0 deletions lib/cli.js
@@ -0,0 +1,103 @@
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')
.req()
.val(function(value) {
(['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) {
(['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) {
(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));
};
2 changes: 1 addition & 1 deletion lib/ometa-highlighter.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -10,10 +10,10 @@
},
"main" : "./lib/ometa-highlighter",
"directories" : { "lib" : "./lib" },
"bin" : { "ohl2html" : "./bin/ohl2html", "ohl2bemjson" : "./bin/ohl2bemjson" },
"bin" : { "ohl" : "./bin/ohl" },
"dependencies" : {
"ometajs" : ">= 2.0.1"
},
"engines" : { "node" : ">=0.2.0" },
"engines" : { "node" : ">=0.4.0" },
"licenses" : [ { "type" : "AS IS" } ]
}
2 changes: 1 addition & 1 deletion src/ometa-highlighter2bemjson.ometajs
Expand Up @@ -4,7 +4,7 @@ ometa OmetaHighlighterToBemjson {
| :c -> OmetaHighlighterToBemjson._escape(c),
tokens = [token*:c] -> c,

topLevel = [:lang tokens:c] -> { block: 'b-code', mods: { 'lang': lang }, content: c }
topLevel = [:lang tokens:c] -> { block: 'ohl', mods: { 'lang': lang }, content: c }
}

OmetaHighlighterToBemjson._escape = (function(){
Expand Down
2 changes: 1 addition & 1 deletion src/ometa-highlighter2bemjson.ometajs.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion tests/bla.bemjson.js
@@ -1,5 +1,5 @@
{
"block": "b-code",
"block": "ohl",
"mods": {
"lang": "js"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/bla.bemjson.js.stdout
@@ -1,5 +1,5 @@
{
"block": "b-code",
"block": "ohl",
"mods": {
"lang": "js"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test.bemjson.js
@@ -1,5 +1,5 @@
{
"block": "b-code",
"block": "ohl",
"mods": {
"lang": "xml"
},
Expand Down
2 changes: 1 addition & 1 deletion tests/test.bemjson.js.stdout
@@ -1,5 +1,5 @@
{
"block": "b-code",
"block": "ohl",
"mods": {
"lang": "xml"
},
Expand Down

0 comments on commit 2ec333b

Please sign in to comment.