-
Notifications
You must be signed in to change notification settings - Fork 0
/
transformPublications.js
73 lines (67 loc) · 2.3 KB
/
transformPublications.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
const fs = require("fs");
const Cite = require('citation-js')
fs.readFile("constants/publications.bib", function(err, buf) {
let bib = buf.toString();//.replace(/@\w*{(\w*)/,(a)=>a+Math.floor(10000*Math.random()))
let output = new Cite(bib).format('bibliography', {
format: 'html',
template: 'apa',
lang: 'en-US',
prepend (entry) {
let {id, issued, DOI, type, title, volume, page} = entry;
if (DOI && !DOI.match(/http/)) {
DOI = "https://doi.org/" + DOI;
}
//get container-title from entry (journal name)
let journal = entry['container-title'];
//get year from issued
let year;
let month;
if (issued && issued['date-parts']) {
year = issued['date-parts'][0][0];
if (issued['date-parts'][0][1]) {
month = issued['date-parts'][0][1];
}
}
//get author names from entry, separated by comma and adding "and" before the last one
let author = entry.author.map((a, i) => {
if (i === entry.author.length - 1) {
return "and " + a.given + " " + a.family;
}
return a.given + " " + a.family;
}).join(", ");
return `{
${(issued && issued['date-parts']) ? ('"date": ' + '[' + issued['date-parts'] + "]," ): "" }
${ DOI ? ('"doi": "' + DOI + '",'): "" }
${ type ? ('"type": "' + type + '",'): "" }
${ type ? ('"author": "' + author + '",'): "" }
${ type ? ('"title": "' + title + '",'): "" }
${ type ? ('"journal": "' + journal + '",'): "" }
${ type ? ('"year": "' + year + '",'): "" }
${ type ? ('"month": "' + month + '",'): "" }
${ type ? ('"volume": "' + volume + '",'): "" }
${ type ? ('"pages": "' + page + '",'): "" }
"content": "`
},
append: () => '"},'
});
output = output.replace(/<div data-csl-entry-id=".*" class="csl-entry">/gim,"")
output = output.replace(/<\/div>/gim,"");
output = output.replace('<div class="csl-bib-body">',"");
output = output.replace(/,([^,]*)$/,"$1");
output = "["+output+"]";
const str = JSON.parse(output).sort(function(a, b){
if (a.date && b.date) {
return b.date[0] - a.date[0]
} else if (a.date) {
return -1;
} else if (b.date) {
return 1;
} else {
return 0;
}
});
let final_str = "export const mypublications = " + JSON.stringify(str) + ";";
fs.writeFile('constants/publications.js', final_str, (err) => {
if (err) throw err;
});
});