Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

pygmentize-bundled

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

pygmentize-bundled - npm Package Compare versions

Comparing version 0.0.0 to 1.0.0

.npmignore

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.
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc