Permalink
Browse files

ohl2bemjson util, OmetaHighlighterToBemjson serializator added, tests…

… updated
  • Loading branch information...
1 parent e7957b2 commit dad244fd1cfb73a611b354645a9de7e3d604c0a6 @arikon arikon committed Aug 15, 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/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
.PHONY: all FORCE
View
@@ -0,0 +1,3 @@
+#!/usr/bin/env node
+
+require('../lib/2bemjson').main();
View
@@ -0,0 +1,82 @@
+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: ohl2bemjson [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',
+ ' -f, --format : specifies code format, valid values are "js" and "xml", if omit use "js"',
+ ' -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;
+ case '-f':
+ case '--format':
+ options.format = args.shift();
+ if (['javascript', 'js', 'xml'].indexOf(options.format) == -1) {
+ sys.error('Wrong format "' + options.format + '" specified, must be one of "js" or "xml"');
+ process.exit(1);
+ }
+ 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 = JSON.stringify(ohl.OmetaHighlighterToBemjson.match(
+ ohl.OmetaHighlighter.matchAll(input, options.format || 'js'),
+ 'topLevel'
+ ), null, 4) + '\n';
+ //result = JSON.stringify(ohl.OmetaHighlighter.matchAll(input, options.format || 'js'), null, 4) + '\n';
+ 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

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

Oops, something went wrong.
View
@@ -10,7 +10,7 @@
},
"main" : "./lib/ometa-highlighter",
"directories" : { "lib" : "./lib" },
- "bin" : { "ohl2html" : "./bin/ohl2html" },
+ "bin" : { "ohl2html" : "./bin/ohl2html", "ohl2bemjson" : "./bin/ohl2bemjson" },
"dependencies" : {
"ometajs" : ">= 2.0.1"
},

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

Oops, something went wrong.
@@ -0,0 +1,45 @@
+ometa OmetaHighlighterToBemjson {
+ token = char+:c -> OmetaHighlighterToBemjson._escape(c.join(''))
+ | [:t ?(t.length > 1) tokens:c] -> { tag: 'span', attrs: { 'class': OmetaHighlighterToBemjson._makeClass(t) }, content: c }
+ | :c -> OmetaHighlighterToBemjson._escape(c),
+ tokens = [token*:c] -> c,
+
+ topLevel = [:lang tokens:c] -> { block: 'b-code', mods: { 'lang': lang }, content: c }
+}
+
+OmetaHighlighterToBemjson._escape = (function(){
+ var amp = new RegExp('&', 'g'),
+ lt = new RegExp('<', 'g'),
+ gt = new RegExp('>', 'g'),
+ apos = new RegExp("'", 'g'),
+ quot = new RegExp('"', 'g');
+
+ return function(s) {
+ return String(s)
+ .replace(amp, '&amp;')
+ .replace(lt, '&lt;')
+ .replace(gt, '&gt;')
+ .replace(apos, '&apos;')
+ .replace(quot, '&quot;');
+ }
+})();
+
+OmetaHighlighterToBemjson._makeClass = (function(){
+ var type2class = {};
+
+ return function(type) {
+ var types = [],
+ typeParts = type.split('.');
+ while (typeParts.length) {
+ types.unshift(typeParts.join('-'));
+ typeParts.pop();
+ }
+
+ var typeClass = '';
+ while (types.length) {
+ var t = types.shift();
+ typeClass += ' ohl-' + (type2class.hasOwnProperty(t) ? type2class[t] : t);
+ }
+ return typeClass.slice(1);
+ }
+})();

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

Oops, something went wrong.
Oops, something went wrong.

0 comments on commit dad244f

Please sign in to comment.