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-12-03 02:52:49 +00:00
|
|
|
const STRIKETHROUGH = "\x1e";
|
2017-12-03 15:00:35 +00:00
|
|
|
const MONOSPACE = "\x11";
|
2017-03-18 08:35:17 +00:00
|
|
|
|
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
|
2018-02-13 11:05:49 +00:00
|
|
|
// This regex allows line feed character
|
|
|
|
const controlCodesRx = /[\u0000-\u0009\u000B-\u001F]/g;
|
2017-03-18 08:35:17 +00:00
|
|
|
|
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
|
2017-12-04 21:12:37 +00:00
|
|
|
// information (`bold`, `textColor`, `bgcolor`, `italic`,
|
2017-12-03 15:00:35 +00:00
|
|
|
// `underline`, `strikethrough`, `monospace`), 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.
|
2019-07-17 09:33:59 +00:00
|
|
|
let colorCodes,
|
|
|
|
bold,
|
|
|
|
textColor,
|
|
|
|
bgColor,
|
|
|
|
hexColor,
|
|
|
|
hexBgColor,
|
|
|
|
italic,
|
|
|
|
underline,
|
|
|
|
strikethrough,
|
|
|
|
monospace;
|
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
|
|
|
italic = false;
|
|
|
|
underline = false;
|
2017-12-03 02:52:49 +00:00
|
|
|
strikethrough = false;
|
2017-12-03 15:00:35 +00:00
|
|
|
monospace = false;
|
2017-03-18 08:35:17 +00:00
|
|
|
};
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2017-03-18 08:35:17 +00:00
|
|
|
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
|
|
|
italic,
|
|
|
|
underline,
|
2017-12-03 02:52:49 +00:00
|
|
|
strikethrough,
|
2017-12-03 15:00:35 +00:00
|
|
|
monospace,
|
2017-04-04 04:36:03 +00:00
|
|
|
text: processedText,
|
|
|
|
start: fragmentStart,
|
2017-11-15 06:35:15 +00:00
|
|
|
end: fragmentStart + processedText.length,
|
2017-04-04 04:36:03 +00:00
|
|
|
});
|
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]) {
|
2019-07-17 09:33:59 +00:00
|
|
|
case RESET:
|
|
|
|
emitFragment();
|
|
|
|
resetStyle();
|
|
|
|
break;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
case BOLD:
|
|
|
|
emitFragment();
|
|
|
|
bold = !bold;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case COLOR:
|
|
|
|
emitFragment();
|
|
|
|
|
|
|
|
// Go one step further to find the corresponding color
|
|
|
|
colorCodes = text.slice(position + 1).match(colorRx);
|
|
|
|
|
|
|
|
if (colorCodes) {
|
|
|
|
textColor = Number(colorCodes[1]);
|
|
|
|
|
|
|
|
if (colorCodes[2]) {
|
|
|
|
bgColor = Number(colorCodes[2]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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).
|
|
|
|
textColor = undefined;
|
|
|
|
bgColor = undefined;
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
break;
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case HEX_COLOR:
|
|
|
|
emitFragment();
|
2017-03-18 08:35:17 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
colorCodes = text.slice(position + 1).match(hexColorRx);
|
2017-04-28 05:28:45 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (colorCodes) {
|
|
|
|
hexColor = colorCodes[1].toUpperCase();
|
2017-04-28 05:28:45 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
if (colorCodes[2]) {
|
|
|
|
hexBgColor = colorCodes[2].toUpperCase();
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +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)
|
|
|
|
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;
|
2017-04-28 05:28:45 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
break;
|
2017-04-28 05:28:45 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case REVERSE: {
|
|
|
|
emitFragment();
|
|
|
|
const tmp = bgColor;
|
|
|
|
bgColor = textColor;
|
|
|
|
textColor = tmp;
|
|
|
|
break;
|
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
case ITALIC:
|
|
|
|
emitFragment();
|
|
|
|
italic = !italic;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case UNDERLINE:
|
|
|
|
emitFragment();
|
|
|
|
underline = !underline;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case STRIKETHROUGH:
|
|
|
|
emitFragment();
|
|
|
|
strikethrough = !strikethrough;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case MONOSPACE:
|
|
|
|
emitFragment();
|
|
|
|
monospace = !monospace;
|
|
|
|
break;
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
const properties = [
|
|
|
|
"bold",
|
|
|
|
"textColor",
|
|
|
|
"bgColor",
|
|
|
|
"hexColor",
|
|
|
|
"hexBgColor",
|
|
|
|
"italic",
|
|
|
|
"underline",
|
|
|
|
"strikethrough",
|
|
|
|
"monospace",
|
|
|
|
];
|
2017-03-18 08:35:17 +00:00
|
|
|
|
|
|
|
function prepare(text) {
|
2019-07-17 09:33:59 +00:00
|
|
|
return (
|
|
|
|
parseStyle(text)
|
|
|
|
// 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-04-04 04:36:03 +00:00
|
|
|
}
|
2018-02-20 07:28:04 +00:00
|
|
|
|
2019-07-17 09:33:59 +00:00
|
|
|
return prev.concat([curr]);
|
|
|
|
}, [])
|
|
|
|
);
|
2017-03-18 08:35:17 +00:00
|
|
|
}
|
|
|
|
|
2019-11-16 17:24:03 +00:00
|
|
|
export default prepare;
|