-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlatex.go
80 lines (69 loc) · 1.36 KB
/
latex.go
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
package main
import (
"bytes"
"errors"
"io"
"mime/multipart"
"os"
"os/exec"
"path/filepath"
)
func execute(w io.Writer, dir string) error {
out := new(bytes.Buffer)
cmd := exec.Command("latexmk", "-interaction=nonstopmode", "-jobname=out")
cmd.Dir = dir
cmd.Stdout = out
cmd.Stderr = out
if err := cmd.Run(); err != nil {
return errors.New(out.String() + err.Error())
}
// copy out
fd, err := os.Open(filepath.Join(dir, "out.pdf"))
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(w, fd)
return err
}
func savePart(r *multipart.Reader, dir string) error {
part, err := r.NextPart()
if err != nil {
return err
}
defer part.Close()
fname := part.FileName()
if fname == "" {
return errors.New("no filename specified")
}
fd, err := os.Create(filepath.Join(dir, filepath.Base(fname)))
if err != nil {
return err
}
defer fd.Close()
_, err = io.Copy(fd, part)
return err
}
func saveAllParts(r *multipart.Reader, dir string) error {
for {
err := savePart(r, dir)
if err == io.EOF {
return nil
}
if err != nil {
return err
}
}
}
func printMultipart(w io.Writer, r *multipart.Reader) error {
// create temporary working directory
dir, err := os.MkdirTemp("", "gotex")
if err != nil {
return err
}
defer os.RemoveAll(dir)
if err := saveAllParts(r, dir); err != nil {
return err
}
return execute(w, dir)
}