2023-08-15 17:25:00 +01:00
|
|
|
package pdf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"embed"
|
|
|
|
"fmt"
|
|
|
|
"log/slog"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"text/template"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"codeflow.dananglin.me.uk/apollo/spruce/internal/cv"
|
2023-08-21 03:07:06 +01:00
|
|
|
tf "codeflow.dananglin.me.uk/apollo/spruce/internal/templatefuncs"
|
2023-08-15 17:25:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
//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.")
|
|
|
|
|
2023-08-21 03:07:06 +01:00
|
|
|
data, err := cv.NewCVFromFile(input)
|
2023-08-15 17:25:00 +01:00
|
|
|
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,
|
|
|
|
}
|
|
|
|
|
2023-08-21 03:07:06 +01:00
|
|
|
tmpl := template.Must(template.New("cv.tmpl.tex").
|
2023-08-15 17:25:00 +01:00
|
|
|
Funcs(fmap).
|
|
|
|
Delims("<<", ">>").
|
|
|
|
ParseFS(templates, "templates/tex/*.tmpl.tex"),
|
|
|
|
)
|
|
|
|
|
2023-08-21 03:07:06 +01:00
|
|
|
if err = tmpl.Execute(file, data); err != nil {
|
2023-08-15 17:25:00 +01:00
|
|
|
return "", fmt.Errorf("unable to execute the CV template. %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
slog.Info("Tex file successfully created.", "filename", output)
|
|
|
|
|
|
|
|
return output, nil
|
|
|
|
}
|