-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
196 lines (171 loc) · 6.15 KB
/
script.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// script.js
function convertToBib() {
const format = document.getElementById('formatSelect').value;
const reference = document.getElementById('referenceInput').value.trim();
let bibtexEntry = '';
switch (format) {
case 'apa':
bibtexEntry = convertAPAtoBib(reference);
break;
case 'ieee':
bibtexEntry = convertIEEEtoBib(reference);
break;
case 'harvard':
bibtexEntry = convertHarvardToBib(reference);
break;
case 'mla':
bibtexEntry = convertMLAtoBib(reference);
break;
case 'chicago':
bibtexEntry = convertChicagotoBib(reference);
break;
case 'vancouver':
bibtexEntry = convertVancouverToBib(reference);
break;
case 'cse':
bibtexEntry = convertCSEtoBib(reference);
break;
case 'acs':
bibtexEntry = convertACStoBib(reference);
break;
default:
bibtexEntry = 'Unsupported format';
}
document.getElementById('bibtexOutput').value = bibtexEntry;
}
// Add escape function to handle special characters in BibTeX
function escapeBibtex(str) {
return str
.replace(/\\/g, '\\\\')
.replace(/{/g, '\\{')
.replace(/}/g, '\\}')
.replace(/"/g, '\\"')
.replace(/_/g, '\\_');
}
// Add conversion functions for new formats
function convertMLAtoBib(reference) {
const regex = /^(.+)\. (.+). (.+), (\d{4})\.$/;
const match = reference.match(regex);
if (match) {
const author = escapeBibtex(match[1]);
const title = escapeBibtex(match[2]);
const publisher = escapeBibtex(match[3]);
const year = match[4];
return `@book{${author.split(' ').join('')}${year},
author = {${author}},
title = {${title}},
publisher = {${publisher}},
year = {${year}},
}`;
}
return 'Invalid MLA format. Please use the format: Author. Title. Publisher, Year.';
}
function convertChicagotoBib(reference) {
const regex = /^(.+)\. (.+). (.+): (.+), (\d{4})\.$/;
const match = reference.match(regex);
if (match) {
const author = escapeBibtex(match[1]);
const title = escapeBibtex(match[2]);
const publisher = escapeBibtex(match[3]);
const city = escapeBibtex(match[4]);
const year = match[5];
return `@book{${author.split(' ').join('')}${year},
author = {${author}},
title = {${title}},
publisher = {${publisher}},
address = {${city}},
year = {${year}},
}`;
}
return 'Invalid Chicago format. Please use the format: Author. Title. Publisher: City, Year.';
}
function convertVancouverToBib(reference) {
const regex = /^(.+)\. (.+). (.+). (.+). (\d{4})\.$/;
const match = reference.match(regex);
if (match) {
const author = escapeBibtex(match[1]);
const title = escapeBibtex(match[2]);
const journal = escapeBibtex(match[3]);
const volume = escapeBibtex(match[4]);
const year = match[5];
return `@article{${author.split(' ').join('')}${year},
author = {${author}},
title = {${title}},
journal = {${journal}},
volume = {${volume}},
year = {${year}},
}`;
}
return 'Invalid Vancouver format. Please use the format: Author. Title. Journal. Volume, Year.';
}
function convertCSEtoBib(reference) {
const regex = /^(.+), (\d{4})\. (.+). (.+), (\d+): (\d+)-(\d+)\.$/;
const match = reference.match(regex);
if (match) {
const author = escapeBibtex(match[1]);
const year = match[2];
const title = escapeBibtex(match[3]);
const journal = escapeBibtex(match[4]);
const volume = match[5];
const startPage = match[6];
const endPage = match[7];
return `@article{${author.split(' ').join('')}${year},
author = {${author}},
title = {${title}},
journal = {${journal}},
volume = {${volume}},
pages = {${startPage}-${endPage}},
year = {${year}},
}`;
}
return 'Invalid CSE format. Please use the format: Author, Year. Title. Journal, Volume: StartPage-EndPage.';
}
function convertACStoBib(reference) {
const regex = /^(.+)\. (.+). (.+), (\d{4})\.$/;
const match = reference.match(regex);
if (match) {
const author = escapeBibtex(match[1]);
const title = escapeBibtex(match[2]);
const journal = escapeBibtex(match[3]);
const year = match[4];
return `@article{${author.split(' ').join('')}${year},
author = {${author}},
title = {${title}},
journal = {${journal}},
year = {${year}},
}`;
}
return 'Invalid ACS format. Please use the format: Author. Title. Journal, Year.';
}
function generateBibFromDOI() {
const doi = document.getElementById('doiInput').value.trim();
if (!doi) {
document.getElementById('bibtexOutput').value = 'Please enter a DOI';
return;
}
const url = `https://api.crossref.org/works/${encodeURIComponent(doi)}/transform/application/x-bibtex`;
fetch(url)
.then(response => {
if (!response.ok) throw new Error('Network response was not ok');
return response.text();
})
.then(data => {
document.getElementById('bibtexOutput').value = data;
})
.catch(error => {
document.getElementById('bibtexOutput').value = 'Failed to retrieve BibTeX entry from DOI';
console.error('Error:', error);
});
}
function generateBibFile() {
const bibtexContent = document.getElementById('bibtexOutput').value.trim();
if (!bibtexContent) {
alert('No BibTeX entry to download');
return;
}
const blob = new Blob([bibtexContent], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'reference.bib';
link.click();
}