This repository has been archived by the owner on Apr 5, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
144 lines (111 loc) · 3.48 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const spawn = require('child_process').spawn
, exec = require('child_process').exec
, path = require('path')
, bl = require('bl')
, through2 = require('through2')
, 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())
}
child.stdout.pipe(stdout)
child.stderr.pipe(stderr)
child.on('exit', function (code) {
if (code !== 0) {
ec = -1
return callback(new Error('Error calling `pygmentize`: ' + stderr.toString()))
}
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)
})
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 (toString) {
if (err)
return callback(err)
return fromString(child, code, callback)
}
// else stream
if (err)
return retStream.emit('error', err)
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[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)
m = stdout.toString().match(/^Python (\d)[.\d]+/i)
if (!m)
return callback(new Error('Cannot determine Python version: [' + stderr.toString() + ']'))
pythonVersions[python] = +m[1]
return callback(null, +m[1])
})
}
module.exports = pygmentize