math-optim/report/report.go
surtur 948a95fb7e
All checks were successful
continuous-integration/drone/push Build is passing
go(report,main): add flag -printreport
2022-07-18 01:58:56 +02:00

110 lines
1.8 KiB
Go

// Copyright 2022 wanderer <a_mirre at utb dot cz>
// SPDX-License-Identifier: GPL-3.0-or-later
package report
import (
_ "embed"
"fmt"
"io"
"log"
"os"
"text/template"
"time"
)
const (
outPrefix = "out/"
texDir = "tex/"
picsDir = "pics/"
)
var (
//go:embed report_base.tex
reportBase []byte
//go:embed meta.sty
metaSty []byte
//go:embed report.tmpl
tmplReportFile []byte
)
// GetPicsDirPrefix returns the path to the folder meant for tex files.
func GetTexDir() string {
return outPrefix + texDir
}
// GetPicsDirPrefix returns the path to the folder meant for plot pics.
func GetPicsDir() string {
return outPrefix + picsDir
}
func emitReportTex(fname string) error {
f, err := os.Create(fname)
if err != nil {
log.Fatal(err)
}
defer f.Close()
tmplReport := template.New("report")
tmplReport = template.Must(tmplReport.Parse(string(tmplReportFile)))
err = tmplReport.Execute(f, struct {
ReportBase string
Timestamp time.Time
}{
ReportBase: string(reportBase),
Timestamp: time.Now(),
})
if err != nil {
log.Println(err)
}
return err
}
func saveMetaSty() error {
// save meta.sty
fmeta, err := os.Create("meta.sty")
if err != nil {
log.Fatal(err)
}
defer fmeta.Close()
_, err = fmeta.Write(metaSty)
return err
}
// SaveAndPrint emits the tex files necessary to compile the report in full
// (report.tex, meta.sty) and prints the report.tex file to console.
func SaveAndPrint(doPrint bool) {
fname := "report.tex"
err := emitReportTex(fname)
if err != nil {
log.Fatal(err)
}
err = saveMetaSty()
if err != nil {
log.Fatal(err)
}
if doPrint {
fmt.Fprint(os.Stderr, "\n* printing the report...\n\n")
fh, err := os.Open(fname)
if err != nil {
log.Fatal(err)
}
_, err = io.Copy(os.Stdout, fh)
if err != nil {
log.Fatal(err)
}
}
}