2018-01-26 08:43:53 +01:00
|
|
|
package views
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"html/template"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
var funcs = template.FuncMap{
|
2018-01-29 09:18:19 +01:00
|
|
|
"asset": assetURLFn,
|
|
|
|
"url": relURLFn,
|
2018-01-26 08:43:53 +01:00
|
|
|
"lower": lower,
|
|
|
|
"upper": upper,
|
|
|
|
"date": dateFn,
|
|
|
|
"humanDate": readableDateFn,
|
2018-01-29 09:18:19 +01:00
|
|
|
"t": translateFn,
|
2018-01-26 08:43:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func lower(input string) string {
|
|
|
|
return strings.ToLower(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func upper(input string) string {
|
|
|
|
return strings.ToUpper(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
func assetURLFn(input string) string {
|
|
|
|
url := "/static/" //os.Getenv("ASSET_URL")
|
|
|
|
return fmt.Sprintf("%s%s", url, input)
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
func relURLFn(input string) string {
|
|
|
|
url := "/" //os.Getenv("ASSET_URL")
|
|
|
|
return fmt.Sprintf("%s%s", url, input)
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:43:53 +01:00
|
|
|
func dateFn(format string, input interface{}) string {
|
|
|
|
var t time.Time
|
|
|
|
switch date := input.(type) {
|
|
|
|
default:
|
|
|
|
t = time.Now()
|
|
|
|
case time.Time:
|
|
|
|
t = date
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.Format(format)
|
|
|
|
}
|
|
|
|
|
2018-01-29 09:18:19 +01:00
|
|
|
func translateFn(language string, text string) string {
|
|
|
|
return text
|
|
|
|
}
|
|
|
|
|
2018-01-26 08:43:53 +01:00
|
|
|
func readableDateFn(t time.Time) string {
|
|
|
|
if time.Now().Before(t) {
|
|
|
|
return "in the future"
|
|
|
|
}
|
|
|
|
diff := time.Now().Sub(t)
|
|
|
|
day := 24 * time.Hour
|
|
|
|
month := 30 * day
|
|
|
|
year := 12 * month
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case diff < time.Second:
|
|
|
|
return "just now"
|
|
|
|
case diff < 5*time.Minute:
|
|
|
|
return "a few minutes ago"
|
|
|
|
case diff < time.Hour:
|
|
|
|
return fmt.Sprintf("%d minutes ago", diff/time.Minute)
|
|
|
|
case diff < day:
|
|
|
|
return fmt.Sprintf("%d hours ago", diff/time.Hour)
|
|
|
|
case diff < month:
|
|
|
|
return fmt.Sprintf("%d days ago", diff/day)
|
|
|
|
case diff < year:
|
|
|
|
return fmt.Sprintf("%d months ago", diff/month)
|
|
|
|
default:
|
|
|
|
return fmt.Sprintf("%d years ago", diff/year)
|
|
|
|
}
|
|
|
|
}
|