46 lines
797 B
Go
46 lines
797 B
Go
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())
|
|
}
|