package report import ( "bytes" _ "embed" "fmt" "html/template" "math" "strings" "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": r.applyRate, "time": fmtDateTime, "date": fmtDate, "duration": fmtDuration, "brakes": func(text string) template.HTML { return template.HTML(strings.ReplaceAll(template.HTMLEscapeString(text), "\n", "
")) }, }).Parse(htmlTemplate)) r.Style = style buf := new(bytes.Buffer) _ = tmpl.Execute(buf, r) return buf.String() } func (r Report) applyRate(dur time.Duration) string { cost := dur.Hours() * r.Rate cost = math.Round(cost/0.05) * 0.05 return fmt.Sprintf("%.2f", cost) } func fmtDateTime(t time.Time) string { return t.Format("02.08.2006 15:04") } func fmtDate(t time.Time) string { return t.Format("02.08.2006") } func fmtDuration(d time.Duration) string { return fmt.Sprintf("%.2f h", d.Hours()) } func (r Report) Total() string { dur := time.Duration(0) for _, i := range r.Issues { dur += i.Duration } return r.applyRate(dur) }