Browse files

pygmentize(options,[code,callback]) interface

  • Loading branch information...
1 parent 953a826 commit 3a6fbf67fa558c14a7d3921dac32adee2a73ffea @rvagg committed Oct 18, 2012
Showing with 94 additions and 59 deletions.
  1. +30 −13 README.md
  2. +51 −31 index.js
  3. +4 −4 package.json
  4. +9 −11 test/test.js
View
43 README.md
@@ -1,18 +1,35 @@
# Pygmentize (Bundled)
-A simple wrapper around Python's Pygments code formatter, with Pygments
-bundled.
+A simple wrapper around Python's Pygments code formatter, with Pygments bundled.
-This fork of [rvagg's original code](https://github.com/rvagg/node-pygmentize-bundled) implements a streaming API so that you can syntax highlight from a stream.
+Available as a simple *String-in, Buffer-out* interface and also as a *read/write-Stream* interface.
-Similar to [pksunkara's pygments.js](https://github.com/pksunkara/pygments.js) but this comes bundled with Pygments so it doesn't need to be installed on your system, you just need to have Python.
+
+## API
+
+**pygmentize(options, code, callback)**
+
+Pygmentize a given `code` string and return it as a Buffer to the `callback` Function.
+
+* `options` contains options to be passed to Pygments. Currently only `"lang"` and `"format"` are supported.
+* `code` is a String to be formatted.
+* `callback` is a Function, called when complete. The first argument will be an `error` object/string if there was a problem and the second argument will be a Buffer containing your formatted code.
+
+**pygmentize(options)**
+
+When you only supply the `options` argument, it will return a read/write Stream that you can pipe to and from to format your code.
+
+* `options` contains options to be passed to Pygments. Currently only `"lang"` and `"format"` are supported.
+
+
+## Examples
The string interface is very simple:
```js
var pygmentize = require('pygmentize-bundled')
-pygmentize('js', 'html').fromString('var a = "b";', function (err, result) {
+pygmentize({ lang: 'js', format: 'html'}, 'var a = "b";', function (err, result) {
console.log(result.toString())
})
```
@@ -29,26 +46,26 @@ Results in:
</pre></div>
```
-A streaming API is also available:
+A streaming API is also available. Simply omit the `code` and `callback` arguments:
```js
var pygmentize = require('pygmentize-bundled')
process.stdin.pipe(
- pygmentize('js', 'html').fromStream()
+ pygmentize({ lang: 'js', format: 'html' })
).pipe(process.stdout);
```
-## API
+Refer to the [Pygments documentation](http://pygments.org/docs/). For supported languages, see the list of [lexers](http://pygments.org/docs/lexers/), for supported formatted, see the list of [formatters](http://pygments.org/docs/formatters/).
-**pygmentize(lang, format).fromString(code, callback)**
-**pygmentize(lang, format).fromStream()**
+## Contributors
+
+* [Rod Vagg](https://github.com/rvagg)
+* [Cyril Rohr](https://github.com/crohr)
-Refer to the [Pygments documentation](http://pygments.org/docs/). For supported languages, see the list of [lexers](http://pygments.org/docs/lexers/), for supported formatted, see the list of [formatters](http://pygments.org/docs/formatters/).
-Licence & copyright
--------------------
+## Licence & copyright
Pygments (Bundled) is Copyright (c) 2012 Rod Vagg <@rvagg> and licenced under the MIT licence. All rights not explicitly granted in the MIT license are reserved. See the included LICENSE file for more details.
View
82 index.js
@@ -1,72 +1,92 @@
-var spawn = require('child_process').spawn
- , path = require('path')
- , Stream = require('stream').Stream
+var spawn = require('child_process').spawn
+ , path = require('path')
+ , Stream = require('stream').Stream
-module.exports = function (lang, format, opts) {
- var exec = spawn(path.join(__dirname, 'vendor/pygments/pygmentize'), [ '-f', format, '-l', lang, '-P', 'encoding=utf8' ])
- , stdout = []
- , stderr = ''
+ , defaultFormat = 'html'
+ , defaultLang = 'js'
+ , defaultEncoding = 'utf8'
+
+ , fromString = function(exec, code, callback) {
+ var stdout = []
+ , stderr = ''
- return {
- fromString: function(code, callback) {
exec.stdout.on('data', function(data) {
- stdout.push(data);
+ stdout.push(data)
})
+
exec.stderr.on('data', function (data) {
- stderr += data.toString();
+ stderr += data.toString()
})
+
exec.on('exit', function (code) {
if (code !== 0) return callback('Error: ' + stderr)
var buf = new Buffer(stdout.reduce(function (p, c) { return p + c.length }, 0))
, i = 0
stdout.forEach(function(s) {
- s.copy(buf, i, 0, s.length);
- i += s.length;
+ s.copy(buf, i, 0, s.length)
+ i += s.length
})
- callback(null, buf);
+ callback(null, buf)
})
- exec.stdin.write(code);
- exec.stdin.end();
- },
- fromStream: function() {
- var stream = new Stream();
+ exec.stdin.write(code)
+ exec.stdin.end()
+ }
+
+ , fromStream = function(exec) {
+ var stream = new Stream()
+ , stderr = ''
- stream.writable = true;
- stream.readable = true;
+ stream.writable = true
+ stream.readable = true
exec.stdout.on('data', function(data) {
- stream.emit('data', data);
+ stream.emit('data', data)
})
exec.stderr.on('data', function (data) {
- stderr += data.toString();
+ stderr += data.toString()
})
exec.on('exit', function (code) {
if (code !== 0) {
- stream.emit('error', stderr);
+ stream.emit('error', stderr)
} else {
- stream.emit('end');
+ stream.emit('end')
}
})
stream.write = function(data) {
- exec.stdin.write(data);
+ exec.stdin.write(data)
}
stream.end = function() {
- exec.stdin.end();
+ exec.stdin.end()
}
stream.destroy = function() {
- stream.emit("close");
+ stream.emit("close")
}
- return stream;
+ return stream
}
- }
-}
+
+ , pygmentize = function (options, code, callback) {
+ options = options || {}
+
+ var execArgs = [
+ '-f', options.format || defaultFormat
+ , '-l', options.lang || defaultLang
+ , '-P', 'encoding=' + (options.encoding || defaultEncoding)
+ ]
+ , exec = spawn(path.join(__dirname, 'vendor/pygments/pygmentize'), execArgs)
+
+ return typeof code == 'string' && typeof callback == 'function'
+ ? fromString(exec, code, callback)
+ : fromStream(exec)
+ }
+
+module.exports = pygmentize
View
8 package.json
@@ -18,11 +18,11 @@
, "stream"
]
, "authors": [
- "Rod Vagg @rvagg <rod@vagg.org> (https://github.com/rvagg)",
- "Cyril Rohr @crohr (https://github.com/crohr)"
+ "Rod Vagg @rvagg <rod@vagg.org> (https://github.com/rvagg)"
+ , "Cyril Rohr @crohr (https://github.com/crohr)"
]
, "license": "MIT"
, "devDependencies": {
- "stream-equal": ">= 0.0.1"
- }
+ "stream-equal": ">= 0.0.1"
+ }
}
View
20 test/test.js
@@ -1,7 +1,6 @@
var assert = require('assert')
, pygments = require('../')
, fs = require('fs')
- , Stream = require('stream').Stream
, streamEqual = require('stream-equal')
, cases = [
@@ -20,25 +19,24 @@ var assert = require('assert')
]
cases.forEach(function (c) {
- pygments(c.lang, c.format).fromString(c.input, function (err, result) {
+ pygments({ lang: c.lang, format: c.format }, c.input, function (err, result) {
assert.equal(err, null)
result = result.toString().replace(/\n/g, '')
assert.equal(result, c.output)
})
})
-var fileIn = fs.createReadStream(__dirname + '/fixtures/active_model.rb');
-var fileOut = fs.createWriteStream(__dirname + '/fixtures/active_model.tmp', { flags: 'w+', encoding: null, mode: 0666 });
+var fileIn = fs.createReadStream(__dirname + '/fixtures/active_model.rb')
+var fileOut = fs.createWriteStream(__dirname + '/fixtures/active_model.tmp', { flags: 'w+', encoding: null, mode: 0666 })
-fileIn.pipe(pygments("rb", "html").fromStream()).pipe(fileOut);
+fileIn.pipe(pygments({ lang: 'rb', format: 'html' })).pipe(fileOut)
fileOut.on("close", function() {
- var expectedResult = fs.createReadStream(__dirname + '/fixtures/active_model.html');
- var result = fs.createReadStream(__dirname + '/fixtures/active_model.tmp');
+ var expectedResult = fs.createReadStream(__dirname + '/fixtures/active_model.html')
+ var result = fs.createReadStream(__dirname + '/fixtures/active_model.tmp')
streamEqual(expectedResult, result, function(err, equal) {
- if(err) throw err;
+ if(err) throw err
assert.equal(equal, true)
- });
-
-})
+ })
+})

0 comments on commit 3a6fbf6

Please sign in to comment.