This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
172 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,111 +1,147 @@ | ||
var spawn = require('child_process').spawn | ||
, path = require('path') | ||
, Stream = require('stream').Stream | ||
|
||
, defaultFormat = 'html' | ||
, defaultLang = 'js' | ||
, defaultEncoding = 'utf8' | ||
|
||
, fromString = function (exec, code, callback) { | ||
var stdout = [] | ||
, stderr = '' | ||
, ec = 0 | ||
, exitClose = function () { | ||
if (++ec < 2) return | ||
|
||
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.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) { | ||
ec = -1 | ||
return callback('Error: ' + stderr) | ||
} | ||
exitClose() | ||
}) | ||
exec.on('close', exitClose) | ||
|
||
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) | ||
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') | ||
|
||
, confDir = path.join(process.env.HOME || process.env.USERPROFILE, '.config') | ||
, confFile = path.join(confDir, 'node-pygmentize-bundled.json') | ||
|
||
, defaultFormat = 'html' | ||
, defaultLang = 'js' | ||
, defaultEncoding = 'utf8' | ||
|
||
var pythonVersions | ||
|
||
function fromString (child, code, callback) { | ||
var stdout = bl() | ||
, stderr = bl() | ||
, ec = 0 | ||
, exitClose = function () { | ||
if (++ec < 2) | ||
return | ||
|
||
callback(null, stdout.slice()) | ||
} | ||
|
||
stream.end = function () { | ||
exec.stdin.end() | ||
} | ||
child.stdout.pipe(stdout) | ||
child.stderr.pipe(stderr) | ||
|
||
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) | ||
] | ||
|
||
if (options.options) { | ||
for (var option in options.options) { | ||
execArgs.push('-P', option + '=' + options.options[option]) | ||
} | ||
} | ||
|
||
var exec = spawn( | ||
typeof options.python == 'string' ? options.python : 'python' | ||
, [ path.join(__dirname, 'vendor/pygments/pygmentize') ].concat(execArgs) | ||
) | ||
|
||
return typeof code == 'string' && typeof callback == 'function' | ||
? fromString(exec, code, callback) | ||
: fromStream(exec) | ||
child.on('exit', function (code) { | ||
if (code !== 0) { | ||
ec = -1 | ||
return callback('Error: ' + stderr) | ||
} | ||
exitClose() | ||
}) | ||
child.on('close', 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) | ||
}) | ||
|
||
module.exports = pygmentize | ||
intStream.pipe(child.stdin) | ||
child.stdout.pipe(outStream) | ||
child.stderr.pipe(stderr) | ||
|
||
child.on('exit', function (code) { | ||
if (code !== 0) | ||
retStream.emit('error', stderr.toString()) | ||
retStream.__end() | ||
}) | ||
} | ||
|
||
function pygmentize (options, code, callback) { | ||
options = options || {} | ||
|
||
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() | ||
|
||
if (typeof options.options == 'object') { | ||
Object.keys(options.options).forEach(function (key) { | ||
execArgs.push('-P', key + '=' + options.options[key]) | ||
}) | ||
} | ||
|
||
spawnPygmentize(options, execArgs, function (err, child) { | ||
if (err) | ||
return callback(err) | ||
if (toString) | ||
return fromString(child, code, callback) | ||
fromStream(retStream, intStream, child) | ||
}) | ||
|
||
if (retStream) { | ||
retStream.__write = retStream.write | ||
retStream.write = intStream.write.bind(intStream) | ||
retStream.__end = retStream.end | ||
retStream.end = intStream.end.bind(intStream) | ||
} | ||
|
||
return retStream | ||
} | ||
|
||
function spawnPygmentize (options, execArgs, callback) { | ||
var python = typeof options.python == 'string' ? options.python : 'python' | ||
|
||
pythonVersion(python, function (err, version) { | ||
if (err) | ||
return callback(err) | ||
if (version != 2 && version != 3) | ||
return callback(new Error('Unsupported Python version: ' + version)) | ||
|
||
var pyg = path.join( | ||
__dirname | ||
, 'vendor/pygments' | ||
, version == 2 ? 'build-2.7' : 'build-3.3' | ||
, 'pygmentize' | ||
) | ||
|
||
callback(null, spawn(python, [ pyg ].concat(execArgs))) | ||
}) | ||
} | ||
|
||
function pythonVersion (python, callback) { | ||
if (!pythonVersions) { | ||
try { | ||
pythonVersions = require(confFile) | ||
} catch (e) {} | ||
if (typeof pythonVersions != 'object') | ||
pythonVersions = {} | ||
} | ||
|
||
if (pythonVersions[python]) | ||
return callback(null, pythonVersions[python]) | ||
|
||
exec(python + ' -V', function (err, stdout, stderr) { | ||
if (err) | ||
return callback(err) | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters