Source file LaTeX_pipeline.ml
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
open Forester_prelude
open Forester_core
module EP = Eio.Path
type 'a env = 'a constraint 'a = <
cwd : Eio.Fs.dir_ty EP.t;
process_mgr : _ Eio.Process.mgr;
stdout : _ Eio.Flow.sink;
..
> as 'a
let indent_string string =
string
|> String.split_on_char '\n'
|> List.map (Format.sprintf "\t%s")
|> String.concat "\n"
let latex_to_dvi ~(env : _ env) code =
let cwd = Eio.Stdenv.cwd env in
let mgr = Eio.Stdenv.process_mgr env in
Eio_util.with_open_tmp_dir ~env @@ fun tmp ->
let tex_fn = "job.tex" in
EP.save ~append:false ~create:(`Or_truncate 0o644) EP.(tmp / tex_fn) code;
let out_buf = Buffer.create 1000 in
let stdout = Eio.Flow.buffer_sink out_buf in
let stderr = Eio_util.null_sink () in
let cmd = ["latex"; "-halt-on-error"; "-interaction=nonstopmode"; tex_fn] in
begin
try
Eio.Process.run ~cwd:tmp ~stdout ~stderr mgr cmd
with exn ->
let formatted_output = Buffer.contents out_buf |> indent_string in
Reporter.fatalf External_error
"Encountered fatal LuaLaTeX error: @.@.%s@.@. while running `%s` in directory `%s`."
formatted_output
(String.concat " " cmd)
(Eio.Path.native_exn tmp)
end;
EP.load EP.(tmp / "job.dvi")
let dvi_to_svg ~env dvi =
let cwd = Eio.Stdenv.cwd env in
let mgr = Eio.Stdenv.process_mgr env in
let out_buf = Buffer.create 1000 in
let err_buf = Buffer.create 1000 in
let stdout = Eio.Flow.buffer_sink out_buf in
let stderr = Eio_util.null_sink () in
let stdin = Eio.Flow.string_source dvi in
let cmd = ["dvisvgm"; "--exact"; "--clipjoin"; "--font-format=woff"; "--bbox=papersize"; "--zoom=1.5"; "--stdin"; "--stdout"] in
begin
try Eio.Process.run ~cwd ~stdin ~stdout ~stderr mgr cmd with _ ->
Reporter.fatalf External_error "Encountered fatal error running `dvisvgm`"
end;
Buffer.contents out_buf
let latex_to_svg ~env code =
code
|> latex_to_dvi ~env
|> dvi_to_svg ~env