texlates/generate.go

161 lines
4.1 KiB
Go
Raw Permalink 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"
2019-08-10 00:15:00 +00:00
"strings"
"text/template"
"time"
)
var (
2019-08-14 21:53:41 +00:00
fileName = flag.String("file", "output", "name of output file")
tplPath = flag.String("tpl", "daily", "template name")
startDate = flag.String("start", "", "date of start (yyyy-mm-dd)")
2019-08-10 00:15:00 +00:00
courseDays = flag.String("courseDays", "Monday,Wednesday,Other", "days for the class template")
2019-08-14 21:53:41 +00:00
holidays = flag.String("holidays", "", "dates (yyyy-mm-dd) comma separated of holidays")
course = flag.String("course", "Cxxx", "course for lesson template")
2019-08-10 00:15:00 +00:00
courses = flag.String("courses", "Cxxx,Cyyy,Czzz", "courses for the class template")
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)
2019-08-14 21:53:41 +00:00
case "lesson":
lesson(tmpfile)
2019-08-10 00:10:28 +00:00
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())
2019-08-14 21:53:41 +00:00
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stderr
2019-08-10 00:10:28 +00:00
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{}{
2019-08-10 00:15:00 +00:00
"Courses": strings.Split(*courses, ","),
"Days": strings.Split(*courseDays, ","),
2019-08-10 00:25:12 +00:00
"DateRange": fmt.Sprintf("%s-%s", day.Format("2006/01/02"), day.Add(48*time.Hour).Format("02")),
2019-08-10 00:10:28 +00:00
"Week": fmt.Sprintf("Week %d", i+1),
}
check(body.Execute(file, info))
2019-08-10 00:39:58 +00:00
day = day.Add(24 * 7 * time.Hour)
2019-08-10 00:10:28 +00:00
}
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))
}
2019-08-14 21:53:41 +00:00
func lesson(file *os.File) {
head := template.Must(template.ParseFiles("templates/lesson/head.tpl"))
body := template.Must(template.ParseFiles("templates/lesson/body.tpl"))
foot := template.Must(template.ParseFiles("templates/lesson/foot.tpl"))
hs := strings.Split(*holidays, ",")
holdayDates := map[time.Time]bool{}
for _, h := range hs {
if h == "" {
continue
}
day, err := time.Parse("2006-01-02", h)
check(err)
holdayDates[day] = true
}
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*2; {
info := map[string]interface{}{
"Course": *course,
"Date": fmt.Sprintf("%s", day.Format("2006/01/02")),
"Day": fmt.Sprintf("Day %d", i+1),
}
if !holdayDates[day] {
check(body.Execute(file, info))
i++
}
day2 := day.Add(2 * 24 * time.Hour)
info["Date"] = fmt.Sprintf("%s", day2.Format("2006/01/02"))
info["Day"] = fmt.Sprintf("Day %d", i+1)
if !holdayDates[day2] {
check(body.Execute(file, info))
i++
}
day = day.Add(24 * 7 * time.Hour)
}
check(foot.Execute(file, nil))
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}