chalk/source/templates.js

135 lines
3.3 KiB
JavaScript
Raw Normal View History

2017-06-29 16:46:19 -07:00
'use strict';
const TEMPLATE_REGEX = /(?:\\(u(?:[a-f\d]{4}|\{[a-f\d]{1,6}\})|x[a-f\d]{2}|.))|(?:\{(~)?(\w+(?:\([^)]*\))?(?:\.\w+(?:\([^)]*\))?)*)(?:[ \t]|(?=\r?\n)))|(\})|((?:.|[\r\n\f])+?)/gi;
const STYLE_REGEX = /(?:^|\.)(\w+)(?:\(([^)]*)\))?/g;
const STRING_REGEX = /^(['"])((?:\\.|(?!\1)[^\\])*)\1$/;
2020-04-02 15:56:21 +08:00
const ESCAPE_REGEX = /\\(u(?:[a-f\d]{4}|{[a-f\d]{1,6}})|x[a-f\d]{2}|.)|([^\\])/gi;
const ESCAPES = new Map([
['n', '\n'],
['r', '\r'],
['t', '\t'],
['b', '\b'],
['f', '\f'],
['v', '\v'],
['0', '\0'],
['\\', '\\'],
['e', '\u001B'],
['a', '\u0007']
]);
2017-06-29 16:46:19 -07:00
function unescape(c) {
const u = c[0] === 'u';
const bracket = c[1] === '{';
if ((u && !bracket && c.length === 5) || (c[0] === 'x' && c.length === 3)) {
return String.fromCharCode(parseInt(c.slice(1), 16));
}
2017-06-29 16:46:19 -07:00
if (u && bracket) {
return String.fromCodePoint(parseInt(c.slice(2, -1), 16));
}
return ESCAPES.get(c) || c;
}
2017-06-29 16:46:19 -07:00
2019-03-12 20:11:31 +07:00
function parseArguments(name, arguments_) {
const results = [];
2019-03-12 20:11:31 +07:00
const chunks = arguments_.trim().split(/\s*,\s*/g);
let matches;
2017-06-29 16:46:19 -07:00
for (const chunk of chunks) {
2018-12-26 02:37:03 +01:00
const number = Number(chunk);
if (!Number.isNaN(number)) {
results.push(number);
} else if ((matches = chunk.match(STRING_REGEX))) {
2018-12-26 02:37:03 +01:00
results.push(matches[2].replace(ESCAPE_REGEX, (m, escape, character) => escape ? unescape(escape) : character));
2017-06-29 16:46:19 -07:00
} else {
throw new Error(`Invalid Chalk template style argument: ${chunk} (in style '${name}')`);
2017-06-29 16:46:19 -07:00
}
}
return results;
}
2017-06-29 16:46:19 -07:00
function parseStyle(style) {
STYLE_REGEX.lastIndex = 0;
2017-06-29 16:46:19 -07:00
const results = [];
let matches;
2017-06-29 16:46:19 -07:00
while ((matches = STYLE_REGEX.exec(style)) !== null) {
2019-03-12 20:11:31 +07:00
const name = matches[1];
2017-06-29 16:46:19 -07:00
if (matches[2]) {
const args = parseArguments(name, matches[2]);
results.push([name].concat(args));
2017-06-29 16:46:19 -07:00
} else {
results.push([name]);
2017-06-29 16:46:19 -07:00
}
}
return results;
}
2017-06-29 16:46:19 -07:00
function buildStyle(chalk, styles) {
const enabled = {};
2017-06-29 16:46:19 -07:00
for (const layer of styles) {
for (const style of layer.styles) {
enabled[style[0]] = layer.inverse ? null : style.slice(1);
}
}
2017-06-29 16:46:19 -07:00
let current = chalk;
2019-03-12 20:11:31 +07:00
for (const [styleName, styles] of Object.entries(enabled)) {
if (!Array.isArray(styles)) {
2018-12-26 02:37:03 +01:00
continue;
}
2018-12-26 02:37:03 +01:00
if (!(styleName in current)) {
throw new Error(`Unknown Chalk style: ${styleName}`);
}
2019-03-12 20:11:31 +07:00
current = styles.length > 0 ? current[styleName](...styles) : current[styleName];
2017-06-29 16:46:19 -07:00
}
return current;
2017-06-29 16:46:19 -07:00
}
2019-03-12 20:11:31 +07:00
module.exports = (chalk, temporary) => {
const styles = [];
const chunks = [];
let chunk = [];
// eslint-disable-next-line max-params
2019-03-12 20:11:31 +07:00
temporary.replace(TEMPLATE_REGEX, (m, escapeCharacter, inverse, style, close, character) => {
2018-12-26 02:37:03 +01:00
if (escapeCharacter) {
chunk.push(unescape(escapeCharacter));
} else if (style) {
2018-12-26 02:37:03 +01:00
const string = chunk.join('');
chunk = [];
2018-12-26 02:37:03 +01:00
chunks.push(styles.length === 0 ? string : buildStyle(chalk, styles)(string));
styles.push({inverse, styles: parseStyle(style)});
} else if (close) {
if (styles.length === 0) {
throw new Error('Found extraneous } in Chalk template literal');
}
chunks.push(buildStyle(chalk, styles)(chunk.join('')));
chunk = [];
styles.pop();
2017-06-29 16:46:19 -07:00
} else {
2018-12-26 02:37:03 +01:00
chunk.push(character);
2017-06-29 16:46:19 -07:00
}
});
2017-06-29 16:46:19 -07:00
chunks.push(chunk.join(''));
2017-06-29 16:46:19 -07:00
if (styles.length > 0) {
2020-04-02 15:56:21 +08:00
const errMessage = `Chalk template literal is missing ${styles.length} closing bracket${styles.length === 1 ? '' : 's'} (\`}\`)`;
throw new Error(errMessage);
2017-06-29 16:46:19 -07:00
}
return chunks.join('');
};