From be406549069f6cb0503bfc5debe551f18a7262d4 Mon Sep 17 00:00:00 2001 From: Dmitry Vyukov Date: Tue, 12 Mar 2019 11:29:06 +0100 Subject: pkg/html: add more helper function Add optlink, formatKernelTime, formatCommitTableTitle. Will be useful to format bisection emails. Update #501 --- pkg/html/html.go | 60 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 49 insertions(+), 11 deletions(-) (limited to 'pkg') diff --git a/pkg/html/html.go b/pkg/html/html.go index e985ae977..a07de81d2 100644 --- a/pkg/html/html.go +++ b/pkg/html/html.go @@ -16,6 +16,7 @@ import ( "fmt" "html/template" "strings" + texttemplate "text/template" "time" "github.com/google/syzkaller/dashboard/dashapi" @@ -24,22 +25,30 @@ import ( func CreatePage(page string) *template.Template { const headTempl = `` page = strings.Replace(page, "{{HEAD}}", fmt.Sprintf(headTempl, style, js), 1) - return template.Must(template.New("").Funcs(funcs).Parse(page)) + return template.Must(template.New("").Funcs(Funcs).Parse(page)) } func CreateGlob(glob string) *template.Template { - return template.Must(template.New("").Funcs(funcs).ParseGlob(glob)) + return template.Must(template.New("").Funcs(Funcs).ParseGlob(glob)) } -var funcs = template.FuncMap{ - "link": link, - "formatTime": FormatTime, - "formatClock": formatClock, - "formatDuration": formatDuration, - "formatLateness": formatLateness, - "formatReproLevel": formatReproLevel, - "formatStat": formatStat, - "formatShortHash": formatShortHash, +func CreateTextGlob(glob string) *texttemplate.Template { + return texttemplate.Must(texttemplate.New("").Funcs(texttemplate.FuncMap(Funcs)).ParseGlob(glob)) +} + +var Funcs = template.FuncMap{ + "link": link, + "optlink": optlink, + "formatTime": FormatTime, + "formatKernelTime": formatKernelTime, + "formatClock": formatClock, + "formatDuration": formatDuration, + "formatLateness": formatLateness, + "formatReproLevel": formatReproLevel, + "formatStat": formatStat, + "formatShortHash": formatShortHash, + "formatCommitTableTitle": formatCommitTableTitle, + "formatList": formatStringList, } func link(url, text string) template.HTML { @@ -50,6 +59,13 @@ func link(url, text string) template.HTML { return template.HTML(text) } +func optlink(url, text string) template.HTML { + if url == "" { + return template.HTML("") + } + return link(url, text) +} + func FormatTime(t time.Time) string { if t.IsZero() { return "" @@ -57,6 +73,14 @@ func FormatTime(t time.Time) string { return t.Format("2006/01/02 15:04") } +func formatKernelTime(t time.Time) string { + if t.IsZero() { + return "" + } + // This is how dates appear in git log. + return t.Format("Mon Jan 2 15:04:05 2006 -0700") +} + func formatClock(t time.Time) string { if t.IsZero() { return "" @@ -117,3 +141,17 @@ func formatShortHash(v string) string { } return v[:hashLen] } + +func formatCommitTableTitle(v string) string { + // This function is very specific to how we format tables in text emails. + // Truncate commit title so that whole line fits into 78 chars. + const commitTitleLen = 51 + if len(v) <= commitTitleLen { + return v + } + return v[:commitTitleLen-2] + ".." +} + +func formatStringList(list []string) string { + return strings.Join(list, ", ") +} -- cgit mrf-deployment