342 lines
No EOL
13 KiB
JavaScript
342 lines
No EOL
13 KiB
JavaScript
/**
|
|
* SimonUp Core Compiler (Core.js)
|
|
* Eine moderne, fluss- und blockorientierte Auszeichnungssprache.
|
|
*/
|
|
class SimonUp {
|
|
constructor() {
|
|
this.plugins = [];
|
|
this.headings = []; // Speichert alle gefundenen Überschriften für das TOC
|
|
this.counters = [0, 0, 0, 0, 0, 0]; // Zähler für die hierarchischen Ebenen 1 bis 6
|
|
}
|
|
|
|
/**
|
|
* Registriert eine Erweiterung (Plugin)
|
|
*/
|
|
use(plugin) {
|
|
this.plugins.push(plugin);
|
|
return this;
|
|
}
|
|
|
|
/**
|
|
* Hauptfunktion: Wandelt SimonUp-Text (.up) in valides HTML um
|
|
*/
|
|
parse(text) {
|
|
this.headings = [];
|
|
this.counters = [0, 0, 0, 0, 0, 0];
|
|
|
|
let html = text;
|
|
|
|
for (const plugin of this.plugins) {
|
|
if (plugin.before) html = plugin.before(html);
|
|
}
|
|
|
|
html = this._parseBlocks(html);
|
|
html = this._injectTOC(html);
|
|
|
|
for (const plugin of this.plugins) {
|
|
if (plugin.after) html = plugin.after(html);
|
|
}
|
|
|
|
return html;
|
|
}
|
|
|
|
/**
|
|
* INTERN: Verarbeitet strukturelle Blöcke zeilenweise
|
|
*/
|
|
_parseBlocks(text) {
|
|
const lines = text.replace(/\r\n/g, '\n').split('\n');
|
|
let result = [];
|
|
let inBlock = false;
|
|
let blockType = null;
|
|
let blockMeta = "";
|
|
let blockBuffer = [];
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
let line = lines[i];
|
|
|
|
if (!inBlock && line.startsWith('&') && line.trim().length > 1) {
|
|
inBlock = true;
|
|
const fullHeader = line.substring(1).trim();
|
|
const spaceIndex = fullHeader.indexOf(' ');
|
|
|
|
if (spaceIndex !== -1) {
|
|
blockType = fullHeader.substring(0, spaceIndex).toLowerCase();
|
|
blockMeta = fullHeader.substring(spaceIndex).trim();
|
|
} else {
|
|
blockType = fullHeader.toLowerCase();
|
|
blockMeta = "";
|
|
}
|
|
blockBuffer = [];
|
|
continue;
|
|
}
|
|
|
|
if (inBlock && line.trim() === '&') {
|
|
result.push(this._renderBlock(blockType, blockMeta, blockBuffer));
|
|
inBlock = false;
|
|
blockType = null;
|
|
blockMeta = "";
|
|
continue;
|
|
}
|
|
|
|
if (inBlock) {
|
|
blockBuffer.push(line);
|
|
} else {
|
|
if (line.trim() === '___') {
|
|
result.push('<hr>');
|
|
} else {
|
|
result.push(this._parseInline(line));
|
|
}
|
|
}
|
|
}
|
|
|
|
return this._processParagraphs(result);
|
|
}
|
|
|
|
/**
|
|
* INTERN: Erzeugt das HTML für einen geschlossenen Block
|
|
*/
|
|
_renderBlock(type, meta, lines) {
|
|
const content = lines.join('\n');
|
|
|
|
switch (type) {
|
|
case 'toc':
|
|
let tocHtml = `<div class="simonup-toc-wrapper">\n`;
|
|
if (meta) tocHtml += `<h2>${meta}</h2>\n`;
|
|
if (content.trim()) {
|
|
const intro = lines.map(l => this._parseInline(l)).join('<br>');
|
|
tocHtml += `<p class="toc-intro">${intro}</p>\n`;
|
|
}
|
|
tocHtml += `\n</div>`;
|
|
return tocHtml;
|
|
|
|
case '-':
|
|
return '<ul>\n' + lines.filter(l => l.trim()).map(l => `<li>${this._parseInline(l)}</li>`).join('\n') + '\n</ul>';
|
|
|
|
case '+':
|
|
return '<ol>\n' + lines.filter(l => l.trim()).map(l => `<li>${this._parseInline(l)}</li>`).join('\n') + '\n</ol>';
|
|
|
|
case 'c':
|
|
const langClass = meta ? ` class="language-${meta}"` : '';
|
|
const poolCode = content.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
return `<pre><code${langClass}>${poolCode}</code></pre>`;
|
|
|
|
case 'q':
|
|
return `<blockquote>${lines.map(l => this._parseInline(l)).join('<br>')}</blockquote>`;
|
|
|
|
case 'b':
|
|
return `<div class="bible-block">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`;
|
|
|
|
case 'f':
|
|
return `<details>\n<summary>${meta || 'Details'}</summary>\n<p>${lines.map(l => this._parseInline(l)).join('<br>')}</p>\n</details>`;
|
|
|
|
case 'i':
|
|
return `<div class="infobox" style="border-left: 4px solid ${meta || '#007bff'}; padding: 10px; margin: 10px 0; background: #f8f9fa;">\n${lines.map(l => this._parseInline(l)).join('<br>')}\n</div>`;
|
|
|
|
case 't':
|
|
return this._renderTable(meta, lines);
|
|
|
|
default:
|
|
return `<div class="block-${type}">${lines.map(l => this._parseInline(l)).join('<br>')}</div>`;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* INTERN: Hilfsfunktion zum Rendern von Multi-line-Tabellen und Galerien
|
|
*/
|
|
_renderTable(meta, lines) {
|
|
let tableHtml = '<table>\n';
|
|
let currentCells = [];
|
|
let cellBuffer = [];
|
|
|
|
for (let line of lines) {
|
|
if (line.trim() === '') continue;
|
|
|
|
let parts = line.split('|');
|
|
for (let i = 0; i < parts.length; i++) {
|
|
let part = parts[i];
|
|
if (i === parts.length - 1 && part.trim() === '') {
|
|
if (cellBuffer.length > 0 || part !== '') {
|
|
cellBuffer.push(part);
|
|
}
|
|
if (cellBuffer.length > 0) {
|
|
currentCells.push(cellBuffer.join('\n'));
|
|
cellBuffer = [];
|
|
}
|
|
tableHtml += '<tr>\n' + currentCells.map(c => `<td>${this._parseInline(c.trim())}</td>`).join('\n') + '\n</tr>\n';
|
|
currentCells = [];
|
|
} else {
|
|
cellBuffer.push(part);
|
|
if (i < parts.length - 1) {
|
|
currentCells.push(cellBuffer.join('\n'));
|
|
cellBuffer = [];
|
|
}
|
|
}
|
|
}
|
|
}
|
|
tableHtml += '</table>';
|
|
return tableHtml;
|
|
}
|
|
|
|
/**
|
|
* INTERN: Verarbeitet Fluss-Formatierungen (Inline) mittels RegEx
|
|
*/
|
|
_parseInline(text) {
|
|
if (!text) return '';
|
|
let html = text;
|
|
|
|
// 1. ESCAPE: Temporär schützen
|
|
html = html.replace(/\\(.)/g, (match, char) => ``);
|
|
|
|
// 2. ÜBERSCHRIFTEN (Flusssicher angepasst)
|
|
|
|
// Regel A: Automatische Nummerierung (3#x Titel 3#x Restlicher Text)
|
|
html = html.replace(/([1-6])#x\s*([^#]+?)\s*\1#x(.*)/g, (match, level, title, rest) => {
|
|
const lvl = parseInt(level);
|
|
const numStr = this._getAutoNumber(lvl);
|
|
const fullTitle = `${numStr} ${title.trim()}`;
|
|
const id = this._slugify(fullTitle);
|
|
|
|
this.headings.push({ level: lvl, text: fullTitle, id: id });
|
|
return `<h${lvl} id="${id}" style="display:inline-block; margin-right:10px;">${fullTitle}</h${lvl}>${rest}`;
|
|
});
|
|
|
|
// Regel B: Vom TOC ausgeschlossene Titel (3#- Titel 3#- Restlicher Text)
|
|
html = html.replace(/([1-6])#-\s*([^#]+?)\s*\1#-(.*)/g, (match, level, title, rest) => {
|
|
const lvl = parseInt(level);
|
|
return `<h${lvl} style="display:inline-block; margin-right:10px;">${title.trim()}</h${lvl}>${rest}`;
|
|
});
|
|
|
|
// Regel C: Manuelle Nummerierung (3# Titel 3# Restlicher Text)
|
|
html = html.replace(/([1-6])#\s*([^#]+?)\s*\1#(.*)/g, (match, level, title, rest) => {
|
|
const lvl = parseInt(level);
|
|
const id = this._slugify(title.trim());
|
|
|
|
this.headings.push({ level: lvl, text: title.trim(), id: id });
|
|
return `<h${lvl} id="${id}" style="display:inline-block; margin-right:10px;">${title.trim()}</h${lvl}>${rest}`;
|
|
});
|
|
|
|
// Regel D: Das schnelle, solitäre # für H1 (# Titel # Restlicher Text)
|
|
html = html.replace(/#([^#]+)#(.*)/g, (match, title, rest) => {
|
|
const id = this._slugify(title.trim());
|
|
this.headings.push({ level: 1, text: title.trim(), id: id });
|
|
return `<h1 id="${id}" style="display:inline-block; margin-right:10px;">${title.trim()}</h1>${rest}`;
|
|
});
|
|
|
|
// 3. STANDARD FLUSS-FORMATIERUNGEN (In priorisierter Reihenfolge)
|
|
html = html.replace(/\*\*([^*]+)\*\*/g, '<strong>$1</strong>'); // Fett
|
|
html = html.replace(/\/([^/]+)\//g, '<em>$1</em>'); // Kursiv
|
|
html = html.replace(/_([^_]+)_/g, '<u>$1</u>'); // Unterstrichen
|
|
html = html.replace(/~([^~]+)~/g, '<del>$1</del>'); // Durchgestrichen
|
|
html = html.replace(/=([^=]+)=/g, '<mark>$1</mark>'); // Highlight
|
|
html = html.replace(/\^([^^]+)\^/g, '<sup>$1</sup>'); // Hochgestellt
|
|
html = html.replace(/°([^°]+)°/g, '<sub>$1</sub>'); // Tiefgestellt
|
|
html = html.replace(/`([^`]+)`/g, '<code>$1</code>'); // Inline-Code
|
|
|
|
// 3.1 MEDIUM-FORMATIERUNG (Sucht nach *, ignoriert aber bereits generiertes HTML)
|
|
html = html.replace(/(?<!<[^>]*)\*([^*<\/>]+)\*(?![^<]*>)/g, '<span style="font-weight: 500;">$1</span>');
|
|
|
|
// 4. AUSRICHTUNG & EINRÜCKUNG (Basiszeichen '>')
|
|
html = html.replace(/>l ([^>]+)>/g, '<div style="text-align: left; display: inline-block; width: 100%;">$1</div>');
|
|
html = html.replace(/>r ([^>]+)>/g, '<div style="text-align: right; display: inline-block; width: 100%;">$1</div>');
|
|
html = html.replace(/>c ([^>]+)>/g, '<div style="text-align: center; display: inline-block; width: 100%;">$1</div>');
|
|
html = html.replace(/>j ([^>]+)>/g, '<div style="text-align: justify; display: inline-block; width: 100%;">$1</div>');
|
|
html = html.replace(/>tt ([^>]+)>/g, '<div style="margin-left: 4em; display: inline-block;">$1</div>');
|
|
html = html.replace(/>t ([^>]+)>/g, '<div style="margin-left: 2em; display: inline-block;">$1</div>');
|
|
|
|
// 5. LINKS & MEDIEN
|
|
html = html.replace(/!\[([^\]]+)\s+w:(\d+)\]\(([^)]+)\)/g, '<img src="$3" alt="$1" style="width: $2px; height: auto;">');
|
|
html = html.replace(/!\[([^\]]+)\]\(([^)]+)\)/g, '<img src="$2" alt="$1">');
|
|
html = html.replace(/\[([^\]]+)\]\{([^}]+)\}/g, '<a href="$2" target="_blank" rel="noopener noreferrer">$1</a>');
|
|
html = html.replace(/\[([^\]]+)\]\(([^)]+)\)/g, '<a href="$2">$1</a>');
|
|
|
|
// 6. FUSSNOTEN
|
|
html = html.replace(/\^x\[([^\]]+)\]\^/g, '<span class="footnote-auto" title="$1">[*]</span>');
|
|
html = html.replace(/\^(\d+)\[([^\]]+)\]\^/g, '<sup class="footnote-manual" title="$2">[$1]</sup>');
|
|
|
|
// 7. ESCAPE ZURÜCKWANDELN
|
|
html = html.replace(/<\!--SU_ESC_(\d+)-->/g, (match, code) => String.fromCharCode(parseInt(code)));
|
|
|
|
return html;
|
|
}
|
|
|
|
/**
|
|
* INTERN: Errechnet die nächste fortlaufende Kapitelnummer
|
|
*/
|
|
_getAutoNumber(level) {
|
|
let idx = level - 1;
|
|
this.counters[idx]++;
|
|
for (let i = idx + 1; i < 6; i++) this.counters[i] = 0;
|
|
let activeParts = this.counters.slice(0, level);
|
|
return activeParts.join('.') + '.';
|
|
}
|
|
|
|
/**
|
|
* INTERN: Macht aus Text eine URL-konforme ID
|
|
*/
|
|
_slugify(text) {
|
|
return text.toLowerCase()
|
|
.replace(/[^a-z0-9\s-]/g, '')
|
|
.replace(/\s+/g, '-');
|
|
}
|
|
|
|
/**
|
|
* INTERN: Erzeugt die fertige HTML-Liste des TOC und injiziert sie in den Platzhalter
|
|
*/
|
|
_injectTOC(html) {
|
|
if (this.headings.length === 0) {
|
|
return html.replace('', '');
|
|
}
|
|
|
|
let tocListHtml = '<div class="simonup-toc">\n<ul>\n';
|
|
let currentLevel = 1;
|
|
|
|
for (const heading of this.headings) {
|
|
if (heading.level > currentLevel) {
|
|
tocListHtml += '<ul>\n'.repeat(heading.level - currentLevel);
|
|
} else if (heading.level < currentLevel) {
|
|
tocListHtml += '</ul>\n'.repeat(currentLevel - heading.level);
|
|
}
|
|
currentLevel = heading.level;
|
|
|
|
tocListHtml += `<li><a href="#${heading.id}">${heading.text}</a></li>\n`;
|
|
}
|
|
|
|
if (currentLevel > 1) {
|
|
tocListHtml += '</ul>\n'.repeat(currentLevel - 1);
|
|
}
|
|
tocListHtml += '</ul>\n</div>';
|
|
|
|
return html.replace('', tocListHtml);
|
|
}
|
|
|
|
/**
|
|
* INTERN: Analysiert leere Zeilen, um Absätze (<p>) und Zeilenumbrüche (<br>) zu trennen
|
|
*/
|
|
_processParagraphs(elements) {
|
|
let processed = [];
|
|
let paragraphBuffer = [];
|
|
|
|
for (let element of elements) {
|
|
if (element.startsWith('<') && !element.startsWith('<a>') && !element.startsWith('<strong>') && !element.startsWith('<em>') && !element.startsWith('<span>') && !element.startsWith('<code>') && !element.startsWith('<img') && !element.startsWith('<h')) {
|
|
if (paragraphBuffer.length > 0) {
|
|
processed.push(`<p>${paragraphBuffer.join('<br>')}</p>`);
|
|
paragraphBuffer = [];
|
|
}
|
|
processed.push(element);
|
|
} else if (element.trim() === '') {
|
|
if (paragraphBuffer.length > 0) {
|
|
processed.push(`<p>${paragraphBuffer.join('<br>')}</p>`);
|
|
paragraphBuffer = [];
|
|
}
|
|
} else {
|
|
paragraphBuffer.push(element);
|
|
}
|
|
}
|
|
|
|
if (paragraphBuffer.length > 0) {
|
|
processed.push(`<p>${paragraphBuffer.join('<br>')}</p>`);
|
|
}
|
|
|
|
return processed.join('\n');
|
|
}
|
|
} |