136 lines
3.2 KiB
Go
136 lines
3.2 KiB
Go
package templates
|
|
|
|
import (
|
|
"fmt"
|
|
"html/template"
|
|
"net/url"
|
|
"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(`<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium %s">%s</span>`, 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(`<span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-xs font-medium %s">%s priority</span>`, 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
|
|
},
|
|
"add": func(a, b int) int {
|
|
return a + b
|
|
},
|
|
"sub": func(a, b int) int {
|
|
return a - b
|
|
},
|
|
"filterURL": func(baseURL string, params map[string]interface{}, setKey, setValue string) string {
|
|
u, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return baseURL
|
|
}
|
|
q := url.Values{}
|
|
for k, v := range params {
|
|
if k == "page" {
|
|
continue
|
|
}
|
|
if s, ok := v.(string); ok && s != "" {
|
|
q.Set(k, s)
|
|
}
|
|
}
|
|
if setValue != "" {
|
|
q.Set(setKey, setValue)
|
|
} else {
|
|
q.Del(setKey)
|
|
}
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
},
|
|
"pageURL": func(baseURL string, params map[string]interface{}, page int) string {
|
|
u, err := url.Parse(baseURL)
|
|
if err != nil {
|
|
return baseURL
|
|
}
|
|
q := u.Query()
|
|
for k, v := range params {
|
|
if s, ok := v.(string); ok && s != "" {
|
|
q.Set(k, s)
|
|
}
|
|
}
|
|
q.Set("page", fmt.Sprintf("%d", page))
|
|
u.RawQuery = q.Encode()
|
|
return u.String()
|
|
},
|
|
}
|
|
}
|