This repository has been archived by the owner on Jun 6, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mathtex.go
186 lines (156 loc) · 4.03 KB
/
mathtex.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
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
package mathtex
import (
"crypto/md5"
"encoding/hex"
"fmt"
"os"
"os/exec"
"strings"
)
var (
// MathtexPath contains path to mathtex binary
MathtexPath = "/var/www/mathtex.cgi"
// MathtexCachePath contains path to mathtex cache files
MathtexCachePath = "/var/www/cache/"
// MathtexWorkPath contains path to mathtex work files
MathtexWorkPath = "/var/www/work/"
// MathtexMsgLevel contains mathtex message level 0-99
MathtexMsgLevel = "0"
// MathtexOutputExt contains mathtex output file extenstion
MathtexOutputExt = "png"
)
// FileOut struct contains data of rendered file
type FileOut struct {
Base string
Name string
Ext string
}
// FileOut.fullname() returns full path
func (f *FileOut) fullname() string {
return f.Base + f.Name + "." + f.Ext
}
// FileOut.outpath() returns full path without .ext
func (f *FileOut) outpath() string {
return f.Base + f.Name
}
// RenderImage render LaTeX expression to PNG or SVG
func RenderImage(expr string) (string, error) {
var (
cmdArgs []string
cmdOut []byte
err error
)
err = CheckBlackList(expr)
if err != nil {
return "", err
}
// Init before using AnalyzeLatex
fileOut := FileOut{
Base: MathtexCachePath,
Name: md5hash(expr),
Ext: MathtexOutputExt,
}
expr = AnalyzeLatex(expr)
cmdArgs = []string{expr, "-m", MathtexMsgLevel, "-o", fileOut.outpath()}
if cmdOut, err = exec.Command(MathtexPath, cmdArgs...).Output(); err != nil {
return "", err
}
// Debug only
if MathtexMsgLevel != "0" {
fmt.Println(string(cmdOut))
}
if flag, err := exists(fileOut.fullname()); flag != true || err != nil {
return "", fmt.Errorf("Failed expression `%s`", expr)
}
return fileOut.fullname(), nil
}
// CheckRenderCache check cache for expression
func CheckRenderCache(expr string) (string, error) {
fileOut := FileOut{
Base: MathtexCachePath,
Name: md5hash(expr),
Ext: MathtexOutputExt,
}
if flag, err := exists(fileOut.fullname()); flag == true && err == nil {
return fileOut.fullname(), nil
}
return "", fmt.Errorf("Sorry, cache %s is not available.", fileOut.fullname())
}
// CheckBlackList parse expression and check for dangerous commands
func CheckBlackList(expr string) error {
var blacklist = []string{
`\input`,
`\write`,
`\usepackage`,
`\dpi`,
`\dvips`,
`\dvipng`,
`\noquiet`,
`\msglevel`,
`\which`,
`\switches`,
`\eval`,
`\advertisement`,
`\version`,
`\environment`,
}
for _, word := range blacklist {
if strings.Contains(expr, word) == true {
return fmt.Errorf("Sorry, command %s is not available.", word)
}
}
return nil
}
// AnalyzeLatex parse expression and add usepackage
func AnalyzeLatex(expr string) string {
eol := "\n"
conditions := map[string]string{
`eqnarray`: `eqnarray`,
`sequencediagram`: `pgf-umlsd`,
}
for cmd := range conditions {
if strings.Contains(expr, `\begin{`+cmd+`}`) == true {
expr = `\usepackage{` + conditions[cmd] + `}` + eol + expr
} else if strings.Contains(expr, `\begin{`+cmd+`*}`) == true {
expr = `\usepackage{` + conditions[cmd] + `}` + eol + expr
}
}
if strings.Contains(expr, `\addplot`) == true {
expr = `\usepackage{pgfplots}` + eol + expr
}
if strings.Contains(expr, `\xymatrix`) == true {
expr = `\usepackage[all]{xy}` + eol + expr
} else if strings.Contains(expr, `\begin{xy}`) == true {
expr = `\usepackage[all]{xy}` + eol + expr
}
if strings.Contains(expr, `picture`) == true {
expr = `\setlength{\unitlength}{1pt}` + eol + expr
}
parmodelist := []string{
`\begin{align`,
`\begin{multline`,
}
for _, word := range parmodelist {
if strings.Contains(expr, word) == true {
expr = `\parmode` + eol + expr
}
}
return expr
}
// md5hash calculate the md5 hash of a string
func md5hash(text string) string {
hasher := md5.New()
hasher.Write([]byte(text))
return hex.EncodeToString(hasher.Sum(nil))
}
// exists returns whether the given file or directory exists or not
func exists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return true, err
}