/** * Custom Micron Parser Implementation for NomadNet * Based on the Micron markup specification from NomadNet */ function parseMicronSource(micronText) { if (!micronText) return ''; let html = ''; const lines = micronText.split('\n'); let sectionLevel = 0; let alignment = ''; let currentStyles = { bold: false, italic: false, underline: false, fg: '', bg: '' }; function escapeHtml(text) { const div = document.createElement('div'); div.textContent = text; return div.innerHTML; } function applyStyles(text, styles) { let result = text; let openTags = []; let closeTags = []; if (styles.bg) { openTags.push(``); closeTags.unshift(''); } if (styles.fg) { openTags.push(``); closeTags.unshift(''); } if (styles.bold) { openTags.push(''); closeTags.unshift(''); } if (styles.italic) { openTags.push(''); closeTags.unshift(''); } if (styles.underline) { openTags.push(''); closeTags.unshift(''); } return openTags.join('') + result + closeTags.join(''); } function processLine(line) { // Skip comments if (line.trim().startsWith('#')) { return ''; } // Handle literal blocks if (line.trim() === '`=') { return '
';
        }
        if (line.trim() === '=`') {
            return '
'; } // Handle sections const sectionMatch = line.match(/^(>+)(.*)$/); if (sectionMatch) { const level = sectionMatch[1].length; const heading = sectionMatch[2].trim(); let result = ''; // Close previous sections if needed while (sectionLevel >= level) { result += ''; sectionLevel--; } // Open new section result += `
`; if (heading) { result += `${escapeHtml(heading)}`; } sectionLevel = level; return result; } // Handle alignment tags at start of line if (line.startsWith('`c')) { alignment = 'center'; line = line.substring(2); } else if (line.startsWith('`l')) { alignment = 'left'; line = line.substring(2); } else if (line.startsWith('`r')) { alignment = 'right'; line = line.substring(2); } else if (line.startsWith('`a')) { alignment = ''; line = line.substring(2); } // Handle dividers if (line.trim() === '-' || line.trim().startsWith('-∿') || line.trim() === '<') { return '
'; } // Process inline formatting let processedLine = line; // Handle color and formatting tags processedLine = processedLine.replace(/`F([0-9a-fA-F]{3})/g, (match, color) => { currentStyles.fg = color; return ``; }); processedLine = processedLine.replace(/`B([0-9a-fA-F]{3})/g, (match, color) => { currentStyles.bg = color; return ``; }); processedLine = processedLine.replace(/`f/g, () => { currentStyles.fg = ''; return ''; }); processedLine = processedLine.replace(/`b/g, () => { currentStyles.bg = ''; return ''; }); // Handle formatting tags processedLine = processedLine.replace(/`!/g, ''); processedLine = processedLine.replace(/!/g, ''); processedLine = processedLine.replace(/`\*/g, ''); processedLine = processedLine.replace(/\*/g, ''); processedLine = processedLine.replace(/`_/g, ''); processedLine = processedLine.replace(/_/g, ''); // Handle reset tag processedLine = processedLine.replace(/``/g, ''); // Handle links processedLine = processedLine.replace(/`\[([^\]]*)`([^\]]+)\]/g, (match, label, url) => { return `${label || url}`; }); processedLine = processedLine.replace(/`\[([^\]]+)\]/g, (match, url) => { return `${url}`; }); // Handle input fields processedLine = processedLine.replace /<([^>]+)`([^>]*)>/g, (match, name, value) => { const escaped_name = escapeHtml(name); const escaped_value = escapeHtml(value); return ``; }); // Apply alignment if (alignment) { processedLine = `
${processedLine}
`; } else if (processedLine.trim()) { processedLine = `
${processedLine}
`; } return processedLine; } // Process each line for (let i = 0; i < lines.length; i++) { const processedLine = processLine(lines[i]); if (processedLine) { html += processedLine + '\n'; } } // Close any remaining sections while (sectionLevel > 0) { html += '
'; sectionLevel--; } return html; } // Also expose it as a global function for compatibility window.parseMicronSource = parseMicronSource; window.parseMicron = parseMicronSource; // Alternative name window.MicronParser = parseMicronSource; // Another alternative console.log('Custom Micron parser loaded successfully!');