BibliographyBackbone: Difference between revisions
From Illustrations in German Translations of Mark Twain's Works
No edit summary |
No edit summary |
||
| (2 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{Seitenkopf|Bibliography Generator}} | {{Seitenkopf|Bibliography Generator}} | ||
<html> | <html> | ||
<div style="max-width: 1000px;"> | |||
<button id="generateBtn" style=" | <button id="generateBtn" style=" | ||
| Line 39: | Line 40: | ||
}; | }; | ||
return text.replace(/[&<>"']/g, m => map[m]); | return text.replace(/[&<>"']/g, m => map[m]); | ||
} | |||
function getOrUnknown(value) { | |||
if (!value || (typeof value === 'string' && !value.trim())) { | |||
return 'unknown'; | |||
} | |||
return value; | |||
} | } | ||
function formatAuthors(authors) { | function formatAuthors(authors) { | ||
if (!authors || authors.length === 0) return ''; | if (!authors || authors.length === 0) { | ||
return 'unknown'; | |||
} | |||
if (authors.length === 1) { | if (authors.length === 1) { | ||
const a = authors[0]; | const a = authors[0]; | ||
const family = a.family || 'unknown'; | |||
const given = a.given || ''; | |||
return given ? `${family}, ${given}` : family; | |||
} | } | ||
if (authors.length === 2) { | if (authors.length === 2) { | ||
const a1 = authors[0]; | const a1 = authors[0]; | ||
const a2 = authors[1]; | const a2 = authors[1]; | ||
const name1 = a1.given ? | const name1 = (a1.family || 'unknown') + (a1.given ? ', ' + a1.given : ''); | ||
const name2 = a2.given ? | const name2 = (a2.family || 'unknown') + (a2.given ? ', ' + a2.given : ''); | ||
return `${name1}, and ${name2}`; | return `${name1}, and ${name2}`; | ||
} | } | ||
const a = authors[0]; | const a = authors[0]; | ||
const name = a.given ? | const name = (a.family || 'unknown') + (a.given ? ', ' + a.given : ''); | ||
return `${name}, et al.`; | return `${name}, et al.`; | ||
} | } | ||
| Line 62: | Line 77: | ||
let citation = ''; | let citation = ''; | ||
const authors = formatAuthors(entry.authors); | |||
citation += authors + '. '; | |||
const title = getOrUnknown(entry.title); | |||
if (entry.type === 'bookSection' || entry.type === 'book') { | |||
citation += `<i>${escapeHtml(title)}</i>. `; | |||
} else { | |||
citation += `"${escapeHtml(title)}." `; | |||
} | } | ||
const container = entry.container; | |||
if ( | if (container && container.trim()) { | ||
citation += `<i>${escapeHtml( | citation += `<i>${escapeHtml(container)}</i>. `; | ||
} | } | ||
const year = getOrUnknown(entry.year); | |||
citation += `${year}. `; | |||
if (entry.url && entry.url.trim()) { | |||
if (entry.url) { | |||
citation += `<a href="${escapeHtml(entry.url)}" target="_blank">${escapeHtml(entry.url)}</a>.`; | citation += `<a href="${escapeHtml(entry.url)}" target="_blank">${escapeHtml(entry.url)}</a>.`; | ||
} else { | } else { | ||
| Line 100: | Line 108: | ||
function generateTagsAttribute(tags) { | function generateTagsAttribute(tags) { | ||
if (!tags || tags.length === 0) return ''; | if (!tags || tags.length === 0) { | ||
return ' data-tags="untagged"'; | |||
} | |||
const normalized = tags | const normalized = tags | ||
.map(tag => tag.toLowerCase().replace(/\s+/g, '_')) | .map(tag => tag.toLowerCase().replace(/\s+/g, '_')) | ||
| Line 108: | Line 118: | ||
function generateTagSpans(tags) { | function generateTagSpans(tags) { | ||
if (!tags || tags.length === 0) return ''; | if (!tags || tags.length === 0) { | ||
return '<span class="tag" data-tag="untagged">untagged</span>'; | |||
} | |||
return tags | return tags | ||
.map(tag => { | .map(tag => { | ||
| Line 121: | Line 133: | ||
const tagsAttr = generateTagsAttribute(entry.tags); | const tagsAttr = generateTagsAttribute(entry.tags); | ||
const tagSpans = generateTagSpans(entry.tags); | const tagSpans = generateTagSpans(entry.tags); | ||
const abstract = entry.abstract ? escapeHtml(entry.abstract) : ''; | const abstract = entry.abstract && entry.abstract.trim() ? escapeHtml(entry.abstract) : ''; | ||
return `<!-- ${index} --> | return `<!-- ${index} --> | ||
| Line 138: | Line 150: | ||
async function generateBibliography() { | async function generateBibliography() { | ||
const output = document.getElementById('bibOutput'); | const output = document.getElementById('bibOutput'); | ||
output.value = 'Lade Daten...'; | output.value = 'Lade Daten...'; | ||
| Line 152: | Line 158: | ||
const entries = await response.json(); | const entries = await response.json(); | ||
const filtered = entries.filter(entry => { | const filtered = entries.filter(entry => { | ||
if (entry.tags && entry.tags.includes('Not relevant for Mark Twain')) { | if (entry.tags && entry.tags.includes('Not relevant for Mark Twain')) { | ||
| Line 165: | Line 169: | ||
}); | }); | ||
const allTags = new Set(); | const allTags = new Set(); | ||
filtered.forEach(entry => { | filtered.forEach(entry => { | ||
| Line 173: | Line 176: | ||
}); | }); | ||
const tagSpans = Array.from(allTags) | const tagSpans = Array.from(allTags) | ||
.sort() | .sort() | ||
| Line 197: | Line 199: | ||
output.value = html; | output.value = html; | ||
} catch (error) { | } catch (error) { | ||
output.value = 'ERROR: ' + error.message; | output.value = 'ERROR: ' + error.message; | ||
} | } | ||
} | } | ||
const btn = document.getElementById('generateBtn'); | |||
if (btn) { | |||
btn.addEventListener('click', generateBibliography); | |||
} | |||
} | |||
})(); | })(); | ||
</script> | </script> | ||
</div> | |||
</html> | </html> | ||
Latest revision as of 00:51, 12 May 2026
Bibliography Generator