texlates/generate.go

107 lines
2.5 KiB
Go
Raw Normal View History

package main
import (
"flag"
2019-08-10 00:10:28 +00:00
"fmt"
"io/ioutil"
"log"
"os"
2019-08-10 00:10:28 +00:00
"os/exec"
"text/template"
"time"
)
var (
2019-08-10 00:10:28 +00:00
fileName = flag.String("file", "output", "Name of output file")
tplPath = flag.String("tpl", "daily", "Path to templates")
startDate = flag.String("start", "", "date of start (yyy-mm-dd)")
2019-08-10 00:10:28 +00:00
pages = flag.Int("pages", 7, "number of pages")
debug = flag.Bool("debug", false, "leave LaTeX log files")
)
func main() {
flag.Parse()
2019-08-10 00:10:28 +00:00
tmpfile, err := ioutil.TempFile("", "texgen")
defer os.Remove(tmpfile.Name()) // clean up
if err != nil {
log.Fatal(err)
}
switch *tplPath {
case "daily":
daily(tmpfile)
case "class":
class(tmpfile)
default:
log.Fatal("Unknown template")
}
if err := tmpfile.Close(); err != nil {
log.Fatal(err)
}
cmd := exec.Command("xelatex", "-halt-on-error", "-jobname="+*fileName, tmpfile.Name())
err = cmd.Run()
if err != nil {
log.Fatal(err)
}
if !*debug {
os.Remove(*fileName + ".aux")
os.Remove(*fileName + ".log")
}
}
func class(file *os.File) {
head := template.Must(template.ParseFiles("templates/class/head.tpl"))
body := template.Must(template.ParseFiles("templates/class/body.tpl"))
foot := template.Must(template.ParseFiles("templates/class/foot.tpl"))
if *startDate == "" {
*startDate = time.Now().Format("2006-01-02")
}
day, err := time.Parse("2006-01-02", *startDate)
check(err)
2019-08-10 00:10:28 +00:00
check(head.Execute(file, nil))
for i := 0; i < *pages; i++ {
info := map[string]interface{}{
"Courses": []string{"C311", "C346", "C490"},
"Days": []string{"Monday", "Wednesday", "Other"},
"DateRange": fmt.Sprintf("%s-%s", day.Format("2006/01/02"), day.Add(24*time.Hour).Format("02")),
"Week": fmt.Sprintf("Week %d", i+1),
}
check(body.Execute(file, info))
day = day.Add(24 * time.Hour)
}
check(foot.Execute(file, nil))
}
func daily(file *os.File) {
head := template.Must(template.ParseFiles("templates/daily/head.tpl"))
body := template.Must(template.ParseFiles("templates/daily/body.tpl"))
foot := template.Must(template.ParseFiles("templates/daily/foot.tpl"))
if *startDate == "" {
*startDate = time.Now().Format("2006-01-02")
}
day, err := time.Parse("2006-01-02", *startDate)
check(err)
check(head.Execute(file, nil))
for i := 0; i < *pages; i++ {
info := map[string]interface{}{
"Date": day.Format("2 Jan"),
"DayOfWeek": day.Format("Monday"),
}
2019-08-10 00:10:28 +00:00
check(body.Execute(file, info))
day = day.Add(24 * time.Hour)
}
2019-08-10 00:10:28 +00:00
check(foot.Execute(file, nil))
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}