-
Notifications
You must be signed in to change notification settings - Fork 0
/
runlatex.js
340 lines (319 loc) · 10.7 KB
/
runlatex.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
var preincludes={};
var buttons ={
"edit": "edit",
"copy": "copy",
"Open in Overleaf": "Open in Overleaf",
"LaTeX Online": "LaTeX Online",
"Delete Output": "Delete Output",
"Compiling PDF": "Compiling PDF"
}
var editors=[];
function llexamples() {
var p = document.getElementsByTagName("pre");
var editor;
for(var i=0;i<p.length;i++) {
p[i].setAttribute("id","pre" + i);
// class=noedit on pre or {: .class :} after closing ``` in markdown
if(!(p[i].classList.contains('noedit') || p[i].parentNode.parentNode.classList.contains('noedit'))) {
// edit
var b = document.createElement("button");
b.innerText=buttons["edit"];
b.setAttribute("onclick",'allowedit("pre' + i + '")');
// p[i].parentNode.insertBefore(b, p[i]);
// copy
var c = document.createElement("button");
c.innerText=buttons["copy"];
c.setAttribute("onclick",'copytoclipboard("pre' + i + '")');
// p[i].parentNode.insertBefore(c, p[i]);
if(p[i].textContent.indexOf("\\documentclass") !== -1) {
// space
var s = document.createElement("div");
s.setAttribute("class",'spacer');
p[i].parentNode.insertBefore(s, p[i].nextSibling);
// latexonline
var r = document.createElement("button");
r.innerText=buttons["LaTeX Online"];
r.setAttribute("onclick",'latexcgi("pre' + i + '")');
r.setAttribute("id","lo-pre" + i);
p[i].parentNode.insertBefore(r, p[i].nextSibling);
// overleaf
var o = document.createElement("button");
o.innerText=buttons["Open in Overleaf"];
o.setAttribute("onclick",'openinoverleaf("pre' + i + '")');
p[i].parentNode.insertBefore(o, p[i].nextSibling);
var f=document.createElement("span");
// action=\"https://httpbin.org/post\"
// action=\"https://www.overleaf.com/docs\"
f.innerHTML="<form style=\"display:none\" id=\"form-pre" + i +"\" action=\"https://www.overleaf.com/docs\" method=\"post\" target=\"_blank\"></form>";
p[i].parentNode.insertBefore(f, p[i].nextSibling);
var f2=document.createElement("span");
// action=\"https://httpbin.org/post\"
// action=\"http://localhost/cgi-bin/p2-debug\"
// action=\"http://localhost/cgi-bin/p2\"
f2.innerHTML="<form style=\"display:none\" id=\"form2-pre" + i + "\" name=\"form2-pre" + i +"\" enctype=\"multipart/form-data\" action=\"https://latexcgi.xyz/cgi-bin/latexcgi\" method=\"post\" target=\"pre" + i + "ifr\"></form>";
p[i].parentNode.insertBefore(f2, p[i].nextSibling);
}
editor = ace.edit(p[i]);
ace.config.set('basePath', 'https://cdnjs.cloudflare.com/ajax/libs/ace/1.4.12') ;
editor.setTheme("ace/theme/textmate");
editor.getSession().setMode("ace/mode/latex");
editor.setOption("minLines",2);
editor.setOption("maxLines",100);
editor.setShowPrintMargin(false);
editor.resize();
editors["pre" + i]=editor;
}
}
}
const commentregex = / %.*/;
const engineregex = /% *!TEX.*[^a-zA-Z]((pdf|xe|lua|u?p)latex(-dev)?) *\n/i;
const returnregex = /% *!TEX.*[^a-zA-Z](pdfjs|pdf|log) *\n/i;
const makeindexregex = /% *!TEX.*[^a-zA-Z]makeindex( [a-z0-9\.\- ]*)\n/ig;
// https://www.overleaf.com/devs
function addinput(f,n,v) {
var inp=document.createElement("input");
inp.setAttribute("type","text");
inp.setAttribute("name",n);
inp.value =encodeURIComponent(v);
f.appendChild(inp);
}
function addinputnoenc(f,n,v) {
var inp=document.createElement("input");
inp.setAttribute("type","text");
inp.setAttribute("name",n);
inp.value =v;
f.appendChild(inp);
}
function addtextarea(f,n,v) {
var inp=document.createElement("textarea");
inp.setAttribute("type","text");
inp.setAttribute("name",n);
inp.textContent=v;
f.appendChild(inp);
}
function openinoverleaf(nd) {
var fm = document.getElementById('form-' + nd);
fm.innerHTML="";
var p = document.getElementById(nd);
//ace var t = p.innerText;
var t = editors[nd].getValue();
addinput(fm,"encoded_snip[]","\n" + t);
addinput(fm,"snip_name[]","document.tex");
if(typeof(preincludes) == "object") {
if(typeof(preincludes[nd]) == "object") {
var incl=preincludes[nd];
for(prop in incl) {
if(editors[prop]==null) {
addinput(fm,"encoded_snip[]",document.getElementById(prop).textContent);
} else {
addinput(fm,"encoded_snip[]",editors[prop].getValue());
}
addinput(fm,"snip_name[]",incl[prop]);
}
}
}
var engv="pdflatex";
var eng=t.match(engineregex);
if(eng != null) {
engv=eng[1].toLowerCase();
if(engv.indexOf("platex") != -1) {
addinput(fm,"encoded_snip[]","$latex = '" + engv + "';\n$bibtex = 'pbibtex';\n$dvipdf = 'dvipdfmx %O -o %D %S';");
addinput(fm,"snip_name[]","latexmkrc");
engv="latex_dvipdf";
}
} else if(t.indexOf("fontspec") !== -1) {
engv="xelatex";
}
addinput(fm,"engine",engv);
fm.submit();
}
// https://github.com/YtoTech/latex-on-http
function latexonhttpbutton(nd) {
var jsn=[];
if(typeof(preincludes) == "object") {
if(typeof(preincludes[nd]) == "object") {
var incl=preincludes[nd];
for(prop in incl) {
jsn.push({path: incl[prop],
content: document.getElementById(prop).innerText
})
}
}
}
var p = document.getElementById(nd);
var t = p.innerText;
jsn.push({main: true, content: t});
var b = document.getElementById('lo-' + nd);
var ifr= document.getElementById(nd + "ifr");
if(ifr == null) {
ifr=document.createElement("iframe");
ifr.setAttribute("width","100%");
ifr.setAttribute("height","500em");
ifr.setAttribute("id",nd + "ifr");
ifr.setAttribute("name",nd + "ifr");
p.parentNode.insertBefore(ifr, b.nextSibling);
d=document.createElement("button");
d.innerText=buttons["Delete Output"];
d.setAttribute("id","del-" + nd);
d.setAttribute("onclick",'deleteoutput("' + nd + '")');
p.parentNode.insertBefore(d, b.nextSibling);
}
var cmd="pdflatex";
var eng=t.match(engineregex);
if(eng != null) {
cmd=eng[1].toLowerCase();
} else if(t.indexOf("fontspec") !== -1) {
cmd="xelatex";
}
var fm = document.getElementById('form2-' + nd);
fm.innerHTML="";
addinputnoenc(fm,"compiler",cmd);
addinputnoenc(fm,"resources",JSON.stringify(jsn));
fm.submit();
// alert(fm.getAttribute("target"));
// alert(ifr.getAttribute("name"));
}
function latexonhttp(nd) {
var jsn=[];
if(typeof(preincludes) == "object") {
if(typeof(preincludes[nd]) == "object") {
var incl=preincludes[nd];
for(prop in incl) {
jsn.push({path: incl[prop],
content: document.getElementById(prop).innerText
})
}
}
}
var p = document.getElementById(nd);
var t = p.innerText;
// no biber support currently
if(t.indexOf("biblatex") !== -1) {
t=t.replace(/usepackage\{biblatex/,'usepackage[]\{biblatex');
t=t.replace(/\]\{biblatex/,',backend=bibtex]\{biblatex');
}
jsn.push({main: true, content: t});
var b = document.getElementById('lo-' + nd);
var ifr= document.getElementById(nd + "ifr");
if(ifr == null) {
ifr=document.createElement("iframe");
ifr.setAttribute("width","100%");
ifr.setAttribute("height","500em");
ifr.setAttribute("id",nd + "ifr");
ifr.setAttribute("name",nd + "ifr");
p.parentNode.insertBefore(ifr, b.nextSibling);
d=document.createElement("button");
d.innerText=buttons["Delete Output"];
d.setAttribute("id","del-" + nd);
d.setAttribute("onclick",'deleteoutput("' + nd + '")');
p.parentNode.insertBefore(d, b.nextSibling);
}
var cmd="pdflatex";
var eng=t.match(engineregex);
if(eng != null) {
cmd=eng[1].toLowerCase();
} else if(t.indexOf("fontspec") !== -1) {
cmd="xelatex";
}
var fm = document.getElementById('form2-' + nd);
fm.innerHTML="";
addinputnoenc(fm,"compiler",cmd);
addinputnoenc(fm,"resources",JSON.stringify(jsn));
fm.submit();
}
//
function copytoclipboard(nd){
var p = document.getElementById(nd);
var nn=document.createElement("textarea");
nn.value=p.innerText;
document.body.appendChild(nn);
nn.select();
document.execCommand("copy");
document.body.removeChild(nn);
}
function allowedit(nd){
var p = document.getElementById(nd);
p.contentEditable="true";
p.setAttribute("spellcheck","false");
p.innerHTML=p.innerText;
p.style.border="solid thin green";
}
function deleteoutput(nd){
var b = document.getElementById('del-' + nd);
var ifr = document.getElementById(nd + 'ifr');
b.parentNode.removeChild(b);
ifr.parentNode.removeChild(ifr);
}
function latexcgi(nd) {
var fm = document.getElementById('form2-' + nd);
fm.innerHTML="";
var p = document.getElementById(nd);
//ace var t = p.innerText;
var t = editors[nd].getValue();
addtextarea(fm,"filecontents[]",t);
addinputnoenc(fm,"filename[]","document.tex");
if(typeof(preincludes) == "object") {
if(typeof(preincludes[nd]) == "object") {
var incl=preincludes[nd];
for(prop in incl) {
if(editors[prop]==null) {
addtextarea(fm,"filecontents[]",document.getElementById(prop).textContent);
} else {
addtextarea(fm,"filecontents[]",editors[prop].getValue());
}
addinputnoenc(fm,"filename[]",incl[prop]);
}
}
}
var engv="pdflatex";
var eng=t.match(engineregex);
if(eng != null) {
engv=eng[1].toLowerCase();
} else if(t.indexOf("fontspec") !== -1) {
engv="xelatex";
}
addinput(fm,"engine",engv);
var rtn = t.match(returnregex);
var rtnv = "";
if(rtn == null) {
// ES6 / IE
if (typeof Symbol == "undefined") addinput(fm,"return","pdf");
} else {
rtnv=rtn[1].toLowerCase();
addinput(fm,"return",rtnv);
}
var mki = makeindexregex.exec(t);
while (mki != null) {
addinputnoenc(fm,"makeindex[]",mki[1]);
mki = makeindexregex.exec(t);
}
var b = document.getElementById('lo-' + nd);
var ifr= document.getElementById(nd + "ifr");
if(ifr == null) {
ifr=document.createElement("iframe");
ifr.setAttribute("width","100%");
ifr.setAttribute("height","500em");
ifr.setAttribute("id",nd + "ifr");
ifr.setAttribute("name",nd + "ifr");
p.parentNode.insertBefore(ifr, b.nextSibling);
d=document.createElement("button");
d.innerText=buttons["Delete Output"];
d.setAttribute("id","del-" + nd);
d.setAttribute("onclick",'deleteoutput("' + nd + '")');
p.parentNode.insertBefore(d, b.nextSibling);
}
var loading=document.createElement("div");
loading.id=nd+"load";
loading.textContent=buttons["Compiling PDF"] + " . . .";
p.parentNode.insertBefore(loading, ifr);
// scroll only if really close to the bottom
var rect = b.getBoundingClientRect();
if(document.documentElement.clientHeight - rect.bottom < 50){
window.scrollBy(0,150);
}
setTimeout(function () {
p.parentNode.removeChild(document.getElementById(nd+"load"));
}, 1000);
fm.submit();
}
window.addEventListener('load', llexamples, false);