ansi-to-html
Advanced tools
Comparing version 0.6.11 to 0.6.12
@@ -1,51 +0,53 @@ | ||
'use strict'; | ||
"use strict"; | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } | ||
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } | ||
var entities = require('entities'); | ||
var defaults = { | ||
fg: '#FFF', | ||
bg: '#000', | ||
newline: false, | ||
escapeXML: false, | ||
stream: false, | ||
colors: getDefaultColors() | ||
fg: '#FFF', | ||
bg: '#000', | ||
newline: false, | ||
escapeXML: false, | ||
stream: false, | ||
colors: getDefaultColors() | ||
}; | ||
function getDefaultColors() { | ||
var colors = { | ||
0: '#000', | ||
1: '#A00', | ||
2: '#0A0', | ||
3: '#A50', | ||
4: '#00A', | ||
5: '#A0A', | ||
6: '#0AA', | ||
7: '#AAA', | ||
8: '#555', | ||
9: '#F55', | ||
10: '#5F5', | ||
11: '#FF5', | ||
12: '#55F', | ||
13: '#F5F', | ||
14: '#5FF', | ||
15: '#FFF' | ||
}; | ||
range(0, 5).forEach(function (red) { | ||
range(0, 5).forEach(function (green) { | ||
range(0, 5).forEach(function (blue) { | ||
return setStyleColor(red, green, blue, colors); | ||
}); | ||
}); | ||
var colors = { | ||
0: '#000', | ||
1: '#A00', | ||
2: '#0A0', | ||
3: '#A50', | ||
4: '#00A', | ||
5: '#A0A', | ||
6: '#0AA', | ||
7: '#AAA', | ||
8: '#555', | ||
9: '#F55', | ||
10: '#5F5', | ||
11: '#FF5', | ||
12: '#55F', | ||
13: '#F5F', | ||
14: '#5FF', | ||
15: '#FFF' | ||
}; | ||
range(0, 5).forEach(function (red) { | ||
range(0, 5).forEach(function (green) { | ||
range(0, 5).forEach(function (blue) { | ||
return setStyleColor(red, green, blue, colors); | ||
}); | ||
}); | ||
range(0, 23).forEach(function (gray) { | ||
var c = gray + 232; | ||
var l = toHexString(gray * 10 + 8); | ||
colors[c] = '#' + l + l + l; | ||
}); | ||
return colors; | ||
}); | ||
range(0, 23).forEach(function (gray) { | ||
var c = gray + 232; | ||
var l = toHexString(gray * 10 + 8); | ||
colors[c] = '#' + l + l + l; | ||
}); | ||
return colors; | ||
} | ||
/** | ||
@@ -57,11 +59,11 @@ * @param {number} red | ||
*/ | ||
function setStyleColor(red, green, blue, colors) { | ||
var c = 16 + red * 36 + green * 6 + blue; | ||
var r = red > 0 ? red * 40 + 55 : 0; | ||
var g = green > 0 ? green * 40 + 55 : 0; | ||
var b = blue > 0 ? blue * 40 + 55 : 0; | ||
colors[c] = toColorHexString([r, g, b]); | ||
var c = 16 + red * 36 + green * 6 + blue; | ||
var r = red > 0 ? red * 40 + 55 : 0; | ||
var g = green > 0 ? green * 40 + 55 : 0; | ||
var b = blue > 0 ? blue * 40 + 55 : 0; | ||
colors[c] = toColorHexString([r, g, b]); | ||
} | ||
/** | ||
@@ -72,12 +74,13 @@ * Converts from a number like 15 to a hex string like 'F' | ||
*/ | ||
function toHexString(num) { | ||
var str = num.toString(16); | ||
var str = num.toString(16); | ||
while (str.length < 2) { | ||
str = '0' + str; | ||
} | ||
while (str.length < 2) { | ||
str = '0' + str; | ||
} | ||
return str; | ||
return str; | ||
} | ||
/** | ||
@@ -88,12 +91,32 @@ * Converts from an array of numbers like [15, 15, 15] to a hex string like 'FFF' | ||
*/ | ||
function toColorHexString(ref) { | ||
var results = []; | ||
var results = []; | ||
var _iteratorNormalCompletion = true; | ||
var _didIteratorError = false; | ||
var _iteratorError = undefined; | ||
for (var j = 0, len = ref.length; j < len; j++) { | ||
results.push(toHexString(ref[j])); | ||
try { | ||
for (var _iterator = ref[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var r = _step.value; | ||
results.push(toHexString(r)); | ||
} | ||
} catch (err) { | ||
_didIteratorError = true; | ||
_iteratorError = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion && _iterator["return"] != null) { | ||
_iterator["return"](); | ||
} | ||
} finally { | ||
if (_didIteratorError) { | ||
throw _iteratorError; | ||
} | ||
} | ||
} | ||
return '#' + results.join(''); | ||
return '#' + results.join(''); | ||
} | ||
/** | ||
@@ -105,18 +128,19 @@ * @param {Array} stack | ||
*/ | ||
function generateOutput(stack, token, data, options) { | ||
var result; | ||
var result; | ||
if (token === 'text') { | ||
result = pushText(data, options); | ||
} else if (token === 'display') { | ||
result = handleDisplay(stack, data, options); | ||
} else if (token === 'xterm256') { | ||
result = pushForegroundColor(stack, options.colors[data]); | ||
} else if (token === 'rgb') { | ||
result = handleRgb(stack, data); | ||
} | ||
if (token === 'text') { | ||
result = pushText(data, options); | ||
} else if (token === 'display') { | ||
result = handleDisplay(stack, data, options); | ||
} else if (token === 'xterm256') { | ||
result = pushForegroundColor(stack, options.colors[data]); | ||
} else if (token === 'rgb') { | ||
result = handleRgb(stack, data); | ||
} | ||
return result; | ||
return result; | ||
} | ||
/** | ||
@@ -127,14 +151,13 @@ * @param {Array} stack | ||
*/ | ||
function handleRgb(stack, data) { | ||
data = data.substring(2).slice(0, -1); | ||
var operation = +data.substr(0, 2); | ||
var color = data.substring(5).split(';'); | ||
var rgb = color.map(function (value) { | ||
return ('0' + Number(value).toString(16)).substr(-2); | ||
}).join(''); | ||
return pushStyle(stack, (operation === 38 ? 'color:#' : 'background-color:#') + rgb); | ||
function handleRgb(stack, data) { | ||
data = data.substring(2).slice(0, -1); | ||
var operation = +data.substr(0, 2); | ||
var color = data.substring(5).split(';'); | ||
var rgb = color.map(function (value) { | ||
return ('0' + Number(value).toString(16)).substr(-2); | ||
}).join(''); | ||
return pushStyle(stack, (operation === 38 ? 'color:#' : 'background-color:#') + rgb); | ||
} | ||
/** | ||
@@ -146,65 +169,65 @@ * @param {Array} stack | ||
*/ | ||
function handleDisplay(stack, code, options) { | ||
code = parseInt(code, 10); | ||
var result; | ||
var codeMap = { | ||
'-1': function _() { | ||
return '<br/>'; | ||
}, | ||
0: function _() { | ||
return stack.length && resetStyles(stack); | ||
}, | ||
1: function _() { | ||
return pushTag(stack, 'b'); | ||
}, | ||
3: function _() { | ||
return pushTag(stack, 'i'); | ||
}, | ||
4: function _() { | ||
return pushTag(stack, 'u'); | ||
}, | ||
8: function _() { | ||
return pushStyle(stack, 'display:none'); | ||
}, | ||
9: function _() { | ||
return pushTag(stack, 'strike'); | ||
}, | ||
22: function _() { | ||
return pushStyle(stack, 'font-weight:normal;text-decoration:none;font-style:normal'); | ||
}, | ||
23: function _() { | ||
return closeTag(stack, 'i'); | ||
}, | ||
24: function _() { | ||
return closeTag(stack, 'u'); | ||
}, | ||
39: function _() { | ||
return pushForegroundColor(stack, options.fg); | ||
}, | ||
49: function _() { | ||
return pushBackgroundColor(stack, options.bg); | ||
}, | ||
53: function _() { | ||
return pushStyle(stack, 'text-decoration:overline'); | ||
} | ||
}; | ||
if (codeMap[code]) { | ||
result = codeMap[code](); | ||
} else if (4 < code && code < 7) { | ||
result = pushTag(stack, 'blink'); | ||
} else if (29 < code && code < 38) { | ||
result = pushForegroundColor(stack, options.colors[code - 30]); | ||
} else if (39 < code && code < 48) { | ||
result = pushBackgroundColor(stack, options.colors[code - 40]); | ||
} else if (89 < code && code < 98) { | ||
result = pushForegroundColor(stack, options.colors[8 + (code - 90)]); | ||
} else if (99 < code && code < 108) { | ||
result = pushBackgroundColor(stack, options.colors[8 + (code - 100)]); | ||
function handleDisplay(stack, code, options) { | ||
code = parseInt(code, 10); | ||
var codeMap = { | ||
'-1': function _() { | ||
return '<br/>'; | ||
}, | ||
0: function _() { | ||
return stack.length && resetStyles(stack); | ||
}, | ||
1: function _() { | ||
return pushTag(stack, 'b'); | ||
}, | ||
3: function _() { | ||
return pushTag(stack, 'i'); | ||
}, | ||
4: function _() { | ||
return pushTag(stack, 'u'); | ||
}, | ||
8: function _() { | ||
return pushStyle(stack, 'display:none'); | ||
}, | ||
9: function _() { | ||
return pushTag(stack, 'strike'); | ||
}, | ||
22: function _() { | ||
return pushStyle(stack, 'font-weight:normal;text-decoration:none;font-style:normal'); | ||
}, | ||
23: function _() { | ||
return closeTag(stack, 'i'); | ||
}, | ||
24: function _() { | ||
return closeTag(stack, 'u'); | ||
}, | ||
39: function _() { | ||
return pushForegroundColor(stack, options.fg); | ||
}, | ||
49: function _() { | ||
return pushBackgroundColor(stack, options.bg); | ||
}, | ||
53: function _() { | ||
return pushStyle(stack, 'text-decoration:overline'); | ||
} | ||
}; | ||
var result; | ||
return result; | ||
if (codeMap[code]) { | ||
result = codeMap[code](); | ||
} else if (4 < code && code < 7) { | ||
result = pushTag(stack, 'blink'); | ||
} else if (29 < code && code < 38) { | ||
result = pushForegroundColor(stack, options.colors[code - 30]); | ||
} else if (39 < code && code < 48) { | ||
result = pushBackgroundColor(stack, options.colors[code - 40]); | ||
} else if (89 < code && code < 98) { | ||
result = pushForegroundColor(stack, options.colors[8 + (code - 90)]); | ||
} else if (99 < code && code < 108) { | ||
result = pushBackgroundColor(stack, options.colors[8 + (code - 100)]); | ||
} | ||
return result; | ||
} | ||
/** | ||
@@ -214,12 +237,11 @@ * Clear all the styles | ||
*/ | ||
function resetStyles(stack) { | ||
var stackClone = stack.slice(0); | ||
stack.length = 0; | ||
return stackClone.reverse().map(function (tag) { | ||
return '</' + tag + '>'; | ||
}).join(''); | ||
function resetStyles(stack) { | ||
var stackClone = stack.slice(0); | ||
stack.length = 0; | ||
return stackClone.reverse().map(function (tag) { | ||
return '</' + tag + '>'; | ||
}).join(''); | ||
} | ||
/** | ||
@@ -232,12 +254,13 @@ * Creates an array of numbers ranging from low to high | ||
*/ | ||
function range(low, high) { | ||
var results = []; | ||
var results = []; | ||
for (var j = low; j <= high; j++) { | ||
results.push(j); | ||
} | ||
for (var j = low; j <= high; j++) { | ||
results.push(j); | ||
} | ||
return results; | ||
return results; | ||
} | ||
/** | ||
@@ -248,8 +271,9 @@ * Returns a new function that is true if value is NOT the same category | ||
*/ | ||
function notCategory(category) { | ||
return function (e) { | ||
return (category === null || e.category !== category) && category !== 'all'; | ||
}; | ||
return function (e) { | ||
return (category === null || e.category !== category) && category !== 'all'; | ||
}; | ||
} | ||
/** | ||
@@ -260,27 +284,28 @@ * Converts a code into an ansi token type | ||
*/ | ||
function categoryForCode(code) { | ||
code = parseInt(code, 10); | ||
var result = null; | ||
code = parseInt(code, 10); | ||
var result = null; | ||
if (code === 0) { | ||
result = 'all'; | ||
} else if (code === 1) { | ||
result = 'bold'; | ||
} else if (2 < code && code < 5) { | ||
result = 'underline'; | ||
} else if (4 < code && code < 7) { | ||
result = 'blink'; | ||
} else if (code === 8) { | ||
result = 'hide'; | ||
} else if (code === 9) { | ||
result = 'strike'; | ||
} else if (29 < code && code < 38 || code === 39 || 89 < code && code < 98) { | ||
result = 'foreground-color'; | ||
} else if (39 < code && code < 48 || code === 49 || 99 < code && code < 108) { | ||
result = 'background-color'; | ||
} | ||
if (code === 0) { | ||
result = 'all'; | ||
} else if (code === 1) { | ||
result = 'bold'; | ||
} else if (2 < code && code < 5) { | ||
result = 'underline'; | ||
} else if (4 < code && code < 7) { | ||
result = 'blink'; | ||
} else if (code === 8) { | ||
result = 'hide'; | ||
} else if (code === 9) { | ||
result = 'strike'; | ||
} else if (29 < code && code < 38 || code === 39 || 89 < code && code < 98) { | ||
result = 'foreground-color'; | ||
} else if (39 < code && code < 48 || code === 49 || 99 < code && code < 108) { | ||
result = 'background-color'; | ||
} | ||
return result; | ||
return result; | ||
} | ||
/** | ||
@@ -291,10 +316,11 @@ * @param {string} text | ||
*/ | ||
function pushText(text, options) { | ||
if (options.escapeXML) { | ||
return entities.encodeXML(text); | ||
} | ||
if (options.escapeXML) { | ||
return entities.encodeXML(text); | ||
} | ||
return text; | ||
return text; | ||
} | ||
/** | ||
@@ -306,12 +332,12 @@ * @param {Array} stack | ||
*/ | ||
function pushTag(stack, tag, style) { | ||
if (!style) { | ||
style = ''; | ||
} | ||
if (!style) { | ||
style = ''; | ||
} | ||
stack.push(tag); | ||
return ['<' + tag, style ? ' style="' + style + '"' : void 0, '>'].join(''); | ||
stack.push(tag); | ||
return ['<' + tag, style ? ' style="' + style + '"' : void 0, '>'].join(''); | ||
} | ||
/** | ||
@@ -322,14 +348,15 @@ * @param {Array} stack | ||
*/ | ||
function pushStyle(stack, style) { | ||
return pushTag(stack, 'span', style); | ||
return pushTag(stack, 'span', style); | ||
} | ||
function pushForegroundColor(stack, color) { | ||
return pushTag(stack, 'span', 'color:' + color); | ||
return pushTag(stack, 'span', 'color:' + color); | ||
} | ||
function pushBackgroundColor(stack, color) { | ||
return pushTag(stack, 'span', 'background-color:' + color); | ||
return pushTag(stack, 'span', 'background-color:' + color); | ||
} | ||
/** | ||
@@ -340,14 +367,15 @@ * @param {Array} stack | ||
*/ | ||
function closeTag(stack, style) { | ||
var last; | ||
var last; | ||
if (stack.slice(-1)[0] === style) { | ||
last = stack.pop(); | ||
} | ||
if (stack.slice(-1)[0] === style) { | ||
last = stack.pop(); | ||
} | ||
if (last) { | ||
return '</' + style + '>'; | ||
} | ||
if (last) { | ||
return '</' + style + '>'; | ||
} | ||
} | ||
/** | ||
@@ -359,137 +387,155 @@ * @param {string} text | ||
*/ | ||
function tokenize(text, options, callback) { | ||
var ansiMatch = false; | ||
var ansiHandler = 3; | ||
var ansiMatch = false; | ||
var ansiHandler = 3; | ||
function remove() { | ||
return ''; | ||
} | ||
function remove() { | ||
return ''; | ||
} | ||
function removeXterm256(m, g1) { | ||
callback('xterm256', g1); | ||
return ''; | ||
} | ||
function removeXterm256(m, g1) { | ||
callback('xterm256', g1); | ||
return ''; | ||
} | ||
function newline(m) { | ||
if (options.newline) { | ||
callback('display', -1); | ||
} else { | ||
callback('text', m); | ||
} | ||
return ''; | ||
function newline(m) { | ||
if (options.newline) { | ||
callback('display', -1); | ||
} else { | ||
callback('text', m); | ||
} | ||
function ansiMess(m, g1) { | ||
ansiMatch = true; | ||
if (g1.trim().length === 0) { | ||
g1 = '0'; | ||
} | ||
return ''; | ||
} | ||
g1 = g1.trimRight(';').split(';'); | ||
function ansiMess(m, g1) { | ||
ansiMatch = true; | ||
for (var o = 0, len = g1.length; o < len; o++) { | ||
callback('display', g1[o]); | ||
} | ||
return ''; | ||
if (g1.trim().length === 0) { | ||
g1 = '0'; | ||
} | ||
function realText(m) { | ||
callback('text', m); | ||
g1 = g1.trimRight(';').split(';'); | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
return ''; | ||
try { | ||
for (var _iterator2 = g1[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var g = _step2.value; | ||
callback('display', g); | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2["return"] != null) { | ||
_iterator2["return"](); | ||
} | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
} | ||
function rgb(m) { | ||
callback('rgb', m); | ||
return ''; | ||
} | ||
return ''; | ||
} | ||
function realText(m) { | ||
callback('text', m); | ||
return ''; | ||
} | ||
/* eslint no-control-regex:0 */ | ||
var tokens = [{ | ||
pattern: /^\x08+/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[[012]?K/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[\(B/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[[34]8;2;\d+;\d+;\d+m/, | ||
sub: rgb | ||
}, { | ||
pattern: /^\x1b\[38;5;(\d+)m/, | ||
sub: removeXterm256 | ||
}, { | ||
pattern: /^\n/, | ||
sub: newline | ||
}, { | ||
pattern: /^\x1b\[((?:\d{1,3};?)+|)m/, | ||
sub: ansiMess | ||
}, { | ||
// CSI n J | ||
// ED - Erase in Display Clears part of the screen. | ||
// If n is 0 (or missing), clear from cursor to end of screen. | ||
// If n is 1, clear from cursor to beginning of the screen. | ||
// If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). | ||
// If n is 3, clear entire screen and delete all lines saved in the scrollback buffer | ||
// (this feature was added for xterm and is supported by other terminal applications). | ||
pattern: /^\x1b\[\d?J/, | ||
sub: remove | ||
}, { | ||
// CSI n ; m f | ||
// HVP - Horizontal Vertical Position Same as CUP | ||
pattern: /^\x1b\[\d{0,3};\d{0,3}f/, | ||
sub: remove | ||
}, { | ||
// catch-all for CSI sequences? | ||
pattern: /^\x1b\[?[\d;]{0,3}/, | ||
sub: remove | ||
}, { | ||
pattern: /^([^\x1b\x08\n]+)/, | ||
sub: realText | ||
}]; | ||
function rgb(m) { | ||
callback('rgb', m); | ||
return ''; | ||
} | ||
/* eslint no-control-regex:0 */ | ||
function process(handler, i) { | ||
if (i > ansiHandler && ansiMatch) { | ||
return; | ||
} | ||
ansiMatch = false; | ||
var tokens = [{ | ||
pattern: /^\x08+/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[[012]?K/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[\(B/, | ||
sub: remove | ||
}, { | ||
pattern: /^\x1b\[[34]8;2;\d+;\d+;\d+m/, | ||
sub: rgb | ||
}, { | ||
pattern: /^\x1b\[38;5;(\d+)m/, | ||
sub: removeXterm256 | ||
}, { | ||
pattern: /^\n/, | ||
sub: newline | ||
}, { | ||
pattern: /^\x1b\[((?:\d{1,3};?)+|)m/, | ||
sub: ansiMess | ||
}, { | ||
// CSI n J | ||
// ED - Erase in Display Clears part of the screen. | ||
// If n is 0 (or missing), clear from cursor to end of screen. | ||
// If n is 1, clear from cursor to beginning of the screen. | ||
// If n is 2, clear entire screen (and moves cursor to upper left on DOS ANSI.SYS). | ||
// If n is 3, clear entire screen and delete all lines saved in the scrollback buffer | ||
// (this feature was added for xterm and is supported by other terminal applications). | ||
pattern: /^\x1b\[\d?J/, | ||
sub: remove | ||
}, { | ||
// CSI n ; m f | ||
// HVP - Horizontal Vertical Position Same as CUP | ||
pattern: /^\x1b\[\d{0,3};\d{0,3}f/, | ||
sub: remove | ||
}, { | ||
// catch-all for CSI sequences? | ||
pattern: /^\x1b\[?[\d;]{0,3}/, | ||
sub: remove | ||
}, { | ||
pattern: /^([^\x1b\x08\n]+)/, | ||
sub: realText | ||
}]; | ||
text = text.replace(handler.pattern, handler.sub); | ||
function process(handler, i) { | ||
if (i > ansiHandler && ansiMatch) { | ||
return; | ||
} | ||
var handler; | ||
var results1 = []; | ||
var length = text.length; | ||
ansiMatch = false; | ||
text = text.replace(handler.pattern, handler.sub); | ||
} | ||
outer: while (length > 0) { | ||
for (var i = 0, o = 0, len = tokens.length; o < len; i = ++o) { | ||
handler = tokens[i]; | ||
process(handler, i); | ||
var results1 = []; | ||
var _text = text, | ||
length = _text.length; | ||
if (text.length !== length) { | ||
// We matched a token and removed it from the text. We need to | ||
// start matching *all* tokens against the new text. | ||
length = text.length; | ||
continue outer; | ||
} | ||
} | ||
outer: while (length > 0) { | ||
for (var i = 0, o = 0, len = tokens.length; o < len; i = ++o) { | ||
var handler = tokens[i]; | ||
process(handler, i); | ||
if (text.length === length) { | ||
break; | ||
} else { | ||
results1.push(0); | ||
} | ||
if (text.length !== length) { | ||
// We matched a token and removed it from the text. We need to | ||
// start matching *all* tokens against the new text. | ||
length = text.length; | ||
continue outer; | ||
} | ||
} | ||
return results1; | ||
if (text.length === length) { | ||
break; | ||
} | ||
results1.push(0); | ||
length = text.length; | ||
} | ||
return results1; | ||
} | ||
/** | ||
@@ -503,60 +549,87 @@ * If streaming, then the stack is "sticky" | ||
*/ | ||
function updateStickyStack(stickyStack, token, data) { | ||
if (token !== 'text') { | ||
stickyStack = stickyStack.filter(notCategory(categoryForCode(data))); | ||
stickyStack.push({ token: token, data: data, category: categoryForCode(data) }); | ||
} | ||
if (token !== 'text') { | ||
stickyStack = stickyStack.filter(notCategory(categoryForCode(data))); | ||
stickyStack.push({ | ||
token: token, | ||
data: data, | ||
category: categoryForCode(data) | ||
}); | ||
} | ||
return stickyStack; | ||
return stickyStack; | ||
} | ||
function Filter(options) { | ||
var Filter = | ||
/*#__PURE__*/ | ||
function () { | ||
/** | ||
* @param {object} options | ||
* @param {string=} options.fg The default foreground color used when reset color codes are encountered. | ||
* @param {string=} options.bg The default background color used when reset color codes are encountered. | ||
* @param {boolean=} options.newline Convert newline characters to `<br/>`. | ||
* @param {boolean=} options.escapeXML Generate HTML/XML entities. | ||
* @param {boolean=} options.stream Save style state across invocations of `toHtml()`. | ||
* @param {(string[] | {[code: number]: string})=} options.colors Can override specific colors or the entire ANSI palette. | ||
*/ | ||
function Filter(options) { | ||
_classCallCheck(this, Filter); | ||
options = options || {}; | ||
if (options.colors) { | ||
options.colors = Object.assign({}, defaults.colors, options.colors); | ||
options.colors = Object.assign({}, defaults.colors, options.colors); | ||
} | ||
this.opts = Object.assign({}, defaults, options); | ||
this.options = Object.assign({}, defaults, options); | ||
this.stack = []; | ||
this.stickyStack = []; | ||
} | ||
} | ||
/** | ||
* @param {string | string[]} input | ||
* @returns {string} | ||
*/ | ||
Filter.prototype = { | ||
toHtml: function toHtml(input) { | ||
var _this = this; | ||
input = typeof input === 'string' ? [input] : input; | ||
var stack = this.stack; | ||
var options = this.opts; | ||
var buf = []; | ||
_createClass(Filter, [{ | ||
key: "toHtml", | ||
value: function toHtml(input) { | ||
var _this = this; | ||
this.stickyStack.forEach(function (element) { | ||
var output = generateOutput(stack, element.token, element.data, options); | ||
input = typeof input === 'string' ? [input] : input; | ||
var stack = this.stack, | ||
options = this.options; | ||
var buf = []; | ||
this.stickyStack.forEach(function (element) { | ||
var output = generateOutput(stack, element.token, element.data, options); | ||
if (output) { | ||
buf.push(output); | ||
} | ||
}); | ||
if (output) { | ||
buf.push(output); | ||
} | ||
}); | ||
tokenize(input.join(''), options, function (token, data) { | ||
var output = generateOutput(stack, token, data, options); | ||
tokenize(input.join(''), options, function (token, data) { | ||
var output = generateOutput(stack, token, data, options); | ||
if (output) { | ||
buf.push(output); | ||
} | ||
if (output) { | ||
buf.push(output); | ||
} | ||
if (options.stream) { | ||
_this.stickyStack = updateStickyStack(_this.stickyStack, token, data); | ||
} | ||
}); | ||
if (options.stream) { | ||
_this.stickyStack = updateStickyStack(_this.stickyStack, token, data); | ||
} | ||
}); | ||
if (stack.length) { | ||
buf.push(resetStyles(stack)); | ||
} | ||
if (stack.length) { | ||
buf.push(resetStyles(stack)); | ||
} | ||
return buf.join(''); | ||
return buf.join(''); | ||
} | ||
}; | ||
}]); | ||
return Filter; | ||
}(); | ||
module.exports = Filter; |
105
lib/cli.js
@@ -1,49 +0,55 @@ | ||
'use strict'; | ||
"use strict"; | ||
/* eslint no-console:0 */ | ||
var help = '\nuasge: ansi-to-html [options] [file]\n \nfile: The file to display or stdin\n \noptions: \n \n -f, --fg The background color used for resets (#000)\n -b, --bg The foreground color used for resets (#FFF)\n -n, --newline Convert newline characters to <br/> (false)\n -x, --escapeXML Generate XML entities (false)\n -v, --version Print version\n -h, --help Print help\n '; | ||
var args = { | ||
stream: true | ||
}; | ||
var file = null, | ||
skip = false, | ||
args = { | ||
stream: true | ||
}; | ||
skip = false; | ||
for (var i = 2, j = 2, ref = process.argv.length; 2 <= ref ? j < ref : j > ref; i = 2 <= ref ? ++j : --j) { | ||
if (skip) { | ||
skip = false; | ||
continue; | ||
} | ||
if (skip) { | ||
skip = false; | ||
continue; | ||
} | ||
switch (process.argv[i]) { | ||
case '-n': | ||
case '--newline': | ||
args.newline = true; | ||
break; | ||
case '-x': | ||
case '--escapeXML': | ||
args.escapeXML = true; | ||
break; | ||
case '-f': | ||
case '--fg': | ||
args.fg = process.argv[i + 1]; | ||
skip = true; | ||
break; | ||
case '-b': | ||
case '--bg': | ||
args.bg = process.argv[i + 1]; | ||
skip = true; | ||
break; | ||
case '-v': | ||
case '--version': | ||
console.log(require(__dirname + '/../package.json').version); | ||
process.exit(0); | ||
break; | ||
case '-h': | ||
case '--help': | ||
console.log(help); | ||
process.exit(0); | ||
break; | ||
default: | ||
file = process.argv[i]; | ||
} | ||
switch (process.argv[i]) { | ||
case '-n': | ||
case '--newline': | ||
args.newline = true; | ||
break; | ||
case '-x': | ||
case '--escapeXML': | ||
args.escapeXML = true; | ||
break; | ||
case '-f': | ||
case '--fg': | ||
args.fg = process.argv[i + 1]; | ||
skip = true; | ||
break; | ||
case '-b': | ||
case '--bg': | ||
args.bg = process.argv[i + 1]; | ||
skip = true; | ||
break; | ||
case '-v': | ||
case '--version': | ||
console.log(require(__dirname + '/../package.json').version); | ||
process.exit(0); | ||
break; | ||
case '-h': | ||
case '--help': | ||
console.log(help); | ||
process.exit(0); | ||
break; | ||
default: | ||
file = process.argv[i]; | ||
} | ||
} | ||
@@ -54,13 +60,16 @@ | ||
var htmlStream = function htmlStream(stream) { | ||
return stream.on('data', function (chunk) { | ||
return process.stdout.write(convert.toHtml(chunk)); | ||
}); | ||
return stream.on('data', function (chunk) { | ||
return process.stdout.write(convert.toHtml(chunk)); | ||
}); | ||
}; | ||
if (file) { | ||
var stream = require('fs').createReadStream(file, { encoding: 'utf8' }); | ||
htmlStream(stream); | ||
var stream = require('fs').createReadStream(file, { | ||
encoding: 'utf8' | ||
}); | ||
htmlStream(stream); | ||
} else { | ||
process.stdin.setEncoding('utf8'); | ||
htmlStream(process.stdin); | ||
process.stdin.setEncoding('utf8'); | ||
htmlStream(process.stdin); | ||
} |
{ | ||
"name": "ansi-to-html", | ||
"version": "0.6.11", | ||
"version": "0.6.12", | ||
"description": "Convert ansi escaped text streams to html.", | ||
@@ -10,5 +10,5 @@ "main": "lib/ansi_to_html.js", | ||
"scripts": { | ||
"build": "./node_modules/.bin/babel src --out-dir lib", | ||
"lint": "./node_modules/.bin/eslint src test", | ||
"test": "./node_modules/.bin/mocha --reporter tap" | ||
"build": "babel src --out-dir lib", | ||
"lint": "eslint src test", | ||
"test": "mocha --reporter tap" | ||
}, | ||
@@ -19,2 +19,3 @@ "repository": { | ||
}, | ||
"homepage": "https://github.com/rburns/ansi-to-html", | ||
"bugs": { | ||
@@ -60,2 +61,10 @@ "url": "https://github.com/rburns/ansi-to-html/issues" | ||
"url": "https://github.com/NeeEoo" | ||
}, | ||
{ | ||
"name": "Brett Zamir", | ||
"url": "https://github.com/brettz9" | ||
}, | ||
{ | ||
"name": "Piotr Monwid-Olechnowicz", | ||
"url": "https://github.com/hasparus" | ||
} | ||
@@ -65,10 +74,11 @@ ], | ||
"devDependencies": { | ||
"babel-cli": "^6.24.1", | ||
"babel-preset-env": "^1.4.0", | ||
"chai": "~3.5.0", | ||
"eslint": "^3.13.1", | ||
"mocha": "^3.2.0" | ||
"@babel/cli": "^7.4.4", | ||
"@babel/core": "^7.4.5", | ||
"@babel/preset-env": "^7.4.5", | ||
"chai": "~4.2.0", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4" | ||
}, | ||
"dependencies": { | ||
"entities": "^1.1.1" | ||
"entities": "^1.1.2" | ||
}, | ||
@@ -80,5 +90,5 @@ "bin": { | ||
"presets": [ | ||
"env" | ||
"@babel/env" | ||
] | ||
} | ||
} |
@@ -8,31 +8,38 @@ ## Ansi to Html | ||
This was originally a port of the ansi to html converter from | ||
[bcat](https://github.com/rtomayko/bcat/blob/master/lib/bcat/ansi.rb) to Javascript. It has since | ||
undergone quite a lot of modification. | ||
[bcat](https://github.com/rtomayko/bcat/blob/master/lib/bcat/ansi.rb) to | ||
JavaScript. It has since undergone quite a lot of modification. | ||
It has a few additions: | ||
* The API has been altered to accept options in the constructor, and input in <code>toHtml()</code>. | ||
* The API has been altered to accept options in the constructor, and input in `toHtml()`. | ||
* ANSI codes for setting the foreground or background color to default are handled | ||
* the 'erase in line' escape code (\x1b[K) is dropped from the output. | ||
* the 'erase in line' escape code (`\x1b[K`) is dropped from the output. | ||
## Installation | ||
npm install ansi-to-html | ||
```bash | ||
npm install ansi-to-html | ||
``` | ||
## Usage | ||
var Convert = require('ansi-to-html'); | ||
var convert = new Convert(); | ||
```javascript | ||
var Convert = require('ansi-to-html'); | ||
var convert = new Convert(); | ||
console.log(convert.toHtml('\x1b[30mblack\x1b[37mwhite')); | ||
console.log(convert.toHtml('\x1b[30mblack\x1b[37mwhite')); | ||
/* | ||
prints: | ||
<span style="color:#000">black<span style="color:#AAA">white</span></span> | ||
*/ | ||
/* | ||
prints: | ||
<span style="color:#000">black<span style="color:#AAA">white</span></span> | ||
*/ | ||
``` | ||
## Command line usage | ||
Process a file: | ||
When using ansi-to-html from the command line the stream option is set to `true`. | ||
Other options can be provided. See `ansi-to-html -h` for more detail. | ||
### Process a file | ||
```bash | ||
@@ -42,3 +49,3 @@ ansi-to-html the_filename | ||
From STDIN: | ||
### From STDIN | ||
@@ -49,5 +56,2 @@ ```bash | ||
When using ansi-to-html from the command line the stream option is set to `true`. Other options can | ||
be provided. See `ansi-to-html -h` for more detail. | ||
## Options | ||
@@ -57,13 +61,13 @@ | ||
**fg** <code>CSS color values</code> The default foreground color used when reset color codes are encountered. | ||
**fg** `<CSS color values>`. The default foreground color used when reset color codes are encountered. | ||
**bg** <code>CSS color values</code> The default background color used when reset color codes are encountered. | ||
**bg** `<CSS color values>`. The default background color used when reset color codes are encountered. | ||
**newline** <code>true or false</code> Convert newline characters to <code><br/></code>. | ||
**newline** `true` or `false`. Convert newline characters to `<br/>`. | ||
**escapeXML** <code>true or false</code> Generate HTML/XML entities. | ||
**escapeXML** `true` or `false`. Generate HTML/XML entities. | ||
**stream** <code>true or false</code> save style state across invocations of toHtml(). | ||
**stream** `true` or `false`. Save style state across invocations of `toHtml()`. | ||
**colors** <code>Object/Array with values 0 - 255 containing CSS color values</code> Can override specific colors or the entire ANSI palette | ||
**colors** `Object`/`Array` (with values 0 - 255 containing CSS color values). Can override specific colors or the entire ANSI palette | ||
@@ -84,20 +88,25 @@ ### Default options | ||
[![](http://issuestats.com/github/rburns/ansi-to-html/badge/issue?style=flat)](http://issuestats.com/github/rburns/ansi-to-html) | ||
[![](http://issuestats.com/github/rburns/ansi-to-html/badge/pr?style=flat)](http://issuestats.com/github/rburns/ansi-to-html) | ||
Once you have the git repository cloned, install the dependencies: | ||
cd ansi-to-html | ||
npm install | ||
```bash | ||
cd ansi-to-html | ||
npm install | ||
``` | ||
Lint | ||
npm run lint | ||
```bash | ||
npm run lint | ||
``` | ||
Build | ||
npm run build | ||
```bash | ||
npm run build | ||
``` | ||
Test | ||
npm test | ||
```bash | ||
npm test | ||
``` |
@@ -1,5 +0,3 @@ | ||
/* globals describe, it*/ | ||
const {expect} = require('chai'); | ||
const Filter = require('../lib/ansi_to_html.js'); | ||
const expect = require('chai').expect; | ||
@@ -11,3 +9,3 @@ function test(text, result, done, opts) { | ||
var f = new Filter(opts); | ||
const f = new Filter(opts); | ||
@@ -14,0 +12,0 @@ function filtered(memo, t) { |
@@ -1,8 +0,5 @@ | ||
/* globals describe, it*/ | ||
const childProcess = require('child_process'); | ||
const expect = require('chai').expect; | ||
const {EOL} = require('os'); | ||
const {expect} = require('chai'); | ||
const EOL = require('os').EOL; | ||
function getColorCmd(cmd) { | ||
@@ -9,0 +6,0 @@ const cmds = { |
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
No website
QualityPackage does not have a website.
Found 1 instance in 1 package
38099
924
1
108
6
Updatedentities@^1.1.2