~sbinet/star-tex

star-tex/cmd/star-tex/main.go -rw-r--r-- 2.0 KiB
9bb27e8aSebastien Binet ci: bump to freebsd/latest 8 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
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
// 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.

// Command star-tex compiles TeX documents.
package main // import "star-tex.org/x/tex/cmd/star-tex"

import (
	"errors"
	"flag"
	"fmt"
	"io"
	"log"
	"os"
	"path/filepath"
	"strings"

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

var (
	fset = flag.NewFlagSet("star-tex", flag.ContinueOnError)

	usage = `Usage: star-tex [options] FILE.tex [FILE.dvi]

ex:
 $> star-tex ./testdata/hello.tex
 $> star-tex ./testdata/hello.tex ./out.dvi

options:
`
)

func main() {
	os.Exit(xmain(os.Stdout, os.Stderr, os.Args[1:]))
}

func xmain(stdout, stderr io.Writer, args []string) int {
	msg := log.New(stderr, "star-tex: ", 0)

	fset.Usage = func() {
		fmt.Fprintf(stderr, usage)
		fset.PrintDefaults()
	}

	err := fset.Parse(args)
	if err != nil {
		if errors.Is(err, flag.ErrHelp) {
			return 0
		}
		msg.Printf("could not parse args %q: %+v", args, err)
		return 1
	}

	if fset.NArg() < 1 {
		msg.Printf("missing file argument")
		fset.Usage()
		return 1
	}

	f, err := os.Open(args[0])
	if err != nil {
		msg.Printf("could not open input TeX file: %+v", err)
		return 1
	}
	defer f.Close()

	oname := strings.Replace(filepath.Base(f.Name()), ".tex", ".dvi", 1)
	if len(args) > 1 {
		oname = args[1]
	}

	o, err := os.Create(oname)
	if err != nil {
		msg.Printf("could not open output DVI file: %+v", err)
		return 1
	}
	defer o.Close()

	err = process(o, f, os.Stderr)
	if err != nil {
		msg.Printf("could not run star-tex: %+v", err)
		return 1
	}

	err = o.Close()
	if err != nil {
		msg.Printf("could not close output DVI file: %+v", err)
		return 1
	}

	return 0
}

func process(o io.Writer, f io.Reader, stderr io.Writer) error {
	ctx := tex.NewEngine(stderr, os.Stdin)
	ctx.Jobname = jobNameFrom(o)
	return ctx.Process(o, f)
}

func jobNameFrom(w io.Writer) (job string) {
	o, ok := w.(interface{ Name() string })
	if !ok {
		return job
	}
	job = o.Name()
	job = job[:len(job)-len(filepath.Ext(job))]
	return job
}