feat(templater): Implement unit formats

This commit is contained in:
2024-12-09 16:59:39 +01:00
parent 12f61e373f
commit 692f378eab
2 changed files with 18 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/base64"
"errors"
"fmt"
"html/template"
"image/png"
@@ -89,10 +90,22 @@ func (t *DefaultTemplater) SelectSponsorImage(id int) (string, error) {
return templates.GetImage(sponsors[id%len(sponsors)]), nil
}
func FormatUnit(unit string, amount int) (string, error) {
switch unit {
case "kilometer":
return fmt.Sprintf("%.3f", float64(amount)/1000), nil
case "euro":
return fmt.Sprintf("%.2f", float64(amount)/100), nil
default:
return "", errors.New("unknown unit")
}
}
func (t *DefaultTemplater) StringToTemplate(templateString string) (*template.Template, error) {
return template.New("template").Funcs(template.FuncMap{
"barcode": t.GenerateBarcode,
"sponsorLogo": t.SelectSponsorImage,
"formatUnit": FormatUnit,
}).Parse(templateString)
}