I made a markup language that compiles to HTML called NoirCode: const textarea =…
- User
I made a markup language that compiles to HTML called NoirCode:
const textarea = document.getElementById('code'); const previewFrame = document.getElementById('previewFrame'); const errorMessage = document.getElementById('error-message');
/**
-
Compiles custom code to HTML.
-
@param {string} customCode - The custom code to compile.
-
@returns {Object} - An object containing the compiled HTML and any errors. */ function compileToHTML(customCode) { const lines = customCode.split('\n'); let html = ''; let errors = []; let i = 0; const totalLines = lines.length; let inBlockComment = false;
while (i < totalLines) { let line = lines[i].trim();
if (inBlockComment) { if (line.endsWith('#}')) { inBlockComment = false; } i++; continue; } if (line.startsWith('#{')) { inBlockComment = true; i++; continue; } if (line.startsWith('#') && !line.startsWith('#{')) { i++; continue; } if (line === '\\}') { errors.push(`Unexpected block closure '\\}' at line ${i + 1}.`); i++; continue; } const blockStartMatch = line.match(/^(\w+)(\[[^\]]+\])?(#[\w\-]+)?((?:\.[\w\-]+)*?)\\\{$/); if (blockStartMatch) { const tag = blockStartMatch[1].toLowerCase(); const attrs = blockStartMatch[2] ? replaceIndexPlaceholders(blockStartMatch[2].slice(1, -1), 1) : ''; const id = blockStartMatch[3] ? replaceIndexPlaceholders(blockStartMatch[3].slice(1), 1) : ''; const classesRaw = blockStartMatch[4] ? blockStartMatch[4] : ''; const classes = classesRaw.split('.').filter(cls => cls !== ''); let blockContent = ''; i++; let openBlocks = 1; while (i < totalLines && openBlocks > 0) { let currentLine = lines[i].trim(); if (currentLine.endsWith('\\{')) { openBlocks++; } if (currentLine === '\\}') { openBlocks--; if (openBlocks === 0) { i++; break; } } if (openBlocks > 0) { blockContent += lines[i] + '\n'; } i++; } if (openBlocks > 0) { errors.push(`Unclosed block starting with <${tag}>.`); break; } let innerHTML = ''; if (tag === 'script' || tag === 'style' || tag === 'html') { innerHTML = blockContent; } else { let compiledBlock; try { compiledBlock = compileToHTML(blockContent); } catch (error) { errors.push(`Line ${i + 1}: Unexpected error: ${error.message}`); compiledBlock = { html: '', errors: [`Line ${i + 1}: Unexpected error: ${error.message}`] }; } if (compiledBlock.errors.length > 0) { errors = errors.concat(compiledBlock.errors); } innerHTML = compiledBlock.html; } let attrString = ''; if (attrs) attrString += ` ${attrs}`; if (id) attrString += ` id="${id}"`; if (classes.length > 0) attrString += ` class="${classes.join(' ')}"`; html += `<${tag}${attrString}>\n${innerHTML}</${tag}>\n`; continue; } const inlineMatch = line.match(/^(\w+)(\[[^\]]+\])?(#[\w\-]+)?((?:\.[\w\-]+)*?)(?:\{([^}]*)\})?(?:\*(\d+))?$/); if (inlineMatch) { try { const tag = inlineMatch[1].toLowerCase(); const attrs = inlineMatch[2] ? replaceIndexPlaceholders(inlineMatch[2].slice(1, -1), 1) : ''; const id = inlineMatch[3] ? replaceIndexPlaceholders(inlineMatch[3].slice(1), 1) : ''; const classesRaw = inlineMatch[4] ? inlineMatch[4] : ''; const classes = classesRaw.split('.').filter(cls => cls !== ''); const content = inlineMatch[5] || ''; const repeat = inlineMatch[6] ? parseInt(inlineMatch[6]) : 1; let finalAttrString = ''; if (attrs) finalAttrString += ` ${attrs}`; if (id) finalAttrString += ` id="${id}"`; if (classes.length > 0) finalAttrString += ` class="${classes.join(' ')}"`; for (let j = 1; j <= repeat; j++) { const currentContent = replaceIndexPlaceholders(content, j); const currentAttrString = replaceIndexPlaceholders(finalAttrString, j); html += `<${tag}${currentAttrString}>${currentContent}</${tag}>\n`; } i++; } catch (err) { errors.push(`Line ${i + 1}: ${err.message}`); } continue; } const siblingTokens = splitRespectingBlocks(line, '+'); siblingTokens.forEach(token => { const nestingTokens = splitRespectingBlocks(token, '>'); const nestedHTML = processNesting(nestingTokens, 0, errors, i + 1); html += nestedHTML; }); const textLineMatch = line.match(/^[^+\>]+$/); if (!blockStartMatch && !inlineMatch && textLineMatch) { html += `${escapeHTML(line)}\n`; } i++;
-
- Assistant