pygmentize-bundled
Advanced tools
Comparing version 0.0.0 to 1.0.0
112
index.js
@@ -1,30 +0,92 @@ | ||
var spawn = require('child_process').spawn | ||
, path = require('path') | ||
var spawn = require('child_process').spawn | ||
, path = require('path') | ||
, Stream = require('stream').Stream | ||
module.exports = function (lang, format, code, callback) { | ||
var exec = spawn(path.join(__dirname, 'pygments/pygmentize'), [ '-f', format, '-l', lang, '-P', 'encoding=utf8' ]) | ||
, stdout = [] | ||
, stderr = '' | ||
, defaultFormat = 'html' | ||
, defaultLang = 'js' | ||
, defaultEncoding = 'utf8' | ||
exec.stdout.on('data', function(data) { | ||
stdout.push(data) | ||
}) | ||
exec.stderr.on('data', function (data) { | ||
stderr += data.toString() | ||
}) | ||
exec.on('exit', function (code) { | ||
if (code !== 0) return callback('Error: ' + stderr) | ||
, fromString = function(exec, code, callback) { | ||
var stdout = [] | ||
, stderr = '' | ||
var buf = new Buffer(stdout.reduce(function (p, c) { return p + c.length }, 0)) | ||
, i = 0 | ||
exec.stdout.on('data', function(data) { | ||
stdout.push(data) | ||
}) | ||
stdout.forEach(function(s) { | ||
s.copy(buf, i, 0, s.length) | ||
i += s.length | ||
}) | ||
exec.stderr.on('data', function (data) { | ||
stderr += data.toString() | ||
}) | ||
callback(null, buf) | ||
}) | ||
exec.stdin.write(code) | ||
exec.stdin.end() | ||
} | ||
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 | ||
}) | ||
callback(null, buf) | ||
}) | ||
exec.stdin.write(code) | ||
exec.stdin.end() | ||
} | ||
, fromStream = function(exec) { | ||
var stream = new Stream() | ||
, stderr = '' | ||
stream.writable = true | ||
stream.readable = true | ||
exec.stdout.on('data', function(data) { | ||
stream.emit('data', data) | ||
}) | ||
exec.stderr.on('data', function (data) { | ||
stderr += data.toString() | ||
}) | ||
exec.on('exit', function (code) { | ||
if (code !== 0) { | ||
stream.emit('error', stderr) | ||
} else { | ||
stream.emit('end') | ||
} | ||
}) | ||
stream.write = function(data) { | ||
exec.stdin.write(data) | ||
} | ||
stream.end = function() { | ||
exec.stdin.end() | ||
} | ||
stream.destroy = function() { | ||
stream.emit("close") | ||
} | ||
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 |
{ | ||
"name": "pygmentize-bundled" | ||
, "version": "0.0.0" | ||
, "version": "1.0.0" | ||
, "description": "A simple wrapper around Python's Pygments code formatter, with Pygments bundled" | ||
, "main": "index.js" | ||
, "scripts": { | ||
"test": "node ./test.js" | ||
"test": "node ./test/test.js" | ||
} | ||
@@ -18,7 +18,12 @@ , "repository": { | ||
, "highlight" | ||
, "stream" | ||
] | ||
, "authors": [ | ||
"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" | ||
} | ||
} |
# 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. | ||
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. | ||
Available as a simple *String-in, Buffer-out* interface and also as a *read/write-Stream* interface. | ||
Currently the interface is very simple, no additional options: | ||
## 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', 'var a = "b";', function (err, result) { | ||
pygmentize({ lang: 'js', format: 'html'}, 'var a = "b";', function (err, result) { | ||
console.log(result.toString()) | ||
@@ -30,13 +49,25 @@ }) | ||
## API | ||
A streaming API is also available. Simply omit the `code` and `callback` arguments: | ||
**pygmentize(lang, format, code, callback)** | ||
```js | ||
var pygmentize = require('pygmentize-bundled') | ||
process.stdin.pipe( | ||
pygmentize({ lang: 'js', format: 'html' }) | ||
).pipe(process.stdout); | ||
``` | ||
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 | ||
------------------- | ||
## Contributors | ||
* [Rod Vagg](https://github.com/rvagg) | ||
* [Cyril Rohr](https://github.com/crohr) | ||
## 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. | ||
Pygments is licenced under the BSD licence. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
455
1
72
4794926
1
358
1
2