-
Notifications
You must be signed in to change notification settings - Fork 14
/
latex.d
68 lines (53 loc) · 1.51 KB
/
latex.d
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
module adrdox.latex;
import std.process;
import std.file;
import arsd.dom;
string makeDataUrl(string mimeType, in void[] data) {
import std.base64;
auto data64 = Base64.encode(cast(const(ubyte[])) data);
return "data:" ~ mimeType ~ ";base64," ~ cast(string)(data64);
}
// requires latex and dvipng to be installed on your system already, it just
// calls out to them in the shell
Element mathToImgHtml(string mathCode) {
string dir = tempDir;
// FIXME: this should prolly be unique or somethign
string filebase = "./adrdox";
std.file.write(dir ~ "/" ~ filebase ~ ".latex",
`\documentclass{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\pagestyle{empty}
\begin{document}
$ `~mathCode~` $
\end{document}`
);
auto tpl = executeShell(
"latex -interaction=nonstopmode " ~ filebase ~ ".latex"
~ " && " ~
"dvipng -T tight -D 200 -o "~filebase~".png -bg Transparent "~filebase~".dvi -z 9",
null, Config.none, size_t.max, dir
);
if(tpl.status != 0)
return null;
auto prefix = dir ~ "/" ~ filebase;
if(exists(prefix ~ ".aux"))
remove(prefix ~ ".aux");
if(exists(prefix ~ ".dvi"))
remove(prefix ~ ".dvi");
if(exists(prefix ~ ".latex"))
remove(prefix ~ ".latex");
if(exists(prefix ~ ".log"))
remove(prefix ~ ".log");
if(exists(prefix ~ ".png")) {
auto file = read(prefix ~ ".png");
remove(prefix ~ ".png");
auto img = Element.make("img");
img.alt = mathCode;
img.src = makeDataUrl("image/png", file);
img.className = "rendered-math";
return img;
}
return null;
}