-
Notifications
You must be signed in to change notification settings - Fork 3
/
main.go
78 lines (73 loc) · 2.2 KB
/
main.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
package main
import (
"encoding/base64"
"net/http"
"os/exec"
"github.com/go-martini/martini"
)
func mathJax(expressionType string, outType string, expression string) string {
var mathJaxCmd *exec.Cmd
if expressionType == "am" {
if outType == "mml" {
mathJaxCmd = exec.Command("am2mml", expression)
} else if outType == "png" {
mathJaxCmd = exec.Command("am2png", expression)
} else if outType == "svg" {
mathJaxCmd = exec.Command("am2svg", expression)
}
} else if expressionType == "mml" {
if outType == "mml" {
mathJaxCmd = exec.Command("mml2mml", expression)
} else if outType == "png" {
mathJaxCmd = exec.Command("mml2png", expression)
} else if outType == "svg" {
mathJaxCmd = exec.Command("mml2svg", expression)
} else if outType == "svg-html5" {
mathJaxCmd = exec.Command("mml2svg-html5", expression)
}
} else if expressionType == "page" {
if outType == "mml" {
mathJaxCmd = exec.Command("page2mml", expression)
} else if outType == "png" {
mathJaxCmd = exec.Command("page2png", expression)
} else if outType == "svg" {
mathJaxCmd = exec.Command("page2svg", expression)
}
} else if expressionType == "tex" {
if outType == "mml" {
mathJaxCmd = exec.Command("tex2mml", expression)
} else if outType == "png" {
mathJaxCmd = exec.Command("tex2png", expression)
} else if outType == "svg" {
mathJaxCmd = exec.Command("tex2svg", expression)
} else if outType == "svg-filter" {
mathJaxCmd = exec.Command("tex2svg-filter", expression)
}
}
mathJaxOut, err := mathJaxCmd.Output()
if err != nil {
panic(err)
}
return string(mathJaxOut)
}
func mathJaxHandler(res http.ResponseWriter, params martini.Params) string {
var contentType string
switch params["outType"] {
case "svg":
contentType = "image/svg+xml"
case "png":
contentType = "image/png"
case "svg-html5":
contentType = "text/html"
default:
contentType = "text/html"
}
res.Header().Set("Content-Type", contentType)
expression, _ := base64.URLEncoding.DecodeString(params["expression"])
return mathJax(params["expressionType"], params["outType"], string(expression))
}
func main() {
m := martini.Classic()
m.Get("/:expressionType/:outType/:expression", mathJaxHandler)
m.Run()
}