2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2022-08-27 02:50:33 +00:00
|
|
|
"net/url"
|
2022-08-11 03:59:59 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
2022-08-10 05:10:47 +00:00
|
|
|
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/config"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/markdown"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/photo"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/utility"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TemplateFuncs available to all pages.
|
|
|
|
func TemplateFuncs(r *http.Request) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2022-08-11 03:59:59 +00:00
|
|
|
"InputCSRF": InputCSRF(r),
|
|
|
|
"SincePrettyCoarse": SincePrettyCoarse(),
|
|
|
|
"ComputeAge": utility.Age,
|
|
|
|
"Split": strings.Split,
|
|
|
|
"ToMarkdown": ToMarkdown,
|
2022-08-13 06:11:36 +00:00
|
|
|
"PhotoURL": photo.URLPath,
|
2022-08-13 22:39:31 +00:00
|
|
|
"Now": time.Now,
|
|
|
|
"PrettyTitle": func() template.HTML {
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<strong style="color: #0077FF">non</strong>` +
|
|
|
|
`<strong style="color: #FF77FF">shy</strong>`,
|
|
|
|
))
|
|
|
|
},
|
|
|
|
"Pluralize64": func(count int64, labels ...string) string {
|
|
|
|
if len(labels) < 2 {
|
|
|
|
labels = []string{"", "s"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 1 {
|
|
|
|
return labels[0]
|
|
|
|
} else {
|
|
|
|
return labels[1]
|
|
|
|
}
|
|
|
|
},
|
2022-08-21 22:40:24 +00:00
|
|
|
"Pluralize": func(count int, labels ...string) string {
|
|
|
|
if len(labels) < 2 {
|
|
|
|
labels = []string{"", "s"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 1 {
|
|
|
|
return labels[0]
|
|
|
|
} else {
|
|
|
|
return labels[1]
|
|
|
|
}
|
|
|
|
},
|
2022-08-14 00:42:42 +00:00
|
|
|
"Substring": func(value string, n int) string {
|
|
|
|
if n > len(value) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return value[:n]
|
|
|
|
},
|
2022-08-24 05:55:19 +00:00
|
|
|
"TrimEllipses": func(value string, n int) string {
|
|
|
|
if n > len(value) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return value[:n] + "…"
|
|
|
|
},
|
2022-08-14 21:40:57 +00:00
|
|
|
"IterRange": func(start, n int) []int {
|
|
|
|
var result = []int{}
|
|
|
|
for i := start; i <= n; i++ {
|
|
|
|
result = append(result, i)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
},
|
2022-08-21 22:40:24 +00:00
|
|
|
"SubtractInt": func(a, b int) int {
|
|
|
|
return a - b
|
|
|
|
},
|
2022-08-27 02:50:33 +00:00
|
|
|
"UrlEncode": func(values ...interface{}) string {
|
|
|
|
var result string
|
|
|
|
for _, value := range values {
|
|
|
|
result += url.QueryEscape(fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
},
|
2022-08-10 05:10:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// InputCSRF returns the HTML snippet for a CSRF token hidden input field.
|
|
|
|
func InputCSRF(r *http.Request) func() template.HTML {
|
|
|
|
return func() template.HTML {
|
|
|
|
ctx := r.Context()
|
|
|
|
if token, ok := ctx.Value(session.CSRFKey).(string); ok {
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<input type="hidden" name="%s" value="%s">`,
|
|
|
|
config.CSRFInputName,
|
|
|
|
token,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
return template.HTML(`[CSRF middleware error]`)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-11 03:59:59 +00:00
|
|
|
|
|
|
|
// SincePrettyCoarse formats a time.Duration in plain English. Intended for "joined 2 months ago" type
|
|
|
|
// strings - returns the coarsest level of granularity.
|
|
|
|
func SincePrettyCoarse() func(time.Time) template.HTML {
|
|
|
|
return func(since time.Time) template.HTML {
|
2022-08-14 21:40:57 +00:00
|
|
|
return template.HTML(utility.FormatDurationCoarse(time.Since(since)))
|
2022-08-11 03:59:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToMarkdown renders input text as Markdown.
|
|
|
|
func ToMarkdown(input string) template.HTML {
|
|
|
|
return template.HTML(markdown.Render(input))
|
|
|
|
}
|