This repository has been archived by the owner on Apr 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
zotero-sharelatex-cayw.user.js
162 lines (150 loc) · 5.11 KB
/
zotero-sharelatex-cayw.user.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
// ==UserScript==
// @version 0.10
// @name Zotero ShareLaTeX Cite-as-you-Write
// @namespace https://github.com/dlukes
// @author dlukes
// @description Insert citations from Zotero into ShareLaTeX as you write.
// @match *://sharelatex.korpus.cz/*
// @run-at document-end
// @grant unsafeWindow
// @grant GM.xmlHttpRequest
// ==/UserScript==
"use strict";
const LOG_PREFIX = "zotero-sharelatex-cayw:";
console.debug(LOG_PREFIX, "Initializing.");
const COLLECTION_RE = /-\*-\s*zotero-sharelatex-cayw-collection:\s*(.*?)\s*-\*-/;
const DROP_FIELDS = (() => {
const conf = ["abstract", "file", "keywords", "eprint", "eprinttype"];
const ans = new Set();
for (const elem of conf) {
ans.add(elem);
}
return ans;
})();
const DROP_FIELDS_FOR_TYPE = (() => {
const conf = [
["article", ["url", "urldate"]],
["incollection", ["url", "urldate"]],
["book", ["edition", "volume", "series"]]
];
const ans = new Map();
for (const pair of conf) {
const type = pair[0];
const fields = pair[1];
const field_set = new Set();
for (const field of fields) {
field_set.add(field);
}
ans.set(type, field_set);
}
return ans;
})();
const EMPTY_SET = new Set();
function cleanBib(string) {
const lines = string.match(/[^\r\n]+/g);
const clean = [];
let groups, type, key, field;
let skip = false;
for (let line of lines) {
if (line.match(/^%/)) {
continue;
} else if (groups = line.match(/^@(.*?)\{(.*),/)) {
type = groups[1];
key = groups[2];
skip = false;
} else if (groups = line.match(/^ (.*?) = \{/)) {
field = groups[1];
const drop_fields_for_type = DROP_FIELDS_FOR_TYPE.get(type) || EMPTY_SET;
skip = DROP_FIELDS.has(field) || drop_fields_for_type.has(field);
} else if (line.match(/^\}/)) {
skip = false
}
if (!skip) {
// make sure trailing commas are present
line = line.replace(/(?!^)\}$/, "},");
clean.push(line)
} else {
console.debug(LOG_PREFIX, "Removing field", field, "in entry type", type, "with key", key);
}
}
return clean.join("\n");
}
function zotError() {
const msg = "Can't reach the bibliography database! Make sure that Zotero is " +
"running and the Better BibTeX extension for Zotero is installed.";
console.error(LOG_PREFIX, msg);
alert(msg);
}
function zotWarnAndAsk() {
const msg = "No collection declaration found in file. Specify one in the following " +
"format:\n\n" +
" % -*- zotero-sharelatex-cayw-collection: <library-number>/<collection-name>.<format> -*-\n\n" +
"E.g. the following will generate a biblatex bibliography for a collection named " +
"NLP within your private Zotero library (0):\n\n" +
" % -*- zotero-sharelatex-cayw-collection: 0/NLP.biblatex -*-\n\n" +
"To figure out the identifier for a collection, right-click on the collection " +
"in Zotero, select Download Better BibTeX export, and inspect the generated " +
"URLs.\n\n" +
"As a default, I can also just try to insert a bibliography based on your " +
"entire private library, but that may take a while, depending on its size. " +
"Proceed?";
console.warn(LOG_PREFIX, msg);
return confirm(msg);
}
function getAceEditor() {
const ace = unsafeWindow.ace;
return ace.edit(document.querySelector(".ace-editor-body"));
}
function zoteroFetchAndInsert(url, postProcessFunc) {
console.debug(LOG_PREFIX, "Sending request to Better BibTeX URL", url);
GM.xmlHttpRequest({
method: "GET",
url: url,
headers: {
"Zotero-Allowed-Request": true
},
onload: function(resp) {
const editor = getAceEditor();
const content = postProcessFunc(resp.responseText);
// cursor position = an object of the form {column: x, row: y}
const cursorPosition = editor.getCursorPosition();
editor.session.insert(cursorPosition, content);
},
onerror: zotError
});
}
function zoteroInsertBibliography() {
const editor = getAceEditor();
const doc = editor.session.toString();
const match = COLLECTION_RE.exec(doc);
let collection;
if (match) {
collection = "collection?/" + match[1];
} else {
if (!zotWarnAndAsk()) return;
collection = "library?/0/library.biblatex";
}
zoteroFetchAndInsert(
"http://localhost:23119/better-bibtex/" + collection,
// TODO: you can manipulate the string before it's inserted -- e.g.
// get rid of unnecessary fields
cleanBib,
);
}
function zoteroCite() {
zoteroFetchAndInsert(
// TODO: customize citation format by modifying the URL
"http://localhost:23119/better-bibtex/cayw?format=latex",
// TODO: you can manipulate the string before it's inserted
responseText => responseText,
);
}
window.onkeyup = (ev) => {
// TODO: you can customize the keyboard shortcuts here
if (ev.ctrlKey && ev.shiftKey && ev.keyCode === 190) {
zoteroInsertBibliography();
} else if (ev.ctrlKey && ev.keyCode === 190) {
zoteroCite();
}
};
console.debug(LOG_PREFIX, "Done initializing.");