From 787d2bc904f465b046f53560223382509df98624 Mon Sep 17 00:00:00 2001 From: Chris Sexton Date: Fri, 9 Aug 2019 20:10:28 -0400 Subject: [PATCH] add semester/course planner --- generate.go | 84 ++++++++++++++++++++++++++++++++------- templates/class/body.tpl | 18 +++++++++ templates/class/class.tex | 67 +++++++++++++++++++++++++++++++ templates/class/foot.tpl | 1 + templates/class/head.tpl | 38 ++++++++++++++++++ 5 files changed, 194 insertions(+), 14 deletions(-) create mode 100644 templates/class/body.tpl create mode 100644 templates/class/class.tex create mode 100644 templates/class/foot.tpl create mode 100644 templates/class/head.tpl diff --git a/generate.go b/generate.go index 87d0c14..e83c314 100644 --- a/generate.go +++ b/generate.go @@ -2,27 +2,59 @@ package main import ( "flag" + "fmt" + "io/ioutil" "log" "os" - "path" + "os/exec" "text/template" "time" ) var ( - fileName = flag.String("file", "output.tex", "Name of output file") - tplPath = flag.String("tpl", "templates/daily-planner", "Path to templates") + 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)") - days = flag.Int("days", 7, "number of days") - - output = os.Stdout + pages = flag.Int("pages", 7, "number of pages") + debug = flag.Bool("debug", false, "leave LaTeX log files") ) func main() { flag.Parse() - head := template.Must(template.ParseFiles(path.Join(*tplPath, "head.tpl"))) - body := template.Must(template.ParseFiles(path.Join(*tplPath, "body.tpl"))) - foot := template.Must(template.ParseFiles(path.Join(*tplPath, "foot.tpl"))) + 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") @@ -30,17 +62,41 @@ func main() { day, err := time.Parse("2006-01-02", *startDate) check(err) - check(head.Execute(output, nil)) - for i := 0; i < *days; i++ { + 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"), } - check(body.Execute(output, info)) + check(body.Execute(file, info)) day = day.Add(24 * time.Hour) } - check(foot.Execute(output, nil)) - + check(foot.Execute(file, nil)) } func check(err error) { diff --git a/templates/class/body.tpl b/templates/class/body.tpl new file mode 100644 index 0000000..9e4789f --- /dev/null +++ b/templates/class/body.tpl @@ -0,0 +1,18 @@ +\begin{tabular}{ |r|r|p{35mm}|p{48mm}| } + \hline + \multicolumn{4}{|c|}{ + \cellcolor{anti-flashwhite} + \noindent + \textbox{ {{.DateRange}} \hfill} + \textbox{\hfil Semester Planner \hfil} + \textbox{\hfill {{.Week}} } + }\\ + \hline +\end{tabular} + +\begin{tabular}{|m{1em}|m{\colwidth}|m{\colwidth}|m{\colwidth}|@{}m{0cm}@{}}\hline + {{range .Days}}& \centering\arraybackslash{ {{.}} } {{end}} \\\hline + {{range .Courses}} + \centering\rotatebox{90}{ {{.}} } &&& \\[75mm] \hline + {{end}} +\end{tabular} diff --git a/templates/class/class.tex b/templates/class/class.tex new file mode 100644 index 0000000..f160e21 --- /dev/null +++ b/templates/class/class.tex @@ -0,0 +1,67 @@ +\documentclass[letter]{article} +%\input \jobname + +\def \dayOfWeek{Week 9} +\def \date{2019/10/21-23} +\def \colwidth{56mm} + +\usepackage[utf8]{inputenc} +\usepackage[letterpaper, margin=1cm]{geometry} +\usepackage{array} +\usepackage{graphicx} +\usepackage{colortbl} +\usepackage[x11names]{xcolor} + +\pagenumbering{gobble} + +\renewcommand{\arraystretch}{1.6} + +\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} +\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}} +\newcolumntype{S}{>{\centering\arraybackslash} m{.4\linewidth} } + +\definecolor{anti-flashwhite}{rgb}{0.95, 0.95, 0.96} + +\newcommand*{\grayline}{% + \arrayrulecolor{anti-flashwhite} + \cline{1-3} + \arrayrulecolor{black} +} + +% TODO: Make this reactive +\newcommand\textbox[1]{% + \parbox{60.5mm}{#1}% +} + +\begin{document} +\centering + +\begin{tabular}{ |r|r|p{35mm}|p{48mm}| } + \hline + \multicolumn{4}{|c|}{ + \cellcolor{anti-flashwhite} + \noindent + \textbox{\date \hfill} + \textbox{\hfil Semester Planner \hfil} + \textbox{\hfill \dayOfWeek} + }\\ + \hline +\end{tabular} + +\begin{tabular}{|m{1em}|m{\colwidth}|m{\colwidth}|m{\colwidth}|@{}m{0cm}@{}}\hline + & \centering Monday & \centering Wednesday & \centering\arraybackslash{Other} \\\hline + \centering\rotatebox{90}{C311} &&& \\[75mm] \hline + \centering\rotatebox{90}{C346} &&& \\[75mm] \hline + \centering\rotatebox{90}{C490} &&& \\[75mm] \hline +\end{tabular} + +%\begin{tabular}{ |m{11mm}|C{54mm}|C{54mm}|C{54mm}| } +% \hline +% Course & Monday & Wednesday & Other \\\hline +% \rule{0pt}{78mm}C311 &&&\\\hline +% \rule{0pt}{78mm}C346 &&&\\\hline +% \rule{0pt}{78mm}C490 &&&\\\hline +% +%\end{tabular} + +\end{document} diff --git a/templates/class/foot.tpl b/templates/class/foot.tpl new file mode 100644 index 0000000..6b47932 --- /dev/null +++ b/templates/class/foot.tpl @@ -0,0 +1 @@ +\end{document} diff --git a/templates/class/head.tpl b/templates/class/head.tpl new file mode 100644 index 0000000..03f7c27 --- /dev/null +++ b/templates/class/head.tpl @@ -0,0 +1,38 @@ +\documentclass[letter]{article} +%\input \jobname + +\def \dayOfWeek{Week 9} +\def \date{2019/10/21-23} +\def \colwidth{56mm} + +\usepackage[utf8]{inputenc} +\usepackage[letterpaper, margin=1cm]{geometry} +\usepackage{array} +\usepackage{graphicx} +\usepackage{colortbl} +\usepackage[x11names]{xcolor} + +\pagenumbering{gobble} + +\renewcommand{\arraystretch}{1.6} + +\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}} +\newcolumntype{M}[1]{>{\centering\arraybackslash}m{#1}} +\newcolumntype{S}{>{\centering\arraybackslash} m{.4\linewidth} } + +\definecolor{anti-flashwhite}{rgb}{0.95, 0.95, 0.96} + +\newcommand*{\grayline}{% + \arrayrulecolor{anti-flashwhite} + \cline{1-3} + \arrayrulecolor{black} +} + +% TODO: Make this reactive +\newcommand\textbox[1]{% + \parbox{60.5mm}{#1}% +} + +\begin{document} +\centering +