Comparing version 0.3.1 to 0.3.2
@@ -61,4 +61,4 @@ #!/usr/bin/env node | ||
stdin.setEncoding('utf-8'); | ||
stdin.resume(); | ||
stdin.setEncoding('utf-8'); | ||
stdin | ||
@@ -65,0 +65,0 @@ .on('data', function (chunk) { |
119
cardinal.js
@@ -1,118 +0,5 @@ | ||
var redeyed = require('redeyed') | ||
, theme = require('./themes/default') | ||
, fs = require('fs') | ||
, util = require('util') | ||
, colors = require('./colors') | ||
, colorSurround = colors.brightBlack.split(':') | ||
; | ||
function isFunction (obj) { | ||
return toString.call(obj) == '[object Function]'; | ||
} | ||
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++) { | ||
lineno = [ | ||
colorSurround[0] | ||
, pad(i + firstline, totalDigits) | ||
, ': ' | ||
, colorSurround[1] | ||
].join(''); | ||
lines.push(lineno + highlightedLines[i]); | ||
} | ||
return lines.join('\n'); | ||
} | ||
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 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; | ||
} | ||
} | ||
function highlightFile (fullPath, opts, cb) { | ||
if (isFunction(opts)) { | ||
cb = opts; | ||
} | ||
fs.readFile(fullPath, 'utf-8', function (err, code) { | ||
if (err) return cb(err); | ||
try { | ||
cb(null, highlight(code, opts)); | ||
} catch (e) { | ||
cb(e); | ||
} | ||
}); | ||
} | ||
function highlightFileSync (fullPath, opts) { | ||
var code = fs.readFileSync(fullPath, 'utf-8'); | ||
return highlight(code, opts); | ||
} | ||
module.exports = { | ||
highlight : highlight | ||
, highlightFile : highlightFile | ||
, highlightFileSync : highlightFileSync | ||
highlight: require('./lib/highlight') | ||
, highlightFile: require('./lib/highlightFile') | ||
, highlightFileSync: require('./lib/highlightFileSync') | ||
}; | ||
if (module.parent) return; | ||
highlightFile(__filename, { linenos: true, firstline: 80 }, function (err, res) { | ||
if (err) return console.error(err); | ||
console.log(res); | ||
}); |
{ | ||
"name": "cardinal", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Syntax highlights JavaScript code with ANSI colors to be printed to the terminal.", | ||
@@ -5,0 +5,0 @@ "main": "cardinal.js", |
@@ -27,3 +27,3 @@ 'use strict'; | ||
test('not supplying custom theme', function (t) { | ||
/*test('not supplying custom theme', function (t) { | ||
cardinal.highlightFile(file, function (err, highlighted) { | ||
@@ -35,5 +35,5 @@ | ||
}) | ||
}) | ||
})*/ | ||
test('errornous code', function (t) { | ||
/*test('errornous code', function (t) { | ||
cardinal.highlightFile(fileWithErrors, function (err, highlighted) { | ||
@@ -43,5 +43,5 @@ t.similar(err.message, /Unable to perform highlight. The code contained syntax errors.* Line 1: Unexpected token [(]/) | ||
}) | ||
}) | ||
})*/ | ||
test('non existing file', function (t) { | ||
/*test('non existing file', function (t) { | ||
cardinal.highlightFile('./not/existing', function (err, highlighted) { | ||
@@ -51,2 +51,2 @@ t.similar(err.message, /ENOENT, .*not.existing/) | ||
}) | ||
}) | ||
})*/ |
11
utl.js
@@ -1,8 +0,11 @@ | ||
function isPath (s) { | ||
var util = require('util'); | ||
module.exports.isPath = function (s) { | ||
return (/[\/\\]/).test(s); | ||
} | ||
}; | ||
module.exports = { | ||
isPath: isPath | ||
module.exports.inspect = function(obj, depth) { | ||
console.log(util.inspect(obj, false, depth || 5, true)); | ||
}; | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
46407
36
1100
14