58 lines
1.3 KiB
Go
58 lines
1.3 KiB
Go
|
package pdf
|
||
|
|
||
|
import (
|
||
|
"embed"
|
||
|
"fmt"
|
||
|
"log/slog"
|
||
|
"os"
|
||
|
"path/filepath"
|
||
|
"text/template"
|
||
|
"time"
|
||
|
|
||
|
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
|
||
|
tf "codeflow.dananglin.me.uk/apollo/spruce/internal/templateFuncs"
|
||
|
)
|
||
|
|
||
|
//go:embed templates/tex/*
|
||
|
var templates embed.FS
|
||
|
|
||
|
// tex generates the CV document as a Tex file.
|
||
|
func tex(input, tempDir string, historyLimit time.Time) (string, error) {
|
||
|
slog.Info("Creating the Tex file.")
|
||
|
|
||
|
c, err := cv.NewCVFromFile(input)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("unable to create a new CV value from %s; %w", input, err)
|
||
|
}
|
||
|
|
||
|
output := filepath.Join(tempDir, "cv.tex")
|
||
|
|
||
|
file, err := os.Create(output)
|
||
|
if err != nil {
|
||
|
return "", fmt.Errorf("unable to create output file %s; %w", output, err)
|
||
|
}
|
||
|
defer file.Close()
|
||
|
|
||
|
fmap := template.FuncMap{
|
||
|
"notLastElement": tf.NotLastElement,
|
||
|
"join": tf.JoinSentences,
|
||
|
"durationToString": tf.FormatDuration,
|
||
|
"withinTimePeriod": tf.WithinTimePeriod(historyLimit),
|
||
|
"location": tf.Location,
|
||
|
}
|
||
|
|
||
|
t := template.Must(template.New("cv.tmpl.tex").
|
||
|
Funcs(fmap).
|
||
|
Delims("<<", ">>").
|
||
|
ParseFS(templates, "templates/tex/*.tmpl.tex"),
|
||
|
)
|
||
|
|
||
|
if err = t.Execute(file, c); err != nil {
|
||
|
return "", fmt.Errorf("unable to execute the CV template. %w", err)
|
||
|
}
|
||
|
|
||
|
slog.Info("Tex file successfully created.", "filename", output)
|
||
|
|
||
|
return output, nil
|
||
|
}
|