Permalink
Browse files

Merge pull request #6 from SpotterRF/modularize

Can now load select languages (rather than all)
  • Loading branch information...
2 parents 907a91c + acd89c1 commit edccb418f17a0c719179d3fbcdfa735f231a9cd8 @andris9 committed Jan 22, 2012
View
@@ -0,0 +1,113 @@
+#!/usr/bin/env node
+(function () {
+ "use strict";
+
+ var fs = require('fs')
+ , Highlight = require('highlight')
+ //, Highlight = require('../lib/highlight')
+ , filename = process.argv[2]
+ , langArgIndex = 4
+ , langArg
+ , langs
+ ;
+
+ if (filename.match(/--languages(=)?/)) {
+ langArg = filename;
+ langArgIndex = 3;
+ filename = null;
+ }
+
+ langArg = process.argv[langArgIndex - 1] || '';
+
+ if (langArg = langArg.match(/--languages(=(.*))?/)) {
+ langs = (process.argv[langArgIndex]||'').split(/\s*,\s*/g);
+
+ if (langArg[2]) {
+ langs = langArg[2].split(/,/);
+ }
+ }
+
+ function printUsage() {
+ console.warn("Usages:");
+ console.warn("highlight site/docs/index.html > highlighted.html");
+ console.warn("cat site/docs/index.html | highlight > highlighted.html");
+ }
+
+ function handleInput(err, text) {
+ var hlBlock
+ , wrappedInHtml
+ ;
+
+ if (err) {
+ printUsage();
+ return;
+ }
+
+ wrappedInHtml = !!text.match(/<.*?code.*?>/i);
+
+ // TODO test if filename extension reveals code type
+ Highlight.init(function (err) {
+ if (err) {
+ console.error('[highlight-cli]', err.message);
+ //console.error(err.stack);
+ return;
+ }
+
+ hlBlock = Highlight.highlight(text, ' ', wrappedInHtml);
+ console.info(hlBlock);
+ }, langs);
+ }
+
+ readInput(handleInput, filename);
+
+ //
+ // this could (and probably should) be its own module
+ //
+ function readInput(cb, filename) {
+
+ function readFile() {
+ fs.readFile(filename, 'utf8', function (err, text) {
+ if (err) {
+ console.error("[ERROR] couldn't read from '" + filename + "':");
+ console.error(err.message);
+ return;
+ }
+
+ cb(err, text);
+ });
+ }
+
+ function readStdin() {
+ var text = ''
+ , timeoutToken
+ , stdin = process.stdin
+ ;
+
+ stdin.resume();
+
+ // how to tell piping vs waiting for user input?
+ timeoutToken = setTimeout(function () {
+ cb(new Error('no stdin data'));
+ stdin.pause();
+ }, 1000);
+
+ stdin.on('data', function (chunk) {
+ clearTimeout(timeoutToken);
+ text += chunk;
+ });
+
+ stdin.on('end', function () {
+ cb(null, text);
+ });
+ }
+
+ if (filename) {
+ readFile();
+ }
+ else {
+ readStdin();
+ }
+
+ }
+
+}());
View
@@ -0,0 +1,34 @@
+{
+ "name" : "highlight-cli",
+ "description" : "cli for the highlight module",
+ "version" : "1.1.0",
+ "author" : "AJ ONeal",
+ "homepage": "https://github.com/andris9/highlight",
+ "maintainers":[
+ {
+ "name":"andris",
+ "email":"andris@node.ee"
+ }
+ ],
+ "engines": {
+ "node": "*"
+ },
+ "repository" : {
+ "type" : "git",
+ "url" : "http://github.com/andris9/highlight.git"
+ },
+ "bin": {
+ "highlight": "./highlight-cli.js"
+ },
+ "main": "./highlight-cli",
+ "dependencies": {
+ "highlight": "*"
+ },
+ "licenses" : [
+ {
+ "type": "BSD",
+ "url": "http://github.com/andris9/highlight/blob/master/LICENSE"
+ }
+ ],
+ "preferGlobal": true
+}
View
@@ -0,0 +1,16 @@
+<!DOCTYPE html>
+<html>
+ <head>
+ <title>Foo</title>
+ </head>
+ <body>
+ Bar
+ <br/>
+ Baz
+ <p>Quux</p>
+ <ul>
+ <li>Qux</li>
+ <li>Grault</li>
+ </ul>
+ </body>
+</html>
View
@@ -0,0 +1,5 @@
+(function () {
+ "use strict";
+
+ console.log('Hello World!');
+}();
@@ -0,0 +1,5 @@
+<pre><code>(function () {
+ "use strict";
+
+ console.log('Hello World!');
+}();</code></pre>
@@ -0,0 +1,29 @@
+(function () {
+ "use strict";
+
+ var Highlight = require("../lib/highlight.js")
+ , assert = require('assert')
+ , fs = require('fs')
+ , reHasAnnotations = /\sclass="tag"/
+ ;
+
+ function runTest(err) {
+
+ assert.ok(!err, err && err.message);
+ assert.strictEqual(1, Highlight.loadedLanguages.length, 'more than one language is loaded: ' + Highlight.loadedLanguages);
+ assert.strictEqual('xml', Highlight.loadedLanguages[0], 'xml isn\'t the language');
+
+ fs.readFile('./example.html', 'utf8', function (err, text) {
+ var annotated
+ ;
+
+ assert.ok(!err, 'threw error reading example.html');
+ annotated = Highlight.highlight(text, ' ');
+ assert.ok(annotated.match(reHasAnnotations));
+ //console.log(annotated);
+ console.info('[PASS] source is annotated');
+ });
+ }
+
+ Highlight.init(runTest, ['xml']);
+}());
@@ -0,0 +1,41 @@
+(function () {
+ "use strict";
+
+ var Highlight = require("../lib/highlight.js")
+ , assert = require('assert')
+ , fs = require('fs')
+ , reHasAnnotations = /\sclass="[\w-]+"/
+ ;
+
+ function runTest(err) {
+
+ //console.log(Highlight.loadedLanguages);
+ assert.ok(!err, err && err.message);
+ assert.strictEqual(Highlight.languages.length, Highlight.loadedLanguages.length
+ , 'not all languages were loaded: '
+ + Highlight.languages.length
+ + " "
+ + Highlight.loadedLanguages.length
+ );
+ assert.deepEqual(Highlight.languages, Highlight.loadedLanguages
+ , 'not all languages were loaded: '
+ + JSON.stringify(Highlight.languages, null, ' ')
+ + "\n"
+ + JSON.stringify(Highlight.loadedLanguages, null, ' ')
+ );
+
+ // It's okay that these run out-of-order / in-parallel
+ fs.readFile('./example.js', 'utf8', function (err, text) {
+ var annotated
+ ;
+
+ assert.ok(!err, 'threw error reading example.js');
+ annotated = Highlight.highlight(text, ' ');
+ assert.ok(annotated.match(reHasAnnotations));
+ //console.log(annotated);
+ console.info('[PASS] annotated source (perhaps incorrectly) with all modules loaded');
+ });
+ }
+
+ Highlight.init(runTest);
+}());
@@ -0,0 +1,29 @@
+(function () {
+ "use strict";
+
+ var Highlight = require("../lib/highlight.js")
+ , assert = require('assert')
+ , fs = require('fs')
+ , reHasAnnotations = /\sclass="[\w-]+"/
+ ;
+
+ function runTest(err) {
+
+ assert.ok(!err, err && err.message);
+ assert.strictEqual(1, Highlight.loadedLanguages.length, 'more than one language is loaded: ' + Highlight.loadedLanguages);
+ assert.strictEqual('javascript', Highlight.loadedLanguages[0], 'javascript is the language');
+
+ fs.readFile('./example.js.html', 'utf8', function (err, text) {
+ var annotated
+ ;
+
+ assert.ok(!err, 'threw error reading example.js.html');
+ annotated = Highlight.highlight(text, ' ', true);
+ assert.ok(annotated.match(reHasAnnotations));
+ //console.log(annotated);
+ console.info('[PASS] source is annotated');
+ });
+ }
+
+ Highlight.init(runTest, ['javascript']);
+}());
@@ -0,0 +1,36 @@
+(function () {
+ "use strict";
+
+ var Highlight = require("../lib/highlight.js")
+ , assert = require('assert')
+ , fs = require('fs')
+ , reHasMarkup = /<.*class=["']?[\w-]+["'?]/
+ , reHasAnnotations = /\sclass="[\w-]+"/
+ ;
+
+ function runTest(err) {
+
+ //console.log(Highlight.loadedLanguages);
+ assert.ok(!err, err && err.message);
+ assert.strictEqual(0, Highlight.loadedLanguages.length
+ , 'some languages were loaded: '
+ + Highlight.languages.length
+ + " "
+ + Highlight.loadedLanguages.length
+ );
+
+ // It's okay that these run out-of-order / in-parallel
+ fs.readFile('./example.js', 'utf8', function (err, text) {
+ var annotated
+ ;
+
+ assert.ok(!err, 'threw error reading example.js');
+ annotated = Highlight.highlight(text, ' ');
+ assert.ok(!annotated.match(reHasAnnotations));
+ //console.log(annotated);
+ console.info('[PASS] source is not annotated');
+ });
+ }
+
+ Highlight.init(runTest, []);
+}());
Oops, something went wrong.

0 comments on commit edccb41

Please sign in to comment.