// Solstice Syntax Highlighter // Add this script to your HTML file before the closing 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 += `${escapeHtml(code.substring(i, end))}`; 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 += `${escapeHtml(code.substring(i, end))}`; 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 += `${code.substring(i, end)}`; 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 if (['def', 'if', 'while', 'return', 'ground', 'puts', 'def', 'struct', 'new'].includes(word)) { result += `${word}`; } else if (['input', 'print', 'println'].includes(word)) { result += `${word}`; } else if (['int', 'double', 'string', 'char', 'bool'].includes(word)) { result += `${word}`; } else if (['true', 'false'].includes(word)) { result += `${word}`; } 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 += `${word}`; } else { result += word; } } i = end; matched = true; } // Check for operators else if ('+-*/=!<>:'.includes(code[i])) { let op = code[i]; if (i + 1 < code.length && '='.includes(code[i + 1])) { op += code[i + 1]; i++; } result += `${escapeHtml(op)}`; 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, '''); }