This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+76
View File
@@ -0,0 +1,76 @@
var redeyed = require('redeyed')
, theme = require('../themes/default')
, colors = require('ansicolors')
, colorSurround = colors.brightBlack
, 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
, lines = []
, totalDigits
, 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)
, 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;
}
};
+23
View File
@@ -0,0 +1,23 @@
var fs = require('fs')
, highlight = require('./highlight');
function isFunction (obj) {
return toString.call(obj) == '[object Function]';
}
module.exports = function highlightFile (fullPath, opts, cb) {
if (isFunction(opts)) {
cb = opts;
opts = { };
}
opts = opts || { };
fs.readFile(fullPath, 'utf-8', function (err, code) {
if (err) return cb(err);
try {
cb(null, highlight(code, opts));
} catch (e) {
cb(e);
}
});
};
+8
View File
@@ -0,0 +1,8 @@
var fs = require('fs')
, highlight = require('./highlight');
module.exports = function highlightFileSync (fullPath, opts) {
var code = fs.readFileSync(fullPath, 'utf-8');
opts = opts || { };
return highlight(code, opts);
};