UNPKG

7 kBJavaScriptView Raw
1export default function(dom, data) {
2 let css = `
3 dt-cite {
4 color: hsla(206, 90%, 20%, 0.7);
5 }
6 dt-cite .citation-number {
7 cursor: default;
8 white-space: nowrap;
9 font-family: -apple-system, BlinkMacSystemFont, "Roboto", Helvetica, sans-serif;
10 font-size: 75%;
11 color: hsla(206, 90%, 20%, 0.7);
12 display: inline-block;
13 line-height: 1.1em;
14 text-align: center;
15 position: relative;
16 top: -2px;
17 margin: 0 2px;
18 }
19 figcaption dt-cite .citation-number {
20 font-size: 11px;
21 font-weight: normal;
22 top: -2px;
23 line-height: 1em;
24 }
25 `;
26
27 let style = dom.createElement("style");
28 style.textContent = css;
29 dom.querySelector("body").appendChild(style);
30
31 let citations = data.citations;
32 /*if (data.citations) {
33 citations = Object.keys(data.citations).map(c => data.citations[c]);
34 citations.sort((a, b) => {
35 return a.author.localeCompare(b.author);
36 });
37 }*/
38
39 var citeTags = [].slice.apply(dom.querySelectorAll("dt-cite"));
40 citeTags.forEach((el,n) => {
41 var key = el.getAttribute("key");
42 if (key) {
43 var keys = key.split(",");
44 var cite_string = inline_cite_short(keys);
45 var cite_hover_str = "";
46 keys.map((key,n) => {
47 if (n>0) cite_hover_str += "<br><br>";
48 cite_hover_str += hover_cite(data.bibliography[key]);
49 });
50 cite_hover_str = cite_hover_str.replace(/"/g, "&#39;")
51 var orig_string = el.innerHTML;
52 if (orig_string != "") orig_string += " ";
53 el.innerHTML = `<span id="citation-${n}" data-hover="${cite_hover_str}">${orig_string}<span class="citation-number">${cite_string}</span></span>`;
54 }
55 });
56
57 let bibEl = dom.querySelector("dt-bibliography");
58 if (bibEl) {
59 let ol = dom.createElement("ol");
60 citations.forEach(key => {
61 let el = dom.createElement("li");
62 el.innerHTML = bibliography_cite(data.bibliography[key]);
63 ol.appendChild(el);
64 })
65 bibEl.appendChild(ol);
66 }
67
68 function inline_cite_short(keys){
69 function cite_string(key){
70 if (key in data.bibliography){
71 var n = data.citations.indexOf(key)+1;
72 return ""+n;
73 } else {
74 return "?";
75 }
76 }
77 return "["+keys.map(cite_string).join(", ")+"]";
78 }
79
80 function inline_cite_long(keys){
81 function cite_string(key){
82 if (key in data.bibliography){
83 var ent = data.bibliography[key];
84 var names = ent.author.split(" and ");
85 names = names.map(name => name.split(",")[0].trim())
86 var year = ent.year;
87 if (names.length == 1) return names[0] + ", " + year;
88 if (names.length == 2) return names[0] + " & " + names[1] + ", " + year;
89 if (names.length > 2) return names[0] + ", et al., " + year;
90 } else {
91 return "?";
92 }
93 }
94 return keys.map(cite_string).join(", ");
95 }
96
97 function author_string(ent, template, sep, finalSep){
98 var names = ent.author.split(" and ");
99 let name_strings = names.map(name => {
100 name = name.trim();
101 if (name.indexOf(",") != -1){
102 var last = name.split(",")[0].trim();
103 var firsts = name.split(",")[1];
104 } else {
105 var last = name.split(" ").slice(-1)[0].trim();
106 var firsts = name.split(" ").slice(0,-1).join(" ");
107 }
108 var initials = "";
109 if (firsts != undefined) {
110 initials = firsts.trim().split(" ").map(s => s.trim()[0]);
111 initials = initials.join(".")+".";
112 }
113 return template.replace("${F}", firsts)
114 .replace("${L}", last)
115 .replace("${I}", initials);
116 });
117 if (names.length > 1) {
118 var str = name_strings.slice(0, names.length-1).join(sep);
119 str += (finalSep || sep) + name_strings[names.length-1];
120 return str;
121 } else {
122 return name_strings[0];
123 }
124 }
125
126 function venue_string(ent) {
127 var cite = (ent.journal || ent.booktitle || "")
128 if ("volume" in ent){
129 var issue = ent.issue || ent.number;
130 issue = (issue != undefined)? "("+issue+")" : "";
131 cite += ", Vol " + ent.volume + issue;
132 }
133 if ("pages" in ent){
134 cite += ", pp. " + ent.pages
135 }
136 if (cite != "") cite += ". "
137 if ("publisher" in ent){
138 cite += ent.publisher;
139 if (cite[cite.length-1] != ".") cite += ".";
140 }
141 return cite;
142 }
143
144 function link_string(ent){
145 if ("url" in ent){
146 var url = ent.url;
147 var arxiv_match = (/arxiv\.org\/abs\/([0-9\.]*)/).exec(url);
148 if (arxiv_match != null){
149 url = `http://arxiv.org/pdf/${arxiv_match[1]}.pdf`;
150 }
151
152 if (url.slice(-4) == ".pdf"){
153 var label = "PDF";
154 } else if (url.slice(-5) == ".html") {
155 var label = "HTML";
156 }
157 return ` &ensp;<a href="${url}">[${label||"link"}]</a>`;
158 }/* else if ("doi" in ent){
159 return ` &ensp;<a href="https://doi.org/${ent.doi}" >[DOI]</a>`;
160 }*/ else {
161 return "";
162 }
163 }
164 function doi_string(ent, new_line){
165 if ("doi" in ent) {
166 return `${new_line?"<br>":""} <a href="https://doi.org/${ent.doi}" style="text-decoration:inherit;">DOI: ${ent.doi}</a>`;
167 } else {
168 return "";
169 }
170 }
171
172 function bibliography_cite(ent, fancy){
173 if (ent){
174 var cite = "<b>" + ent.title + "</b> "
175 cite += link_string(ent) + "<br>";
176 cite += author_string(ent, "${L}, ${I}", ", ", " and ");
177 if (ent.year || ent.date){
178 cite += ", " + (ent.year || ent.date) + ". "
179 } else {
180 cite += ". "
181 }
182 cite += venue_string(ent);
183 cite += doi_string(ent);
184 return cite
185 /*var cite = author_string(ent, "${L}, ${I}", ", ", " and ");
186 if (ent.year || ent.date){
187 cite += ", " + (ent.year || ent.date) + ". "
188 } else {
189 cite += ". "
190 }
191 cite += "<b>" + ent.title + "</b>. ";
192 cite += venue_string(ent);
193 cite += doi_string(ent);
194 cite += link_string(ent);
195 return cite*/
196 } else {
197 return "?";
198 }
199 }
200
201 function hover_cite(ent){
202 if (ent){
203 var cite = "";
204 cite += "<b>" + ent.title + "</b>";
205 cite += link_string(ent);
206 cite += "<br>"
207
208 var a_str = author_string(ent, "${I} ${L}", ", ") + ".";
209 var v_str = venue_string(ent).trim() + " " + ent.year + ". " + doi_string(ent, true);
210
211 if ((a_str+v_str).length < Math.min(40, ent.title.length)) {
212 cite += a_str + " " + v_str;
213 } else {
214 cite += a_str + "<br>" + v_str;
215 }
216 return cite;
217 } else {
218 return "?";
219 }
220 }
221
222
223 //https://scholar.google.com/scholar?q=allintitle%3ADocument+author%3Aolah
224 function get_GS_URL(ent){
225 if (ent){
226 var names = ent.author.split(" and ");
227 names = names.map(name => name.split(",")[0].trim())
228 var title = ent.title.split(" ")//.replace(/[,:]/, "")
229 var url = "http://search.labs.crossref.org/dois?"//""https://scholar.google.com/scholar?"
230 url += uris({q: names.join(" ") + " " + title.join(" ")})
231 }
232
233 }
234}