package templates
import (
"fmt"
"html/template"
"strings"
"time"
"github.com/mattnite/forgejo-tickets/internal/markdown"
)
func templateFuncs() template.FuncMap {
return template.FuncMap{
"formatDate": func(t time.Time) string {
return t.Format("Jan 2, 2006")
},
"formatDateTime": func(t time.Time) string {
return t.Format("Jan 2, 2006 3:04 PM")
},
"statusBadge": func(status string) template.HTML {
var class string
switch status {
case "open":
class = "bg-yellow-100 text-yellow-800"
case "in_progress":
class = "bg-blue-100 text-blue-800"
case "closed":
class = "bg-green-100 text-green-800"
default:
class = "bg-gray-100 text-gray-800"
}
label := strings.ReplaceAll(status, "_", " ")
return template.HTML(fmt.Sprintf(`%s`, class, label))
},
"renderMarkdown": func(input string, args ...interface{}) template.HTML {
var mentions map[string]string
if len(args) > 0 {
if m, ok := args[0].(map[string]string); ok {
mentions = m
}
}
return markdown.RenderMarkdown(input, mentions)
},
"isOverdue": func(t *time.Time) bool {
if t == nil {
return false
}
return t.Before(time.Now())
},
"priorityBadge": func(priority string) template.HTML {
var class string
switch priority {
case "high":
class = "bg-red-100 text-red-800"
case "medium":
class = "bg-yellow-100 text-yellow-800"
case "low":
class = "bg-gray-100 text-gray-800"
default:
return ""
}
return template.HTML(fmt.Sprintf(`%s priority`, class, priority))
},
"formatDatePtr": func(t *time.Time) string {
if t == nil {
return ""
}
return t.Format("Jan 2, 2006")
},
"truncate": func(s string, n int) string {
if len(s) <= n {
return s
}
return s[:n] + "..."
},
"deref": func(s *string) string {
if s == nil {
return ""
}
return *s
},
"dict": func(values ...interface{}) map[string]interface{} {
dict := make(map[string]interface{})
for i := 0; i < len(values)-1; i += 2 {
key, _ := values[i].(string)
dict[key] = values[i+1]
}
return dict
},
}
}