BibliographyBackbone: Difference between revisions
From Illustrations in German Translations of Mark Twain's Works
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
{{Seitenkopf|Bibliography Generator}} | {{Seitenkopf|Bibliography Generator}} | ||
<button id="generateBtn" style=" | <button id="generateBtn" style=" | ||
| Line 229: | Line 227: | ||
})(); | })(); | ||
</script> | </script> | ||
Revision as of 00:50, 12 May 2026
Bibliography Generator
<button id="generateBtn" style="
background-color: #008CBA; color: white; padding: 10px 20px; border: none; border-radius: 5px; font-size: 1rem; cursor: pointer; margin-bottom: 1rem;
">Generate Bibliography HTML</button>
<textarea id="bibOutput" style="
width: 100%; height: 400px; font-family: 'Courier New', monospace; font-size: 0.85rem; padding: 10px; border: 2px solid #ddd; border-radius: 5px; box-sizing: border-box; white-space: pre; overflow: auto;
"></textarea>
<script> (function() {
function escapeHtml(text) {
if (!text) return ;
const map = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": '''
};
return text.replace(/[&<>"']/g, m => map[m]);
}
function getOrUnknown(value) {
if (!value || (typeof value === 'string' && !value.trim())) {
return 'unknown';
}
return value;
}
function formatAuthors(authors) {
if (!authors || authors.length === 0) {
return 'unknown';
}
if (authors.length === 1) {
const a = authors[0];
const family = a.family || 'unknown';
const given = a.given || ;
return given ? `${family}, ${given}` : family;
}
if (authors.length === 2) {
const a1 = authors[0];
const a2 = authors[1];
const name1 = (a1.family || 'unknown') + (a1.given ? ', ' + a1.given : );
const name2 = (a2.family || 'unknown') + (a2.given ? ', ' + a2.given : );
return `${name1}, and ${name2}`;
}
const a = authors[0];
const name = (a.family || 'unknown') + (a.given ? ', ' + a.given : );
return `${name}, et al.`;
}
function formatMLA9(entry) {
let citation = ;
// Autoren const authors = formatAuthors(entry.authors); citation += authors + '. ';
// Titel
const title = getOrUnknown(entry.title);
if (entry.type === 'bookSection' || entry.type === 'book') {
citation += `${escapeHtml(title)}. `;
} else {
citation += `"${escapeHtml(title)}." `;
}
// Container
const container = entry.container;
if (container && container.trim()) {
citation += `${escapeHtml(container)}. `;
}
// Jahr
const year = getOrUnknown(entry.year);
citation += `${year}. `;
// URL
if (entry.url && entry.url.trim()) {
citation += `<a href="${escapeHtml(entry.url)}" target="_blank">${escapeHtml(entry.url)}</a>.`;
} else {
citation = citation.trim();
if (!citation.endsWith('.')) {
citation += '.';
}
}
return citation; }
function generateTagsAttribute(tags) {
if (!tags || tags.length === 0) {
return ' data-tags="untagged"';
}
const normalized = tags
.map(tag => tag.toLowerCase().replace(/\s+/g, '_'))
.join(' ');
return ` data-tags="${normalized}"`;
}
function generateTagSpans(tags) {
if (!tags || tags.length === 0) {
return 'untagged';
}
return tags
.map(tag => {
const normalized = tag.toLowerCase().replace(/\s+/g, '_');
return `${escapeHtml(tag)}`;
})
.join('\n ');
}
function generateBibEntry(entry, index) {
const mlaFormatted = formatMLA9(entry);
const tagsAttr = generateTagsAttribute(entry.tags);
const tagSpans = generateTagSpans(entry.tags);
const abstract = entry.abstract && entry.abstract.trim() ? escapeHtml(entry.abstract) : ;
return `
${mlaFormatted}
${abstract || '(No abstract available)'}
<button class="expand-btn">Show Abstract</button>
`;
}
async function generateBibliography() {
console.log('generateBibliography aufgerufen');
const output = document.getElementById('bibOutput');
if (!output) {
console.error('bibOutput not found');
return;
}
output.value = 'Lade Daten...';
try {
const response = await fetch('/BibliographyData?action=raw');
if (!response.ok) throw new Error(`HTTP ${response.status}`);
const entries = await response.json();
console.log('JSON geparst, ' + entries.length + ' Einträge');
// Filtere irrelevante Einträge
const filtered = entries.filter(entry => {
if (entry.tags && entry.tags.includes('Not relevant for Mark Twain')) {
return false;
}
if (!entry.title || entry.title.trim() === 'Untitled') {
return false;
}
return true;
});
// Extrahiere alle Tags
const allTags = new Set();
filtered.forEach(entry => {
if (entry.tags && Array.isArray(entry.tags)) {
entry.tags.forEach(tag => allTags.add(tag));
}
});
// Generiere HTML
const tagSpans = Array.from(allTags)
.sort()
.map(tag => {
const normalized = tag.toLowerCase().replace(/\s+/g, '_');
return `${escapeHtml(tag)}`;
})
.join('\n ');
const entryHTML = filtered
.map((entry, i) => generateBibEntry(entry, i + 1))
.join('\n\n');
const html = `
${entryHTML}
`;
output.value = html;
console.log('Fertig - ' + filtered.length + ' Einträge generiert');
} catch (error) {
output.value = 'ERROR: ' + error.message;
console.error('Fehler:', error);
}
}
document.addEventListener('DOMContentLoaded', function() {
const btn = document.getElementById('generateBtn');
if (btn) {
btn.addEventListener('click', generateBibliography);
console.log('Generate Button bereit');
}
});
})(); </script>