pygmentize-bundled
Advanced tools
Comparing version 1.2.0 to 2.0.0
205
index.js
@@ -1,108 +0,147 @@ | ||
var spawn = require('child_process').spawn | ||
, path = require('path') | ||
, Stream = require('stream').Stream | ||
const spawn = require('child_process').spawn | ||
, exec = require('child_process').exec | ||
, path = require('path') | ||
, fs = require('fs') | ||
, PassThrough = require('readable-stream/passthrough') | ||
, mkdirp = require('mkdirp') | ||
, bl = require('bl') | ||
, through2 = require('through2') | ||
, defaultFormat = 'html' | ||
, defaultLang = 'js' | ||
, defaultEncoding = 'utf8' | ||
, confDir = path.join(process.env.HOME || process.env.USERPROFILE, '.config') | ||
, confFile = path.join(confDir, 'node-pygmentize-bundled.json') | ||
, fromString = function(exec, code, callback) { | ||
var stdout = [] | ||
, stderr = '' | ||
, ec = 0 | ||
, exitClose = function () { | ||
if (++ec < 2) return | ||
, defaultFormat = 'html' | ||
, defaultLang = 'js' | ||
, defaultEncoding = 'utf8' | ||
var buf = new Buffer(stdout.reduce(function (p, c) { return p + c.length }, 0)) | ||
, i = 0 | ||
var pythonVersions | ||
stdout.forEach(function(s) { | ||
s.copy(buf, i, 0, s.length) | ||
i += s.length | ||
}) | ||
function fromString (child, code, callback) { | ||
var stdout = bl() | ||
, stderr = bl() | ||
, ec = 0 | ||
, exitClose = function () { | ||
if (++ec < 2) | ||
return | ||
callback(null, buf) | ||
} | ||
callback(null, stdout.slice()) | ||
} | ||
exec.stdout.on('data', function(data) { | ||
stdout.push(data) | ||
}) | ||
child.stdout.pipe(stdout) | ||
child.stderr.pipe(stderr) | ||
exec.stderr.on('data', function (data) { | ||
stderr += data.toString() | ||
}) | ||
child.on('exit', function (code) { | ||
if (code !== 0) { | ||
ec = -1 | ||
return callback('Error: ' + stderr) | ||
} | ||
exitClose() | ||
}) | ||
child.on('close', exitClose) | ||
exec.on('exit', function (code) { | ||
if (code !== 0) { | ||
ec = -1 | ||
return callback('Error: ' + stderr) | ||
} | ||
exitClose() | ||
child.stdin.write(code) | ||
child.stdin.end() | ||
} | ||
function fromStream (retStream, intStream, child) { | ||
var stderr = bl() | ||
, outStream = through2(function (chunk, enc, callback) { | ||
retStream.__write(chunk, enc, callback) | ||
}) | ||
exec.on('close', exitClose) | ||
exec.stdin.write(code) | ||
exec.stdin.end() | ||
} | ||
intStream.pipe(child.stdin) | ||
child.stdout.pipe(outStream) | ||
child.stderr.pipe(stderr) | ||
, fromStream = function(exec) { | ||
var stream = new Stream() | ||
, stderr = '' | ||
child.on('exit', function (code) { | ||
if (code !== 0) | ||
retStream.emit('error', stderr.toString()) | ||
retStream.__end() | ||
}) | ||
} | ||
stream.writable = true | ||
stream.readable = true | ||
function pygmentize (options, code, callback) { | ||
options = options || {} | ||
exec.stdout.on('data', function(data) { | ||
stream.emit('data', data) | ||
}) | ||
var execArgs = [ | ||
'-f', options.format || defaultFormat | ||
, '-l', options.lang || defaultLang | ||
, '-P', 'encoding=' + (options.encoding || defaultEncoding) | ||
] | ||
, toString = typeof code == 'string' && typeof callback == 'function' | ||
, retStream = !toString && through2() | ||
, intStream = !toString && through2() | ||
exec.stderr.on('data', function (data) { | ||
stderr += data.toString() | ||
}) | ||
if (typeof options.options == 'object') { | ||
Object.keys(options.options).forEach(function (key) { | ||
execArgs.push('-P', key + '=' + options.options[key]) | ||
}) | ||
} | ||
exec.on('exit', function (code) { | ||
if (code !== 0) { | ||
stream.emit('error', stderr) | ||
} else { | ||
stream.emit('end') | ||
} | ||
}) | ||
spawnPygmentize(options, execArgs, function (err, child) { | ||
if (err) | ||
return callback(err) | ||
if (toString) | ||
return fromString(child, code, callback) | ||
fromStream(retStream, intStream, child) | ||
}) | ||
stream.write = function(data) { | ||
exec.stdin.write(data) | ||
} | ||
if (retStream) { | ||
retStream.__write = retStream.write | ||
retStream.write = intStream.write.bind(intStream) | ||
retStream.__end = retStream.end | ||
retStream.end = intStream.end.bind(intStream) | ||
} | ||
stream.end = function() { | ||
exec.stdin.end() | ||
} | ||
return retStream | ||
} | ||
stream.destroy = function() { | ||
stream.emit("close") | ||
} | ||
function spawnPygmentize (options, execArgs, callback) { | ||
var python = typeof options.python == 'string' ? options.python : 'python' | ||
return stream | ||
} | ||
pythonVersion(python, function (err, version) { | ||
if (err) | ||
return callback(err) | ||
if (version != 2 && version != 3) | ||
return callback(new Error('Unsupported Python version: ' + version)) | ||
, pygmentize = function (options, code, callback) { | ||
options = options || {} | ||
var pyg = path.join( | ||
__dirname | ||
, 'vendor/pygments' | ||
, version == 2 ? 'build-2.7' : 'build-3.3' | ||
, 'pygmentize' | ||
) | ||
var execArgs = [ | ||
'-f', options.format || defaultFormat | ||
, '-l', options.lang || defaultLang | ||
, '-P', 'encoding=' + (options.encoding || defaultEncoding) | ||
] | ||
callback(null, spawn(python, [ pyg ].concat(execArgs))) | ||
}) | ||
} | ||
if (options.options) { | ||
for (var option in options.options) { | ||
execArgs.push('-P', option + '=' + options.options[option]) | ||
} | ||
} | ||
function pythonVersion (python, callback) { | ||
if (!pythonVersions) { | ||
try { | ||
pythonVersions = require(confFile) | ||
} catch (e) {} | ||
if (typeof pythonVersions != 'object') | ||
pythonVersions = {} | ||
} | ||
var exec = spawn("python", [path.join(__dirname, 'vendor/pygments/pygmentize')].concat(execArgs)) | ||
if (pythonVersions[python]) | ||
return callback(null, pythonVersions[python]) | ||
return typeof code == 'string' && typeof callback == 'function' | ||
? fromString(exec, code, callback) | ||
: fromStream(exec) | ||
} | ||
exec(python + ' -V', function (err, stdout, stderr) { | ||
if (err) | ||
return callback(err) | ||
module.exports = pygmentize | ||
var m = stderr.toString().match(/^Python (\d)[.\d]+/i) | ||
if (!m) | ||
return callback(new Error('cannot determine Python version: [' + stderr.toString() + ']')) | ||
pythonVersions[python] = +m[1] | ||
mkdirp.sync(confDir) | ||
fs.writeFileSync(confFile, JSON.stringify(pythonVersions), 'utf8') | ||
return callback(null, +m[1]) | ||
}) | ||
} | ||
module.exports = pygmentize |
{ | ||
"name": "pygmentize-bundled" | ||
, "version": "1.2.0" | ||
, "description": "A simple wrapper around Python's Pygments code formatter, with Pygments bundled" | ||
, "main": "index.js" | ||
, "scripts": { | ||
"test": "node ./test/test.js" | ||
} | ||
, "repository": { | ||
"type": "git" | ||
, "url": "https://github.com/rvagg/node-pygmentize-bundled.git" | ||
"name" : "pygmentize-bundled" | ||
, "version" : "2.0.0" | ||
, "description" : "A simple wrapper around Python's Pygments code formatter, with Pygments bundled" | ||
, "main" : "index.js" | ||
, "scripts" : { | ||
"test" : "node ./test.js" | ||
} | ||
, "keywords": [ | ||
, "repository" : { | ||
"type" : "git" | ||
, "url" : "https://github.com/rvagg/node-pygmentize-bundled.git" | ||
} | ||
, "keywords" : [ | ||
"pygments" | ||
@@ -20,3 +20,3 @@ , "pygmentize" | ||
] | ||
, "authors": [ | ||
, "authors" : [ | ||
"Rod Vagg @rvagg <rod@vagg.org> (https://github.com/rvagg)" | ||
@@ -27,6 +27,13 @@ , "Cyril Rohr @crohr (https://github.com/crohr)" | ||
] | ||
, "license": "MIT" | ||
, "devDependencies": { | ||
"stream-equal": ">= 0.0.1" | ||
, "license" : "MIT" | ||
, "dependencies" : { | ||
"mkdirp" : "~0.3.5" | ||
, "readable-stream" : "~1.0.17" | ||
, "bl" : "~0.4.1" | ||
, "through2" : "~0.2.1" | ||
} | ||
, "devDependencies" : { | ||
"stream-equal" : ">= 0.0.1" | ||
, "tape" : "*" | ||
} | ||
} |
# Pygmentize (Bundled) | ||
A simple wrapper around Python's Pygments code formatter, with Pygments bundled. | ||
**Python's Pygments code formatter, for Node.js, distributed with Pygments** | ||
Available as a simple *String-in, Buffer-out* interface and also as a *read/write-Stream* interface. | ||
[![NPM](https://nodei.co/npm/pygmentize-bundled.png?downloads=true&stars=true)](https://nodei.co/npm/pygmentize-bundled/) [![NPM](https://nodei.co/npm-dl/pygmentize-bundled.png?months=6)](https://nodei.co/npm/pygmentize-bundled/) | ||
Can be used as either a *String-in, Buffer-out*, or as a Duplex stream. | ||
Compatible with both Python v2 and v3. | ||
## API | ||
@@ -20,7 +23,6 @@ | ||
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. | ||
When you only supply the `options` argument, it will return a Duplex stream that you can pipe to and from to format your code. | ||
* `options` contains options to be passed to Pygments (see [Options](#options)). | ||
## Options | ||
@@ -30,7 +32,7 @@ | ||
* `lang`: source language/lexer name - `string` | ||
* `format`: output formatter name - `string` | ||
* `options`: lexer and formatter options - `object` | ||
* `lang`: source language/lexer name - `String` | ||
* `format`: output formatter name - `String` | ||
* `python`: the full path to the `python` command on the current system, defaults to `'python'` - `String` | ||
* `options`: lexer and formatter options, each key/value pair is passed through to `pygmentize` with `-P` - `Object` | ||
## Examples | ||
@@ -70,3 +72,3 @@ | ||
A streaming API is also available. Simply omit the `code` and `callback` arguments: | ||
A duplex streaming API is also available. Simply omit the `code` and `callback` arguments: | ||
@@ -76,5 +78,5 @@ ```js | ||
process.stdin.pipe( | ||
pygmentize({ lang: 'js', format: 'html' }) | ||
).pipe(process.stdout); | ||
process.stdin | ||
.pipe(pygmentize({ lang: 'js', format: 'html' })) | ||
.pipe(process.stdout); | ||
``` | ||
@@ -84,3 +86,2 @@ | ||
## Contributors | ||
@@ -97,2 +98,2 @@ | ||
Pygments is licenced under the BSD licence. | ||
Pygments is licenced under the BSD licence. |
@@ -67,3 +67,3 @@ .. -*- mode: rst -*- | ||
or `alias_filenames` that matches `filename` are taken into consideration. | ||
`pygments.util.ClassNotFound` is raised if no lexer thinks it can handle the | ||
@@ -70,0 +70,0 @@ content. |
@@ -40,3 +40,3 @@ .. -*- mode: rst -*- | ||
- `Write your own formatter <formatterdevelopment.txt>`_ | ||
- `Write your own filter <filterdevelopment.txt>`_ | ||
@@ -43,0 +43,0 @@ |
@@ -44,1 +44,6 @@ .. -*- mode: rst -*- | ||
sets up completion for the ``pygmentize`` command in bash. | ||
Java | ||
---- | ||
See the `Java quickstart <java.txt>`_ document. |
@@ -86,2 +86,54 @@ .. -*- mode: rst -*- | ||
Adding and testing a new lexer | ||
============================== | ||
To make pygments aware of your new lexer, you have to perform the following | ||
steps: | ||
First, change to the current directory containing the pygments source code: | ||
.. sourcecode:: console | ||
$ cd .../pygments-main | ||
Next, make sure the lexer is known from outside of the module. All modules in | ||
the ``pygments.lexers`` specify ``__all__``. For example, ``other.py`` sets: | ||
.. sourcecode:: python | ||
__all__ = ['BrainfuckLexer', 'BefungeLexer', ...] | ||
Simply add the name of your lexer class to this list. | ||
Finally the lexer can be made publically known by rebuilding the lexer | ||
mapping: | ||
.. sourcecode:: console | ||
$ make mapfiles | ||
To test the new lexer, store an example file with the proper extension in | ||
``tests/examplefiles``. For example, to test your ``DiffLexer``, add a | ||
``tests/examplefiles/example.diff`` containing a sample diff output. | ||
Now you can use pygmentize to render your example to HTML: | ||
.. sourcecode:: console | ||
$ ./pygmentize -O full -f html -o /tmp/example.html tests/examplefiles/example.diff | ||
Note that this explicitely calls the ``pygmentize`` in the current directory | ||
by preceding it with ``./``. This ensures your modifications are used. | ||
Otherwise a possibly already installed, unmodified version without your new | ||
lexer would have been called from the system search path (``$PATH``). | ||
To view the result, open ``/tmp/example.html`` in your browser. | ||
Once the example renders as expected, you should run the complete test suite: | ||
.. sourcecode:: console | ||
$ make test | ||
Regex Flags | ||
@@ -88,0 +140,0 @@ =========== |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 2 instances in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
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
Copyleft License
License(Experimental) Copyleft license information was found.
Found 1 instance in 1 package
Mixed license
License(Experimental) Package contains multiple licenses.
Found 1 instance in 1 package
Non-permissive License
License(Experimental) A license not known to be considered permissive was found.
Found 1 instance in 1 package
7096567
1
100
4
2
291
461
5
2
+ Addedbl@~0.4.1
+ Addedmkdirp@~0.3.5
+ Addedreadable-stream@~1.0.17
+ Addedthrough2@~0.2.1
+ Addedbl@0.4.2(transitive)
+ Addedcore-util-is@1.0.3(transitive)
+ Addedinherits@2.0.4(transitive)
+ Addedisarray@0.0.1(transitive)
+ Addedmkdirp@0.3.5(transitive)
+ Addedobject-keys@0.4.0(transitive)
+ Addedreadable-stream@1.0.341.1.14(transitive)
+ Addedstring_decoder@0.10.31(transitive)
+ Addedthrough2@0.2.3(transitive)
+ Addedxtend@2.1.2(transitive)