ff9f8b763d943fc0d02277a0ebd48653839516f0.svn-base
2.01 KB
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
'use strict'
var redeyed = require('redeyed')
var theme = require('../themes/default')
var colors = require('ansicolors')
var colorSurround = colors.brightBlack
var surroundClose = '\u001b[39m'
function trimEmptyLines(lines) {
// remove lines from the end until we find a non-empy one
var line = lines.pop()
while (!line || !line.length) {
line = lines.pop()
}
// put the non-empty line back
if (line) lines.push(line)
}
function addLinenos(highlightedCode, firstline) {
var highlightedLines = highlightedCode.split('\n')
trimEmptyLines(highlightedLines)
var linesLen = highlightedLines.length
var lines = []
var totalDigits
var lineno
function getDigits(n) {
if (n < 10) return 1
if (n < 100) return 2
if (n < 1000) return 3
if (n < 10000) return 4
// this works for up to 99,999 lines - any questions?
return 5
}
function pad(n, totalDigits) {
// not pretty, but simple and should perform quite well
var padDigits = totalDigits - getDigits(n)
switch (padDigits) {
case 0: return '' + n
case 1: return ' ' + n
case 2: return ' ' + n
case 3: return ' ' + n
case 4: return ' ' + n
case 5: return ' ' + n
}
}
totalDigits = getDigits(linesLen + firstline - 1)
for (var i = 0; i < linesLen; i++) {
// Don't close the escape sequence here in order to not break multi line code highlights like block comments
lineno = colorSurround(pad(i + firstline, totalDigits) + ': ').replace(surroundClose, '')
lines.push(lineno + highlightedLines[i])
}
return lines.join('\n')
}
module.exports = function highlight(code, opts) {
opts = opts || { }
try {
var result = redeyed(code, opts.theme || theme, { jsx: !!opts.jsx })
var firstline = opts.firstline && !isNaN(opts.firstline) ? opts.firstline : 1
return opts.linenos ? addLinenos(result.code, firstline) : result.code
} catch (e) {
e.message = 'Unable to perform highlight. The code contained syntax errors: ' + e.message
throw e
}
}