/**
* 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 += `
`;
} 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!');