2017-03-18 08:35:17 +00:00
|
|
|
"use strict";
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Styling control codes
|
2017-03-18 08:35:17 +00:00
|
|
|
const BOLD = "\x02";
|
|
|
|
const COLOR = "\x03";
|
2017-04-28 05:28:45 +00:00
|
|
|
const HEX_COLOR = "\x04";
|
2017-03-18 08:35:17 +00:00
|
|
|
const RESET = "\x0f";
|
|
|
|
const REVERSE = "\x16";
|
|
|
|
const ITALIC = "\x1d";
|
|
|
|
const UNDERLINE = "\x1f";
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Color code matcher, with format `XX,YY` where both `XX` and `YY` are
|
|
|
|
// integers, `XX` is the text color and `YY` is an optional background color.
|
2017-03-18 08:35:17 +00:00
|
|
|
const colorRx = /^(\d{1,2})(?:,(\d{1,2}))?/;
|
2017-04-04 04:36:03 +00:00
|
|
|
|
2017-04-28 05:28:45 +00:00
|
|
|
// 6-char Hex color code matcher
|
|
|
|
const hexColorRx = /^([0-9a-f]{6})(?:,([0-9a-f]{6}))?/i;
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Represents all other control codes that to be ignored/filtered from the text
|
2017-03-18 08:35:17 +00:00
|
|
|
const controlCodesRx = /[\u0000-\u001F]/g;
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Converts a given text into an array of objects, each of them representing a
|
|
|
|
// similarly styled section of the text. Each object carries the `text`, style
|
|
|
|
// information (`bold`, `textColor`, `bgcolor`, `reverse`, `italic`,
|
|
|
|
// `underline`), and `start`/`end` cursors.
|
2017-03-18 08:35:17 +00:00
|
|
|
function parseStyle(text) {
|
|
|
|
const result = [];
|
|
|
|
let start = 0;
|
|
|
|
let position = 0;
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// At any given time, these carry style information since last time a styling
|
|
|
|
// control code was met.
|
2017-04-28 05:28:45 +00:00
|
|
|
let colorCodes, bold, textColor, bgColor, hexColor, hexBgColor, reverse, italic, underline;
|
2017-03-18 08:35:17 +00:00
|
|
|
|
|
|
|
const resetStyle = () => {
|
|
|
|
bold = false;
|
|
|
|
textColor = undefined;
|
|
|
|
bgColor = undefined;
|
2017-04-28 05:28:45 +00:00
|
|
|
hexColor = undefined;
|
|
|
|
hexBgColor = undefined;
|
2017-03-18 08:35:17 +00:00
|
|
|
reverse = false;
|
|
|
|
italic = false;
|
|
|
|
underline = false;
|
|
|
|
};
|
|
|
|
resetStyle();
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// When called, this "closes" the current fragment by adding an entry to the
|
|
|
|
// `result` array using the styling information set last time a control code
|
|
|
|
// was met.
|
2017-03-18 08:35:17 +00:00
|
|
|
const emitFragment = () => {
|
2017-04-04 04:36:03 +00:00
|
|
|
// Uses the text fragment starting from the last control code position up to
|
|
|
|
// the current position
|
2017-03-18 08:35:17 +00:00
|
|
|
const textPart = text.slice(start, position);
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Filters out all non-style related control codes present in this text
|
2017-03-18 08:35:17 +00:00
|
|
|
const processedText = textPart.replace(controlCodesRx, "");
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
if (processedText.length) {
|
|
|
|
// Current fragment starts where the previous one ends, or at 0 if none
|
|
|
|
const fragmentStart = result.length ? result[result.length - 1].end : 0;
|
|
|
|
|
|
|
|
result.push({
|
|
|
|
bold,
|
|
|
|
textColor,
|
|
|
|
bgColor,
|
2017-04-28 05:28:45 +00:00
|
|
|
hexColor,
|
|
|
|
hexBgColor,
|
2017-04-04 04:36:03 +00:00
|
|
|
reverse,
|
|
|
|
italic,
|
|
|
|
underline,
|
|
|
|
text: processedText,
|
|
|
|
start: fragmentStart,
|
|
|
|
end: fragmentStart + processedText.length
|
|
|
|
});
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Now that a fragment has been "closed", the next one will start after that
|
|
|
|
start = position + 1;
|
2017-03-18 08:35:17 +00:00
|
|
|
};
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// This loop goes through each character of the given text one by one by
|
|
|
|
// bumping the `position` cursor. Every time a new special "styling" character
|
|
|
|
// is met, an object gets created (with `emitFragment()`)information on text
|
|
|
|
// encountered since the previous styling character.
|
2017-03-18 08:35:17 +00:00
|
|
|
while (position < text.length) {
|
|
|
|
switch (text[position]) {
|
|
|
|
|
|
|
|
case RESET:
|
|
|
|
emitFragment();
|
|
|
|
resetStyle();
|
|
|
|
break;
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Meeting a BOLD character means that the ongoing text is either going to
|
|
|
|
// be in bold or that the previous one was in bold and the following one
|
|
|
|
// must be reset.
|
|
|
|
// This same behavior applies to COLOR, REVERSE, ITALIC, and UNDERLINE.
|
2017-03-18 08:35:17 +00:00
|
|
|
case BOLD:
|
|
|
|
emitFragment();
|
|
|
|
bold = !bold;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COLOR:
|
|
|
|
emitFragment();
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// Go one step further to find the corresponding color
|
2017-03-18 08:35:17 +00:00
|
|
|
colorCodes = text.slice(position + 1).match(colorRx);
|
|
|
|
|
|
|
|
if (colorCodes) {
|
|
|
|
textColor = Number(colorCodes[1]);
|
2017-04-04 04:36:03 +00:00
|
|
|
if (colorCodes[2]) {
|
|
|
|
bgColor = Number(colorCodes[2]);
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2017-04-04 04:36:03 +00:00
|
|
|
// Color code length is > 1, so bump the current position cursor by as
|
|
|
|
// much (and reset the start cursor for the current text block as well)
|
2017-03-18 08:35:17 +00:00
|
|
|
position += colorCodes[0].length;
|
2017-04-04 04:36:03 +00:00
|
|
|
start = position + 1;
|
2017-03-18 08:35:17 +00:00
|
|
|
} else {
|
2017-04-04 04:36:03 +00:00
|
|
|
// If no color codes were found, toggles back to no colors (like BOLD).
|
2017-03-18 08:35:17 +00:00
|
|
|
textColor = undefined;
|
|
|
|
bgColor = undefined;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2017-04-28 05:28:45 +00:00
|
|
|
case HEX_COLOR:
|
|
|
|
emitFragment();
|
|
|
|
|
|
|
|
colorCodes = text.slice(position + 1).match(hexColorRx);
|
|
|
|
|
|
|
|
if (colorCodes) {
|
|
|
|
hexColor = colorCodes[1].toUpperCase();
|
|
|
|
if (colorCodes[2]) {
|
|
|
|
hexBgColor = colorCodes[2].toUpperCase();
|
|
|
|
}
|
|
|
|
// Color code length is > 1, so bump the current position cursor by as
|
|
|
|
// much (and reset the start cursor for the current text block as well)
|
|
|
|
position += colorCodes[0].length;
|
|
|
|
start = position + 1;
|
|
|
|
} else {
|
|
|
|
// If no color codes were found, toggles back to no colors (like BOLD).
|
|
|
|
hexColor = undefined;
|
|
|
|
hexBgColor = undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
case REVERSE:
|
|
|
|
emitFragment();
|
|
|
|
reverse = !reverse;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case ITALIC:
|
|
|
|
emitFragment();
|
|
|
|
italic = !italic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNDERLINE:
|
|
|
|
emitFragment();
|
|
|
|
underline = !underline;
|
|
|
|
break;
|
|
|
|
}
|
2017-04-04 04:36:03 +00:00
|
|
|
|
|
|
|
// Evaluate the next character at the next iteration
|
2017-03-18 08:35:17 +00:00
|
|
|
position += 1;
|
|
|
|
}
|
|
|
|
|
2017-04-04 04:36:03 +00:00
|
|
|
// The entire text has been parsed, so we finalize the current text fragment.
|
2017-03-18 08:35:17 +00:00
|
|
|
emitFragment();
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-04-28 05:28:45 +00:00
|
|
|
const properties = ["bold", "textColor", "bgColor", "hexColor", "hexBgColor", "italic", "underline", "reverse"];
|
2017-03-18 08:35:17 +00:00
|
|
|
|
|
|
|
function prepare(text) {
|
|
|
|
return parseStyle(text)
|
2017-04-04 04:36:03 +00:00
|
|
|
// This optimizes fragments by combining them together when all their values
|
|
|
|
// for the properties defined above are equal.
|
|
|
|
.reduce((prev, curr) => {
|
|
|
|
if (prev.length) {
|
|
|
|
const lastEntry = prev[prev.length - 1];
|
|
|
|
if (properties.every(key => curr[key] === lastEntry[key])) {
|
|
|
|
lastEntry.text += curr.text;
|
|
|
|
lastEntry.end += curr.text.length;
|
|
|
|
return prev;
|
|
|
|
}
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2017-04-04 04:36:03 +00:00
|
|
|
return prev.concat([curr]);
|
|
|
|
}, []);
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = prepare;
|