~sbinet/star-tex

star-tex/engine.go -rw-r--r-- 1.2 KiB
9bb27e8aSebastien Binet ci: bump to freebsd/latest 3 months ago
                                                                                
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
// Copyright ©2021 The star-tex Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package tex

import (
	"io"

	"star-tex.org/x/tex/internal/xtex"
)

const (
	defaultJobname = "output"
)

// Engine is a TeX engine.
type Engine struct {
	stdin  io.ReadCloser
	stdout io.WriteCloser

	// Jobname used for TeX output.
	// Default is "output".
	Jobname string
}

// NewEngine creates a new TeX engine connected to the provided
// stdin and stdout file descriptors.
func NewEngine(stdout io.Writer, stdin io.Reader) *Engine {
	return &Engine{
		stdout:  writerCloser(stdout),
		stdin:   io.NopCloser(stdin),
		Jobname: defaultJobname,
	}
}

// Process reads the provided TeX document and
// compiles it to the provided writer.
func (engine *Engine) Process(w io.Writer, r io.Reader) error {
	jobname := engine.Jobname
	if jobname == "" {
		jobname = defaultJobname
	}

	ctx := xtex.New(engine.stdout, engine.stdin)
	return ctx.Process(writerCloser(w), r, jobname)
}

func writerCloser(w io.Writer) io.WriteCloser {
	if w, ok := w.(io.WriteCloser); ok {
		return w
	}
	return nopWriteCloser{w}
}

type nopWriteCloser struct {
	io.Writer
}

func (nopWriteCloser) Close() error { return nil }