update citation system

This commit is contained in:
Christopher Olah
2017-01-08 13:30:56 -08:00
parent b80bac577e
commit acdce18857
3 changed files with 40 additions and 25 deletions
+7 -5
View File
@@ -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;
}
}
+30 -16
View File
@@ -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){
+3 -4
View File
@@ -50,7 +50,7 @@ export default function(dom, data) {
appendHead(`
<!-- https://scholar.google.com/intl/en/scholar/inclusion.html#indexing -->
`);
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]) )
);
}
}