Permalink
Browse files

Many fixes for NodeJS compatible

  • Loading branch information...
veged committed Sep 11, 2010
1 parent b6769e1 commit 934f3e738af61630f0cb98183690d9fc60da5512
View
@@ -0,0 +1,25 @@
+
+all: lib
+
+src: $(patsubst %.ometajs,%.ometajs.js,$(wildcard src/*.ometajs))
+
+%.ometajs.js: %.ometajs
+ ometajs2js -i $< -o $@
+
+lib: lib/ometa-highlighter.js
+
+lib/ometa-highlighter.js: src
+ -rm $@
+ for i in \
+ ometa-highlighter.js \
+ ometa-highlighter.ometajs.js \
+ ometa-highlighter2html.ometajs.js \
+ ; do \
+ cat $</$$i >> $@ \
+ ; done
+
+tests: FORCE
+ ohl2html -i $@/bla.js -o $@/bla.html
+ cat $@/bla.js | ohl2html > $@/bla.html.stdout
+
+.PHONY: all FORCE
View
@@ -1,6 +0,0 @@
-ometajs:
- $(MAKE) -C src
- -rm lib/ometa-highlighter.js
- cat src/ometa-highlighter.js >> lib/ometa-highlighter.js
- cat src/ometa-highlighter.ometajs.js >> lib/ometa-highlighter.js
- cat src/ometa-highlighter2html.ometajs.js >> lib/ometa-highlighter.js
@@ -1,3 +1,3 @@
-#!/usr/bin/env narwhal
+#!/usr/bin/env node
require('ometa-highlighter/2html').main();
View
@@ -0,0 +1,72 @@
+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

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -1,25 +0,0 @@
-var file = require('file'),
- parser = exports.parser = new (require('args').Parser)();
-
-parser.option('-i', '--input', 'inputFile')
- .help('specifies filename to read the input source, if omit use STDIN')
- .set();
-
-parser.option('-o', '--output', 'outFile')
- .help('specifies filename to write the output, if omit use STDOUT')
- .set();
-
-parser.helpful();
-
-exports.main = function () {
- var options = parser.parse(system.args),
- input = options.inputFile ? file.read(options.inputFile) : system.stdin.read();
-
- var m = require('ometa-highlighter'),
- result = m.OmetaHighlighterToHtml.match(
- m.OmetaHighlighter.matchAll(input, 'js'), // TODO: use format from options
- 'topLevel'
- );
-
- options.outFile ? file.write(options.outFile, result) : system.stdout.write(result);
-};
View
@@ -1,15 +1,15 @@
{
- "name": "ometa-highlighter",
- "description": "Code highlighter based on Ometa/JS (little inspired by Pygments)",
- "version": "0.1.1",
- "homepage" : "http://github.com/veged/ometa-highlighter.git",
- "author": "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)",
+ "name" : "ometa-highlighter",
+ "description" : "Code highlighter based on Ometa/JS (little inspired by Pygments)",
+ "version" : "0.1.1",
+ "homepage": "http://github.com/veged/ometa-highlighter.git",
+ "author" : "Sergey Berezhnoy <veged@mail.ru> (http://github.com/veged)",
"repository" : {
"type" : "git",
"url" : "http://github.com/veged/ometa-highlighter.git"
},
- "directories" : { "lib" : "./lib" }
- "bin" : { "ometa-highlighter2html" : "./bin/ometa-highlighter2html" }
+ "directories" : { "lib" : "./lib" },
+ "bin" : { "ohl2html" : "./bin/ohl2html" },
"dependencies" : {
"ometajs" : ">= 2.0.1"
},
View
@@ -1,5 +0,0 @@
-
-all: $(patsubst %.ometajs,%.ometajs.js,$(wildcard *.ometajs))
-
-%.ometajs.js: %.ometajs
- ometajs2js -i $? -o $@
@@ -94,7 +94,7 @@ ometa OmetaHighlighterXml <: Parser {
attr = name:n spaces:s1 '=':p spaces:s2 string:s -> ['name.attribute', [n[1]].concat(s1).concat([[`punctuation, p]]).concat(s2).concat([s])],
tag = '<':p1 spaces:s1 name:n
- (spaces:s2 attr:a spaces:s3 -> s2.concat([a]).concat(s3))*:as -> Array.prototype.concat.apply([], as):as
+ ((spaces:s2 attr:a spaces:s3 -> s2.concat([a]).concat(s3))*:as -> Array.prototype.concat.apply([], as)):as
spaces:s4 ('>' | ``/>''):p2
-> ['name.tag', [[`punctuation, p1]].concat(s1).concat(n[1]).concat(as).concat(s4).concat([[`punctuation, p2]])]
| '<':p1 spaces:s1 '/':p2 name:n spaces:s2 '>':p3 -> ['name.tag', [[`punctuation, p1 + p2]].concat(s1).concat(n[1]).concat(s2).concat([[`punctuation, p3]])],

Large diffs are not rendered by default.

Oops, something went wrong.

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

Oops, something went wrong.
View
@@ -0,0 +1,5 @@
+<pre class="ohl ohl-js"><code><span class="ohl-keyword ohl-keyword-declaration">function</span><span class="ohl-punctuation">(</span><span class="ohl-name">a</span><span class="ohl-punctuation">)</span> <span class="ohl-punctuation">{</span>
+ <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>
View
@@ -0,0 +1,5 @@
+<pre class="ohl ohl-js"><code><span class="ohl-keyword ohl-keyword-declaration">function</span><span class="ohl-punctuation">(</span><span class="ohl-name">a</span><span class="ohl-punctuation">)</span> <span class="ohl-punctuation">{</span>
+ <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>
View
@@ -0,0 +1,4 @@
+function(a) {
+ var b = a + 'c';
+ return b;
+}

0 comments on commit 934f3e7

Please sign in to comment.