import {Template} from "../mixins/template";
import {body} from "./layout";
import bibtexParse from "bibtex-parse-js";
import mustache from "mustache";
import {page} from "./layout";
let mustacheTemplate = `
{{#hasCitations}}
References
{{#citations}}
-
Unsupervised representation learning with deep convolutional generative adversarial networks
[PDF]
Radford, A., Metz, L. and Chintala, S., 2015. arXiv preprint arXiv:1511.06434.
{{/citations}}
{{/hasCitations}}
`;
export default class Bibliography extends HTMLElement {
static get is() { return "d-bibliography"; }
connectedCallback() {
let s = this.querySelector("script");
if (s) {
let bibliography = new Map();
let rawBib = s.textContent;
let parsed = bibtexParse.toJSON(rawBib);
if(parsed) {
parsed.forEach(e => {
for (var k in e.entryTags) {
var val = e.entryTags[k];
val = val.replace(/[\t\n ]+/g, " ");
val = val.replace(/{\\["^`\.'acu~Hvs]( )?([a-zA-Z])}/g,
(full, x, char) => char);
val = val.replace(/{\\([a-zA-Z])}/g,
(full, char) => char);
e.entryTags[k] = val;
}
e.entryTags.type = e.entryType;
bibliography.set(e.citationKey, e.entryTags);
});
}
this.data = bibliography;
this.render();
}
}
render() {
this.citations = [];
let citationElements = [].slice.apply(document.querySelectorAll("d-cite"));
citationElements.forEach(el => { this.cite(el) });
this.innerHTML = mustache.render(mustacheTemplate, {hasCitations: this.citations.length > 0, citations: this.citations});
}
cite(el) {
let keyString = el.getAttribute("key");
let keys = keyString ? keyString.split(",") : [];
keys.forEach(key => {
let citation = this.data[key];
if (!this.data.has(key)) {
console.error("Citation key '" + key + "' not present in bibliography.")
} else if (this.citations.indexOf(key) !== -1) {
// Bibliography entry has already been cited
} else {
this.citations.push(key);
}
})
}
}
customElements.define(Bibliography.is, Bibliography);