--
This commit is contained in:
parent
24f3d27984
commit
6b60015c02
9588 changed files with 1149251 additions and 49 deletions
76
node_modules/cardinal/lib/highlight.js
generated
vendored
Normal file
76
node_modules/cardinal/lib/highlight.js
generated
vendored
Normal 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
node_modules/cardinal/lib/highlightFile.js
generated
vendored
Normal file
23
node_modules/cardinal/lib/highlightFile.js
generated
vendored
Normal 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
node_modules/cardinal/lib/highlightFileSync.js
generated
vendored
Normal file
8
node_modules/cardinal/lib/highlightFileSync.js
generated
vendored
Normal 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);
|
||||
};
|
||||
Loading…
Reference in a new issue