2026-01-18 14:48:31 +11:00
|
|
|
// Solstice Syntax Highlighter
|
|
|
|
|
// Add this script to your HTML file before the closing </body> tag
|
|
|
|
|
|
|
|
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
|
highlightAllCode();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function highlightAllCode() {
|
|
|
|
|
const codeElements = document.querySelectorAll('pre.code code');
|
|
|
|
|
codeElements.forEach(element => {
|
|
|
|
|
// Get the text content (this automatically decodes HTML entities)
|
|
|
|
|
const code = element.textContent;
|
|
|
|
|
element.innerHTML = highlightSolstice(code);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function highlightSolstice(code) {
|
|
|
|
|
let result = '';
|
|
|
|
|
let i = 0;
|
|
|
|
|
|
|
|
|
|
// Tokenize the code
|
|
|
|
|
while (i < code.length) {
|
|
|
|
|
let matched = false;
|
|
|
|
|
|
|
|
|
|
// Check for comments
|
|
|
|
|
if (code.substr(i, 2) === '//') {
|
|
|
|
|
let end = code.indexOf('\n', i);
|
|
|
|
|
if (end === -1) end = code.length;
|
|
|
|
|
result += `<span class="comment">${escapeHtml(code.substring(i, end))}</span>`;
|
|
|
|
|
i = end;
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for strings
|
|
|
|
|
else if (code[i] === '"') {
|
|
|
|
|
let end = i + 1;
|
|
|
|
|
while (end < code.length && code[end] !== '"') {
|
|
|
|
|
if (code[end] === '\\') end++; // Skip escaped characters
|
|
|
|
|
end++;
|
|
|
|
|
}
|
|
|
|
|
end++; // Include closing quote
|
|
|
|
|
result += `<span class="string">${escapeHtml(code.substring(i, end))}</span>`;
|
|
|
|
|
i = end;
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for numbers
|
|
|
|
|
else if (/\d/.test(code[i])) {
|
|
|
|
|
let end = i;
|
|
|
|
|
while (end < code.length && /[\d.]/.test(code[end])) {
|
|
|
|
|
end++;
|
|
|
|
|
}
|
|
|
|
|
result += `<span class="number">${code.substring(i, end)}</span>`;
|
|
|
|
|
i = end;
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for keywords, types, built-ins, and identifiers
|
|
|
|
|
else if (/[a-zA-Z_]/.test(code[i])) {
|
|
|
|
|
let end = i;
|
|
|
|
|
while (end < code.length && /[a-zA-Z0-9_]/.test(code[end])) {
|
|
|
|
|
end++;
|
|
|
|
|
}
|
|
|
|
|
const word = code.substring(i, end);
|
|
|
|
|
|
|
|
|
|
// Check what type of word it is
|
2026-01-28 18:31:10 +11:00
|
|
|
if (['def', 'if', 'while', 'return', 'ground', 'puts', 'def', 'struct', 'new', 'use'].includes(word)) {
|
2026-01-18 14:48:31 +11:00
|
|
|
result += `<span class="keyword">${word}</span>`;
|
|
|
|
|
} else if (['input', 'print', 'println'].includes(word)) {
|
|
|
|
|
result += `<span class="builtin">${word}</span>`;
|
|
|
|
|
} else if (['int', 'double', 'string', 'char', 'bool'].includes(word)) {
|
|
|
|
|
result += `<span class="type">${word}</span>`;
|
|
|
|
|
} else if (['true', 'false'].includes(word)) {
|
|
|
|
|
result += `<span class="boolean">${word}</span>`;
|
|
|
|
|
} else {
|
|
|
|
|
// Check if it's a function call (followed by '(')
|
|
|
|
|
let j = end;
|
|
|
|
|
while (j < code.length && /\s/.test(code[j])) j++;
|
|
|
|
|
if (code[j] === '(') {
|
|
|
|
|
result += `<span class="function">${word}</span>`;
|
|
|
|
|
} else {
|
|
|
|
|
result += word;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
i = end;
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Check for operators
|
2026-01-27 14:27:09 +11:00
|
|
|
else if ('+-*/=!<>:'.includes(code[i])) {
|
2026-01-18 14:48:31 +11:00
|
|
|
let op = code[i];
|
|
|
|
|
if (i + 1 < code.length && '='.includes(code[i + 1])) {
|
|
|
|
|
op += code[i + 1];
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
result += `<span class="operator">${escapeHtml(op)}</span>`;
|
|
|
|
|
i++;
|
|
|
|
|
matched = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Everything else (whitespace, braces, etc.)
|
|
|
|
|
if (!matched) {
|
|
|
|
|
result += escapeHtml(code[i]);
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function escapeHtml(text) {
|
|
|
|
|
return text
|
|
|
|
|
.replace(/&/g, '&')
|
|
|
|
|
.replace(/</g, '<')
|
|
|
|
|
.replace(/>/g, '>')
|
|
|
|
|
.replace(/"/g, '"')
|
|
|
|
|
.replace(/'/g, ''');
|
|
|
|
|
}
|