package main import ( "encoding/json" "errors" "fmt" "html/template" "io/ioutil" "math" "math/rand" "net/http" "sort" "strconv" "strings" "time" "github.com/dustin/go-humanize" "github.com/gin-gonic/gin" "github.com/jmoiron/sqlx" "github.com/russross/blackfriday" "github.com/thehowl/qsql" "golang.org/x/oauth2" "zxq.co/ripple/go-discord-oauth" "github.com/osuYozora/hanayo/modules/bbcode" "github.com/osuYozora/hanayo/modules/btcaddress" "github.com/osuYozora/hanayo/modules/doc" "github.com/osuYozora/hanayo/modules/fa-semantic-mappings" "zxq.co/ripple/playstyle" "zxq.co/ripple/rippleapi/common" ) // funcMap contains useful functions for the various templates. var funcMap = template.FuncMap{ // html disables HTML escaping on the values it is given. "html": func(value interface{}) template.HTML { return template.HTML(fmt.Sprint(value)) }, // avatars is a function returning the configuration constant AvatarURL "config": func(key string) interface{} { return configMap[key] }, // navbarItem is a function to generate an item in the navbar. // The reason why this exists is that I wanted to have the currently // selected element in the navbar having the "active" class. "navbarItem": func(currentPath, name, path string) template.HTML { var act string if path == currentPath { act = "active " } return template.HTML(fmt.Sprintf(`%s`, act, path, name)) }, // curryear returns the current year. "curryear": func() int { return time.Now().Year() }, // hasAdmin returns, based on the user's privileges, whether they should be // able to see the RAP button (aka AdminPrivilegeAccessRAP). "hasAdmin": func(privs common.UserPrivileges) bool { return privs&common.AdminPrivilegeAccessRAP > 0 }, // isRAP returns whether the current page is in RAP. "isRAP": func(p string) bool { parts := strings.Split(p, "/") return len(parts) > 1 && parts[1] == "admin" }, // favMode is just a helper function for user profiles. Basically checks // whether a float and an int are ==, and if they are it will return "active ", // so that the element in the mode menu of a user profile can be marked as // the current active element. "favMode": func(favMode float64, current int) string { if int(favMode) == current { return "active " } return "" }, // slice generates a []interface{} with the elements it is given. // useful to iterate over some elements, like this: // {{ range slice 1 2 3 }}{{ . }}{{ end }} "slice": func(els ...interface{}) []interface{} { return els }, // int converts a float/int to an int. "int": func(f interface{}) int { if f == nil { return 0 } switch f := f.(type) { case int: return f case float64: return int(f) case float32: return int(f) } return 0 }, // float converts an int to a float. "float": func(i int) float64 { return float64(i) }, // atoi converts a string to an int and then a float64. // If s is not an actual int, it returns nil. "atoi": func(s string) interface{} { i, err := strconv.Atoi(s) if err != nil { return nil } return float64(i) }, // atoint is like atoi but returns always an int. "atoint": func(s string) int { i, _ := strconv.Atoi(s) return i }, // parseUserpage compiles BBCode to HTML. "parseUserpage": func(s string) template.HTML { return template.HTML(bbcode.Compile(s)) }, // time converts a RFC3339 timestamp to the HTML element