Skip to content

Commit 7cfb7ef

Browse files
committed
allow different tex engines
1 parent 70049ab commit 7cfb7ef

File tree

7 files changed

+50
-13
lines changed

7 files changed

+50
-13
lines changed

webtex_api/exec.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,33 @@ package webtex_api
22

33
import (
44
"bytes"
5+
"fmt"
56
"log"
67
"os/exec"
8+
"strings"
79
)
810

11+
type TexEngine string
12+
13+
const (
14+
TEX_LUALATEX TexEngine = "lualatex"
15+
TEX_XELATEX TexEngine = "xelatex"
16+
)
17+
18+
func GetEngine(engine string) (TexEngine, error) {
19+
switch strings.ToLower(engine) {
20+
case "xelatex":
21+
return TEX_XELATEX, nil
22+
case "lualatex":
23+
return TEX_LUALATEX, nil
24+
default:
25+
return "", fmt.Errorf("unknown engine '%s'", engine)
26+
}
27+
}
28+
929
// execCommand runs a given command with args in the workdir
1030
func execCommand(workdir, command string, arg ...string) (string, error) {
31+
log.Println(command, arg)
1132
cmd := exec.Command(command, arg...)
1233
cmd.Dir = workdir
1334
var b bytes.Buffer
@@ -18,8 +39,8 @@ func execCommand(workdir, command string, arg ...string) (string, error) {
1839
}
1940

2041
// runTex starts latexmk to generate `render.dvi` from `in.tex` in a given directory
21-
func runTex(directory string) error {
22-
output, err := execCommand(directory, "/usr/bin/latexmk", "-lualatex", "-norc", "-dvi", "-jobname=render", "in.tex")
42+
func runTex(directory string, engine TexEngine) error {
43+
output, err := execCommand(directory, "/usr/bin/latexmk", fmt.Sprintf("-%s", engine), "-norc", "-dvi", "-jobname=render", "in.tex")
2344
if err != nil {
2445
log.Println("latexmk failed:", err)
2546
log.Println(output)

webtex_api/generate.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ type TemplateContext struct {
1616
Equation string
1717
}
1818

19-
func generateSvg(template *template.Template, equation, filename string) error {
19+
func generateSvg(template *template.Template, equation, filename string, engine TexEngine) error {
2020
dir, err := os.MkdirTemp("", "webtex_render")
2121
if err != nil {
2222
return err
@@ -40,7 +40,7 @@ func generateSvg(template *template.Template, equation, filename string) error {
4040
return err
4141
}
4242

43-
err = runTex(dir)
43+
err = runTex(dir, engine)
4444
if err != nil {
4545
return err
4646
}
@@ -74,7 +74,7 @@ func generateSvg(template *template.Template, equation, filename string) error {
7474
return err
7575
}
7676

77-
func EquationSvg(equation string, cacheDir string, template *template.Template) (string, error) {
77+
func EquationSvg(equation string, cacheDir string, template *template.Template, engine TexEngine) (string, error) {
7878
h := sha1.New()
7979
h.Write([]byte(equation))
8080
sha := hex.EncodeToString(h.Sum(nil))
@@ -85,7 +85,7 @@ func EquationSvg(equation string, cacheDir string, template *template.Template)
8585
return filenameEnd, nil
8686
} else if errors.Is(err, os.ErrNotExist) {
8787
// Generate
88-
err := generateSvg(template, equation, filename)
88+
err := generateSvg(template, equation, filename, engine)
8989
return filenameEnd, err
9090
} else {
9191
return "", err

webtex_render/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"github.com/naboj-org/webtex-render/webtex_api"
45
"os"
56
"text/template"
67
)
@@ -11,6 +12,7 @@ type Config struct {
1112
EquationDirectory string
1213
InputURL string
1314
OutputURL string
15+
Engine webtex_api.TexEngine
1416
Template *template.Template
1517
OnlyInnerHTML bool
1618
}

webtex_render/html.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func parseHtml(config Config) error {
2929
return
3030
}
3131

32-
filepath, err := webtex_api.EquationSvg(src, config.EquationDirectory, config.Template)
32+
filepath, err := webtex_api.EquationSvg(src, config.EquationDirectory, config.Template, config.Engine)
3333
if err != nil {
3434
log.Println("failed generating:", err)
3535
return

webtex_render/main.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ func main() {
1515
equationDirectory := flag.String("eqdir", "equations", "directory to output equations")
1616
inputUrl := flag.String("inurl", "eqn://", "input webtex URL prefix")
1717
outputUrl := flag.String("outurl", "equations", "image src root")
18+
engine := flag.String("engine", "lualatex", "TeX engine")
1819
templateFilename := flag.String("template", "", "TeX template file")
1920
onlyInner := flag.Bool("innerhtml", false, "export only inner HTML of the result (without <html><body> tags)")
2021
version := flag.Bool("version", false, "prints current roxy version")
@@ -51,6 +52,11 @@ func main() {
5152
config.OutputURL = *outputUrl
5253
config.OnlyInnerHTML = *onlyInner
5354

55+
config.Engine, err = webtex_api.GetEngine(*engine)
56+
if err != nil {
57+
panic(err)
58+
}
59+
5460
if *templateFilename == "" {
5561
log.Println("please provide a template file.")
5662
return

webtex_web/config.go

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,21 @@
11
package main
22

33
import (
4+
"github.com/naboj-org/webtex-render/webtex_api"
45
"gopkg.in/yaml.v3"
56
"os"
67
"path"
78
"text/template"
89
)
910

1011
type Renderer struct {
11-
Name string `yaml:"name"`
12-
Key string `yaml:"key"`
13-
TemplatePath string `yaml:"template"`
14-
Template *template.Template `yaml:"-"`
15-
CacheDir string `yaml:"-"`
12+
Name string `yaml:"name"`
13+
Key string `yaml:"key"`
14+
TemplatePath string `yaml:"template"`
15+
Template *template.Template `yaml:"-"`
16+
CacheDir string `yaml:"-"`
17+
Engine webtex_api.TexEngine `yaml:"-"`
18+
EngineString string `yaml:"engine"`
1619
}
1720

1821
type RendererMap map[string]Renderer
@@ -41,6 +44,11 @@ func loadConfig(fileName string) (RendererMap, error) {
4144
return nil, err
4245
}
4346

47+
renderer.Engine, err = webtex_api.GetEngine(renderer.EngineString)
48+
if err != nil {
49+
return nil, err
50+
}
51+
4452
rendererMap[renderer.Name] = renderer
4553
}
4654

webtex_web/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func getRender(c *gin.Context) {
4646
return
4747
}
4848

49-
outputPath, err := webtex_api.EquationSvg(string(equation), renderer.CacheDir, renderer.Template)
49+
outputPath, err := webtex_api.EquationSvg(string(equation), renderer.CacheDir, renderer.Template, renderer.Engine)
5050
if err != nil {
5151
c.String(http.StatusInternalServerError, "error generating equation")
5252
log.Printf("Error while generating: %v\n", err)

0 commit comments

Comments
 (0)