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];
       return a.given ? `${a.family}, ${a.given}` : a.family;
       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 ? `${a1.family}, ${a1.given}` : a1.family;
       const name1 = (a1.family || 'unknown') + (a1.given ? ', ' + a1.given : '');
       const name2 = a2.given ? `${a2.family}, ${a2.given}` : a2.family;
       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 ? `${a.family}, ${a.given}` : a.family;
     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 = '';


     // Autoren
     const authors = formatAuthors(entry.authors);
    if (entry.authors && entry.authors.length > 0) {
    citation += authors + '. ';
      citation += formatAuthors(entry.authors) + '. ';
    }


     // Titel
     const title = getOrUnknown(entry.title);
    if (entry.title) {
    if (entry.type === 'bookSection' || entry.type === 'book') {
      if (entry.type === 'bookSection' || entry.type === 'book') {
      citation += `<i>${escapeHtml(title)}</i>. `;
        citation += `<i>${escapeHtml(entry.title)}</i>. `;
    } else {
      } else {
      citation += `"${escapeHtml(title)}." `;
        citation += `"${escapeHtml(entry.title)}." `;
      }
     }
     }


     // Container
     const container = entry.container;
     if (entry.container) {
     if (container && container.trim()) {
       citation += `<i>${escapeHtml(entry.container)}</i>. `;
       citation += `<i>${escapeHtml(container)}</i>. `;
     }
     }


     // Jahr
     const year = getOrUnknown(entry.year);
    if (entry.year) {
    citation += `${year}. `;
      citation += `${entry.year}. `;
    }


    // URL
     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() {
    console.log('generateBibliography aufgerufen');
     const output = document.getElementById('bibOutput');
     const output = document.getElementById('bibOutput');
    if (!output) {
      console.error('bibOutput not found');
      return;
    }
     output.value = 'Lade Daten...';
     output.value = 'Lade Daten...';


Line 152: Line 158:
        
        
       const entries = await response.json();
       const entries = await response.json();
      console.log('JSON geparst, ' + entries.length + ' Einträge');


      // Filtere irrelevante Einträge
       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:
       });
       });


      // Extrahiere alle Tags
       const allTags = new Set();
       const allTags = new Set();
       filtered.forEach(entry => {
       filtered.forEach(entry => {
Line 173: Line 176:
       });
       });


      // Generiere HTML
       const tagSpans = Array.from(allTags)
       const tagSpans = Array.from(allTags)
         .sort()
         .sort()
Line 197: Line 199:


       output.value = html;
       output.value = html;
      console.log('Fertig - ' + filtered.length + ' Einträge generiert');
     } catch (error) {
     } catch (error) {
       output.value = 'ERROR: ' + error.message;
       output.value = 'ERROR: ' + error.message;
      console.error('Fehler:', error);
     }
     }
   }
   }


   document.addEventListener('DOMContentLoaded', function() {
   const btn = document.getElementById('generateBtn');
    const btn = document.getElementById('generateBtn');
  if (btn) {
    if (btn) {
    btn.addEventListener('click', generateBibliography);
      btn.addEventListener('click', generateBibliography);
   }
      console.log('Generate Button bereit');
    }
   });
})();
})();
</script>
</script>
</div>
</html>
</html>

Latest revision as of 00:51, 12 May 2026

Bibliography Generator