From acdce18857dfc4b7b5b1350b6ee2198334366750 Mon Sep 17 00:00:00 2001 From: Christopher Olah Date: Sun, 8 Jan 2017 13:30:56 -0800 Subject: [PATCH] update citation system --- components/bibliography.js | 12 +++++----- components/citation.js | 46 +++++++++++++++++++++++++------------- components/meta.js | 7 +++--- 3 files changed, 40 insertions(+), 25 deletions(-) diff --git a/components/bibliography.js b/components/bibliography.js index 3848af3..08694c1 100644 --- a/components/bibliography.js +++ b/components/bibliography.js @@ -12,18 +12,20 @@ export default function(dom, data) { bibliography[e.citationKey].type = e.entryType; }); - let citations = {}; + let citations = []; var citeTags = [].slice.apply(dom.querySelectorAll("dt-cite")); citeTags.forEach(el => { let citationKeys = el.getAttribute("key").split(","); citationKeys.forEach(key => { - if (bibliography[key]) { - citations[key] = bibliography[key]; - } else { - console.warn("No bibliography entry found for: " + key); + if (citations.indexOf(key) == -1){ + citations.push(key); + if (! (key in bibliography)){ + console.warn("No bibliography entry found for: " + key); + } } }); }); + data.bibliography = bibliography; data.citations = citations; } } diff --git a/components/citation.js b/components/citation.js index 9f5e473..fe287e8 100644 --- a/components/citation.js +++ b/components/citation.js @@ -8,38 +8,52 @@ export default function(dom, data) { } var citeTags = [].slice.apply(dom.querySelectorAll("dt-cite")); - console.log(citeTags) + console.log(citeTags); citeTags.forEach(el => { var keys = el.getAttribute("key").split(","); console.log(keys) - var cite_string = keys.map(inline_cite).join(", "); + var cite_string = inline_cite_short(keys); el.innerHTML = cite_string; }); let bibEl = dom.querySelector("dt-bibliography"); if (bibEl) { let ol = dom.createElement("ol"); - citations.forEach(citation => { + citations.forEach(key => { let el = dom.createElement("li"); - el.textContent = bibliography_cite(citation); + el.textContent = bibliography_cite(data.bibliography[key]); ol.appendChild(el); }) bibEl.appendChild(ol); } - function inline_cite(key){ - console.log(key, data.citations) - if (key in data.citations){ - var ent = data.citations[key]; - var names = ent.author.split(" and "); - names = names.map(name => name.split(",")[0].trim()) - var year = ent.year; - if (names.length == 1) return names[0] + ", " + year; - if (names.length == 2) return names[0] + " & " + names[1] + ", " + year; - if (names.length > 2) return names[0] + ", et al., " + year; - } else { - return "?"; + function inline_cite_short(keys){ + function cite_string(key){ + if (key in data.bibliography){ + var n = data.citations.indexOf(key)+1; + return ""+n; + } else { + return "?"; + } } + return "["+keys.map(cite_string).join(", ")+"]"; + } + + function inline_cite_long(keys){ + function cite_string(key){ + if (key in data.bibliography){ + var ent = data.bibliography[key]; + var names = ent.author.split(" and "); + names = names.map(name => name.split(",")[0].trim()) + var year = ent.year; + if (names.length == 1) return names[0] + ", " + year; + if (names.length == 2) return names[0] + " & " + names[1] + ", " + year; + if (names.length > 2) return names[0] + ", et al., " + year; + } else { + return "?"; + } + } + return keys.map(cite_string).join(", "); } function bibliography_cite(ent){ diff --git a/components/meta.js b/components/meta.js index fa18230..7581a3b 100644 --- a/components/meta.js +++ b/components/meta.js @@ -50,7 +50,7 @@ export default function(dom, data) { appendHead(` `); - + let journal = data.journal || {}; let zeroPad = (n) => { return n < 10 ? "0" + n : n; }; let publishedYear = data.published.getFullYear(); @@ -74,9 +74,8 @@ export default function(dom, data) { }); if (data.citations) { - let citationKeys = Object.keys(data.citations); - citationKeys.forEach(key => - meta("citation_reference", citation_meta_content(data.citations[key]) ) + data.citations.forEach(key => + meta("citation_reference", citation_meta_content(data.bibliography[key]) ) ); } }