feat: html template

This commit is contained in:
u80864958
2025-08-22 11:47:34 +02:00
parent 68b0256f77
commit 2e279c9b13
14 changed files with 763 additions and 13 deletions

45
report/template.go Normal file
View File

@@ -0,0 +1,45 @@
package report
import (
"bytes"
_ "embed"
"fmt"
"html/template"
"time"
)
//go:embed template.html
var htmlTemplate string
//go:embed style.css
var style template.CSS
func (r Report) ToHTML() string {
tmpl := template.Must(
template.New("report").Funcs(template.FuncMap{
"md": mdToHTML,
"oh": offsetHTags,
"price": applyRate,
"time": fmtTime,
"duration": fmtDuration,
}).Parse(htmlTemplate))
r.Style = style
buf := new(bytes.Buffer)
tmpl.Execute(buf, r)
return buf.String()
}
func applyRate(dur time.Duration) string {
cost := dur.Hours() * 16
return fmt.Sprintf("%.2f", cost)
}
func fmtTime(t time.Time) string {
return t.Format("02.08.2006 15:04")
}
func fmtDuration(d time.Duration) string {
return fmt.Sprintf("%.2f h", d.Hours())
}