BibliographyBackbone: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
Tag: Reverted
No edit summary
 
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Seitenkopf|Bibliography Generator}}
{{Seitenkopf|Bibliography Generator}}
<html>
<html>
<button id="testBtn" style="
<div style="max-width: 1000px;">
 
<button id="generateBtn" style="
   background-color: #008CBA;
   background-color: #008CBA;
   color: white;
   color: white;
Line 10: Line 13:
   cursor: pointer;
   cursor: pointer;
   margin-bottom: 1rem;
   margin-bottom: 1rem;
">Test Years</button>
">Generate Bibliography HTML</button>


<textarea id="bibOutput" style="
<textarea id="bibOutput" style="
Line 24: Line 27:
   overflow: auto;
   overflow: auto;
"></textarea>
"></textarea>
</html>
 
<script>
<script>
async function testYears() {
(function() {
   console.log('testYears aufgerufen');
   function escapeHtml(text) {
  const output = document.getElementById('bibOutput');
    if (!text) return '';
  if (!output) {
    const map = {
    console.error('bibOutput nicht gefunden');
      '&': '&amp;',
     return;
      '<': '&lt;',
      '>': '&gt;',
      '"': '&quot;',
      "'": '&#039;'
    };
     return text.replace(/[&<>"']/g, m => map[m]);
   }
   }


   output.value = 'Lade Daten...\n';
   function getOrUnknown(value) {
    if (!value || (typeof value === 'string' && !value.trim())) {
      return 'unknown';
    }
    return value;
  }


   try {
   function formatAuthors(authors) {
     const response = await fetch('https://illus.twainframe.org/BibliographyData?action=raw');
     if (!authors || authors.length === 0) {
    console.log('Response status:', response.status);
      return 'unknown';
    }
      
      
     if (!response.ok) throw new Error(`HTTP ${response.status}`);
     if (authors.length === 1) {
      const a = authors[0];
      const family = a.family || 'unknown';
      const given = a.given || '';
      return given ? `${family}, ${given}` : family;
    }
      
      
     const entries = await response.json();
    if (authors.length === 2) {
    console.log('JSON geparst, Anzahl Einträge:', entries.length);
      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 = '';
 
    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 (container && container.trim()) {
      citation += `<i>${escapeHtml(container)}</i>. `;
    }
 
    const year = getOrUnknown(entry.year);
    citation += `${year}. `;
 
    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 '<span class="tag" data-tag="untagged">untagged</span>';
    }
    return tags
      .map(tag => {
        const normalized = tag.toLowerCase().replace(/\s+/g, '_');
        return `<span class="tag" data-tag="${normalized}">${escapeHtml(tag)}</span>`;
      })
      .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 `<!-- ${index} -->
<div class="bib-entry"${tagsAttr}>
  <h3>${mlaFormatted}</h3>
  <div class="bib-tags">
    ${tagSpans}
  </div>
  <p class="short-abstract">${abstract || '(No abstract available)'}</p>
  <button class="expand-btn">Show Abstract</button>
  <div class="abstract hidden">
    <p>${abstract || 'No abstract available.'}</p>
  </div>
</div>`;
  }
 
  async function generateBibliography() {
    const output = document.getElementById('bibOutput');
    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();
 
      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;
      });
 
      const allTags = new Set();
      filtered.forEach(entry => {
        if (entry.tags && Array.isArray(entry.tags)) {
          entry.tags.forEach(tag => allTags.add(tag));
        }
      });
 
      const tagSpans = Array.from(allTags)
        .sort()
        .map(tag => {
          const normalized = tag.toLowerCase().replace(/\s+/g, '_');
          return `<span class="tag" data-tag="${normalized}">${escapeHtml(tag)}</span>`;
        })
        .join('\n  ');
 
      const entryHTML = filtered
        .map((entry, i) => generateBibEntry(entry, i + 1))
        .join('\n\n');
 
      const html = `<div class="bib-tags">
  ${tagSpans}
</div>
 
<div id="listView" class="bibliography">
${entryHTML}
</div>


    let result = '';
<div id="networkView" class="network hidden"></div>`;
    entries.forEach((entry, index) => {
      const year = entry.year && entry.year.trim() ? entry.year : 'unknown';
      result += `${index + 1}. ${year}\n`;
    });


    output.value = result;
      output.value = html;
     console.log('Fertig');
     } 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('testBtn');
   if (btn) {
   if (btn) {
     btn.addEventListener('click', testYears);
     btn.addEventListener('click', generateBibliography);
    console.log('Button bereit');
   }
   }
});
})();
</script>
</script>
</div>
</html>

Latest revision as of 00:51, 12 May 2026

Bibliography Generator