115 lines
3.9 KiB
JavaScript
115 lines
3.9 KiB
JavaScript
import { COLORS, COLOR_NAMES } from "../engine/catalog.js";
|
||
import { icon, colonySymbol, colors, displayColorName } from "./art.js";
|
||
import { geneText } from "./gene-label.js";
|
||
|
||
export const food = (p, n = 1) => {
|
||
const label =
|
||
`${n ?? ""} ${displayColorName(p, COLOR_NAMES[p]).toLowerCase()} nutrient${n === 1 ? "" : "s"}`.trim();
|
||
return `<span class="food-amount" role="img" aria-label="${label}" title="${label}"><span class="food ${COLORS[p]}" style="background:${colors[COLORS[p]].shade};border-color:${colors[COLORS[p]].stroke}" aria-hidden="true">${colonySymbol(COLORS[p])}</span>${n === null ? "" : `<span>${n}</span>`}</span>`;
|
||
};
|
||
const amount = (kind, n, label, symbol) =>
|
||
`<span class="resource-amount ${kind}-amount" role="img" aria-label="${label}" title="${label}">${icon(symbol)}<span>${n}</span></span>`;
|
||
export const bp = (n) =>
|
||
amount("bp", n, `${n} biological point${n === 1 ? "" : "s"}`, "bio");
|
||
export const injury = (n) =>
|
||
amount("injury", n, `${n} ${n === 1 ? "injury" : "injuries"}`, "injury");
|
||
export const sensitivity = (n) =>
|
||
amount("mutation", n, `${n} mutation sensitivity`, "sun");
|
||
export const evolutionPoints = (n) =>
|
||
amount(
|
||
"evolution",
|
||
n,
|
||
`${n} evolution point${Number(n) === 1 ? "" : "s"}`,
|
||
"spark",
|
||
);
|
||
|
||
const numbers = {
|
||
a: 1,
|
||
an: 1,
|
||
one: 1,
|
||
two: 2,
|
||
three: 3,
|
||
four: 4,
|
||
five: 5,
|
||
six: 6,
|
||
seven: 7,
|
||
eight: 8,
|
||
nine: 9,
|
||
ten: 10,
|
||
eleven: 11,
|
||
};
|
||
const quantity = `(?:[−-]?\\d+|${Object.keys(numbers).join("|")})`;
|
||
const colours = {
|
||
green: 0,
|
||
coral: 1,
|
||
red: 1,
|
||
blue: 2,
|
||
gold: 3,
|
||
yellow: 3,
|
||
black: 4,
|
||
burgundy: 5,
|
||
};
|
||
const resource = new RegExp(
|
||
`(?<!\\w)(?:(?<foodCount>${quantity}) (?<colour>green|coral|red|blue|gold|yellow|black|burgundy) (?:nutrients?|(?:food )?cubes?|ones?)|(?<bp>${quantity}) BP|(?<injury>${quantity}) injur(?:y|ies)|(?<evolution>${quantity}) (?:evolution points?|EP)|(?<mutation>${quantity}) mutation(?: sensitivity)?)\\b`,
|
||
"gi",
|
||
);
|
||
const count = (value) =>
|
||
numbers[value.toLowerCase()] ?? Number(value.replace("−", "-"));
|
||
|
||
const escape = (text) =>
|
||
String(text).replace(
|
||
/[&<>"']/g,
|
||
(c) =>
|
||
({ "&": "&", "<": "<", ">": ">", '"': """, "'": "'" })[
|
||
c
|
||
],
|
||
);
|
||
|
||
// Translate the sentence before inserting markup, so resource and gene names
|
||
// cannot split grammatical context into independently translated fragments.
|
||
export function renderRichText(source, translate = (text) => text) {
|
||
source = String(source);
|
||
const replacements = [];
|
||
for (const match of source.matchAll(resource)) {
|
||
const g = match.groups;
|
||
const html = g.colour
|
||
? food(colours[g.colour.toLowerCase()], count(g.foodCount))
|
||
: g.bp
|
||
? bp(count(g.bp))
|
||
: g.injury
|
||
? injury(count(g.injury))
|
||
: g.evolution
|
||
? evolutionPoints(count(g.evolution))
|
||
: sensitivity(count(g.mutation));
|
||
replacements.push({
|
||
text: translate(match[0]),
|
||
html: html.replace(
|
||
/(aria-label|title)="([^"]*)"/g,
|
||
(_, attribute, value) => `${attribute}="${escape(translate(value))}"`,
|
||
),
|
||
});
|
||
}
|
||
const text = translate(source);
|
||
const ordered = replacements.sort((a, b) => b.text.length - a.text.length);
|
||
let output = "",
|
||
offset = 0;
|
||
while (offset < text.length) {
|
||
let next;
|
||
for (const replacement of ordered) {
|
||
if (!replacement.text) continue;
|
||
const index = text.indexOf(replacement.text, offset);
|
||
if (index >= 0 && (!next || index < next.index))
|
||
next = { ...replacement, index };
|
||
}
|
||
if (!next) break;
|
||
output += geneText(text.slice(offset, next.index), translate) + next.html;
|
||
offset = next.index + next.text.length;
|
||
}
|
||
return output + geneText(text.slice(offset), translate);
|
||
}
|
||
|
||
export function richText(text) {
|
||
const rendered = renderRichText(text);
|
||
if (rendered === escape(text)) return rendered;
|
||
return `<span data-localized-rich-text="${escape(text)}" translate="no">${rendered}</span>`;
|
||
}
|