2022-08-10 05:10:47 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2023-06-22 03:46:27 +00:00
|
|
|
"bytes"
|
2022-12-15 06:57:06 +00:00
|
|
|
"encoding/json"
|
2022-08-10 05:10:47 +00:00
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"net/http"
|
2022-08-27 02:50:33 +00:00
|
|
|
"net/url"
|
2023-11-05 21:20:41 +00:00
|
|
|
"sort"
|
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"
|
2023-06-22 03:46:27 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/models"
|
2022-08-26 04:21:46 +00:00
|
|
|
"code.nonshy.com/nonshy/website/pkg/photo"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/session"
|
|
|
|
"code.nonshy.com/nonshy/website/pkg/utility"
|
2024-01-07 00:44:05 +00:00
|
|
|
"golang.org/x/text/language"
|
|
|
|
"golang.org/x/text/message"
|
2022-08-10 05:10:47 +00:00
|
|
|
)
|
|
|
|
|
2022-09-10 19:09:46 +00:00
|
|
|
// Generics
|
|
|
|
type Number interface {
|
|
|
|
int | int64 | uint64 | float32 | float64
|
|
|
|
}
|
|
|
|
|
2022-08-10 05:10:47 +00:00
|
|
|
// TemplateFuncs available to all pages.
|
|
|
|
func TemplateFuncs(r *http.Request) template.FuncMap {
|
|
|
|
return template.FuncMap{
|
2024-01-07 00:44:05 +00:00
|
|
|
"InputCSRF": InputCSRF(r),
|
|
|
|
"SincePrettyCoarse": SincePrettyCoarse(),
|
|
|
|
"FormatNumberShort": FormatNumberShort(),
|
|
|
|
"FormatNumberCommas": FormatNumberCommas(),
|
|
|
|
"ComputeAge": utility.Age,
|
|
|
|
"Split": strings.Split,
|
|
|
|
"ToMarkdown": ToMarkdown,
|
|
|
|
"ToJSON": ToJSON,
|
|
|
|
"ToHTML": ToHTML,
|
|
|
|
"PhotoURL": photo.URLPath,
|
|
|
|
"Now": time.Now,
|
2024-03-04 01:58:18 +00:00
|
|
|
"RunTime": RunTime,
|
2022-08-13 22:39:31 +00:00
|
|
|
"PrettyTitle": func() template.HTML {
|
|
|
|
return template.HTML(fmt.Sprintf(
|
|
|
|
`<strong style="color: #0077FF">non</strong>` +
|
|
|
|
`<strong style="color: #FF77FF">shy</strong>`,
|
|
|
|
))
|
|
|
|
},
|
2023-02-14 06:31:50 +00:00
|
|
|
"PrettyTitleShort": func() template.HTML {
|
2023-05-24 03:04:17 +00:00
|
|
|
return template.HTML(`<strong style="color: #0077FF">n</strong>` +
|
|
|
|
`<strong style="color: #FF77FF">s</strong>`,
|
|
|
|
)
|
|
|
|
},
|
2022-09-10 19:09:46 +00:00
|
|
|
"Pluralize": Pluralize[int],
|
|
|
|
"Pluralize64": Pluralize[int64],
|
|
|
|
"PluralizeU64": Pluralize[uint64],
|
|
|
|
"Substring": Substring,
|
|
|
|
"TrimEllipses": TrimEllipses,
|
|
|
|
"IterRange": IterRange,
|
|
|
|
"SubtractInt": SubtractInt,
|
|
|
|
"UrlEncode": UrlEncode,
|
|
|
|
"QueryPlus": QueryPlus(r),
|
2023-06-22 03:46:27 +00:00
|
|
|
"SimplePager": SimplePager(r),
|
2023-06-26 05:55:07 +00:00
|
|
|
"HasSuffix": strings.HasSuffix,
|
2023-09-20 01:24:57 +00:00
|
|
|
|
|
|
|
// Test if a photo should be blurred ({{BlurExplicit .Photo}})
|
|
|
|
"BlurExplicit": BlurExplicit(r),
|
2024-05-09 04:03:31 +00:00
|
|
|
|
|
|
|
// Get a description for an admin scope (e.g. for transparency page).
|
|
|
|
"AdminScopeDescription": config.AdminScopeDescription,
|
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
|
|
|
|
2024-03-04 01:58:18 +00:00
|
|
|
// RunTime returns the elapsed time between the HTTP request start and now, as a formatted string.
|
|
|
|
func RunTime(r *http.Request) string {
|
|
|
|
if rt, ok := r.Context().Value(session.RequestTimeKey).(time.Time); ok {
|
|
|
|
duration := time.Since(rt)
|
|
|
|
return duration.Round(time.Millisecond).String()
|
|
|
|
}
|
|
|
|
return "ERROR"
|
|
|
|
}
|
|
|
|
|
2023-09-20 01:24:57 +00:00
|
|
|
// BlurExplicit returns true if the current user has the blur_explicit setting on and the given Photo is Explicit.
|
|
|
|
func BlurExplicit(r *http.Request) func(*models.Photo) bool {
|
|
|
|
return func(photo *models.Photo) bool {
|
|
|
|
if !photo.Explicit {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
currentUser, err := session.CurrentUser(r)
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return currentUser.GetProfileField("blur_explicit") == "true"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-22 01:12:34 +00:00
|
|
|
// FormatNumberShort will format any integer number into a short representation.
|
|
|
|
func FormatNumberShort() func(v interface{}) template.HTML {
|
|
|
|
return func(v interface{}) template.HTML {
|
|
|
|
var number int64
|
2023-12-22 01:30:34 +00:00
|
|
|
switch t := v.(type) {
|
|
|
|
case int:
|
|
|
|
number = int64(t)
|
|
|
|
case int64:
|
|
|
|
number = int64(t)
|
|
|
|
case uint:
|
|
|
|
number = int64(t)
|
|
|
|
case uint64:
|
|
|
|
number = int64(t)
|
|
|
|
case float32:
|
|
|
|
number = int64(t)
|
|
|
|
case float64:
|
|
|
|
number = int64(t)
|
2023-12-22 01:12:34 +00:00
|
|
|
default:
|
|
|
|
return template.HTML("#INVALID#")
|
|
|
|
}
|
|
|
|
return template.HTML(utility.FormatNumberShort(number))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-07 00:44:05 +00:00
|
|
|
// FormatNumberCommas will pretty print a long number by adding commas.
|
|
|
|
func FormatNumberCommas() func(v interface{}) template.HTML {
|
|
|
|
return func(v interface{}) template.HTML {
|
|
|
|
var number int64
|
|
|
|
switch t := v.(type) {
|
|
|
|
case int:
|
|
|
|
number = int64(t)
|
|
|
|
case int64:
|
|
|
|
number = int64(t)
|
|
|
|
case uint:
|
|
|
|
number = int64(t)
|
|
|
|
case uint64:
|
|
|
|
number = int64(t)
|
|
|
|
case float32:
|
|
|
|
number = int64(t)
|
|
|
|
case float64:
|
|
|
|
number = int64(t)
|
|
|
|
default:
|
|
|
|
return template.HTML("#INVALID#")
|
|
|
|
}
|
|
|
|
p := message.NewPrinter(language.English)
|
|
|
|
return template.HTML(p.Sprintf("%d", number))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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))
|
|
|
|
}
|
2022-09-10 19:09:46 +00:00
|
|
|
|
2023-09-19 00:22:50 +00:00
|
|
|
// ToHTML renders input text as trusted HTML code.
|
|
|
|
func ToHTML(input string) template.HTML {
|
|
|
|
return template.HTML(input)
|
|
|
|
}
|
|
|
|
|
2022-12-15 06:57:06 +00:00
|
|
|
// ToJSON will stringify any json-serializable object.
|
|
|
|
func ToJSON(v any) template.JS {
|
|
|
|
bin, err := json.Marshal(v)
|
|
|
|
if err != nil {
|
|
|
|
return template.JS(err.Error())
|
|
|
|
}
|
|
|
|
return template.JS(string(bin))
|
|
|
|
}
|
|
|
|
|
2022-09-10 19:09:46 +00:00
|
|
|
// Pluralize text based on a quantity number. Provide up to 2 labels for the
|
|
|
|
// singular and plural cases, or the defaults are "", "s"
|
|
|
|
func Pluralize[V Number](count V, labels ...string) string {
|
|
|
|
if len(labels) < 2 {
|
|
|
|
labels = []string{"", "s"}
|
|
|
|
}
|
|
|
|
|
|
|
|
if count == 1 {
|
|
|
|
return labels[0]
|
|
|
|
}
|
|
|
|
return labels[1]
|
|
|
|
}
|
|
|
|
|
|
|
|
// Substring safely returns the first N characters of a string.
|
|
|
|
func Substring(value string, n int) string {
|
|
|
|
if n > len(value) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return value[:n]
|
|
|
|
}
|
|
|
|
|
|
|
|
// TrimEllipses is like Substring but will add an ellipses if truncated.
|
|
|
|
func TrimEllipses(value string, n int) string {
|
|
|
|
if n > len(value) {
|
|
|
|
return value
|
|
|
|
}
|
|
|
|
return value[:n] + "…"
|
|
|
|
}
|
|
|
|
|
|
|
|
// IterRange returns a list of integers useful for pagination.
|
|
|
|
func IterRange(start, n int) []int {
|
|
|
|
var result = []int{}
|
|
|
|
for i := start; i <= n; i++ {
|
|
|
|
result = append(result, i)
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// SubtractInt subtracts two numbers.
|
|
|
|
func SubtractInt(a, b int) int {
|
|
|
|
return a - b
|
|
|
|
}
|
|
|
|
|
|
|
|
// UrlEncode escapes a series of values (joined with no delimiter)
|
|
|
|
func UrlEncode(values ...interface{}) string {
|
|
|
|
var result string
|
|
|
|
for _, value := range values {
|
|
|
|
result += url.QueryEscape(fmt.Sprintf("%v", value))
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// QueryPlus takes the current request's query parameters and upserts them with new values.
|
|
|
|
//
|
|
|
|
// Use it like: {{QueryPlus "page" .NextPage}}
|
|
|
|
//
|
|
|
|
// Returns the query string sans the ? prefix, like "key1=value1&key2=value2"
|
|
|
|
func QueryPlus(r *http.Request) func(...interface{}) template.URL {
|
|
|
|
return func(upsert ...interface{}) template.URL {
|
|
|
|
// Get current parameters.
|
2023-11-07 04:47:08 +00:00
|
|
|
// Note: COPY them from r.Form so we don't accidentally modify r.Form.
|
|
|
|
var params = map[string][]string{}
|
|
|
|
for k, v := range r.Form {
|
|
|
|
params[k] = v
|
|
|
|
}
|
2022-09-10 19:09:46 +00:00
|
|
|
|
|
|
|
// Mix in the incoming fields.
|
|
|
|
for i := 0; i < len(upsert); i += 2 {
|
|
|
|
var (
|
|
|
|
key = fmt.Sprintf("%v", upsert[i])
|
|
|
|
value interface{}
|
|
|
|
)
|
|
|
|
if len(upsert) > i {
|
|
|
|
value = upsert[i+1]
|
|
|
|
}
|
|
|
|
|
|
|
|
params[key] = []string{fmt.Sprintf("%v", value)}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Assemble and return the query string.
|
|
|
|
var parts = []string{}
|
|
|
|
for k, vs := range params {
|
|
|
|
for _, v := range vs {
|
|
|
|
parts = append(parts,
|
|
|
|
fmt.Sprintf("%s=%s", url.QueryEscape(k), url.QueryEscape(v)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2023-11-05 21:20:41 +00:00
|
|
|
|
|
|
|
// Sort them deterministically.
|
|
|
|
sort.Strings(parts)
|
|
|
|
|
2022-09-10 19:09:46 +00:00
|
|
|
return template.URL(strings.Join(parts, "&"))
|
|
|
|
}
|
|
|
|
}
|
2023-06-22 03:46:27 +00:00
|
|
|
|
|
|
|
// SimplePager creates a paginator row (partial template).
|
|
|
|
//
|
|
|
|
// Use it like: {{SimplePager .Pager}}
|
|
|
|
//
|
|
|
|
// It runs the template partial 'simple_pager.html' to customize it for the site theme.
|
|
|
|
func SimplePager(r *http.Request) func(*models.Pagination) template.HTML {
|
|
|
|
return func(pager *models.Pagination) template.HTML {
|
|
|
|
tmpl, err := template.New("index").Funcs(template.FuncMap{
|
|
|
|
"QueryPlus": QueryPlus(r),
|
|
|
|
}).ParseFiles(config.TemplatePath + "/partials/simple_pager.html")
|
|
|
|
if err != nil {
|
|
|
|
return template.HTML(err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
|
|
|
vars = struct {
|
|
|
|
Pager *models.Pagination
|
|
|
|
Request *http.Request
|
|
|
|
}{pager, r}
|
|
|
|
|
|
|
|
buf = bytes.NewBuffer([]byte{})
|
|
|
|
)
|
|
|
|
|
|
|
|
err = tmpl.ExecuteTemplate(buf, "SimplePager", vars)
|
|
|
|
if err != nil {
|
|
|
|
return template.HTML(err.Error())
|
|
|
|
}
|
|
|
|
return template.HTML(buf.String())
|
|
|
|
}
|
|
|
|
}
|