mirror of
https://github.com/filebrowser/filebrowser.git
synced 2024-06-07 23:00:43 +00:00
96 lines
2.0 KiB
Go
96 lines
2.0 KiB
Go
|
package hugo
|
||
|
|
||
|
import (
|
||
|
"errors"
|
||
|
"log"
|
||
|
"reflect"
|
||
|
"strings"
|
||
|
"unicode"
|
||
|
)
|
||
|
|
||
|
// Defined checks if variable is defined in a struct
|
||
|
func Defined(data interface{}, field string) bool {
|
||
|
t := reflect.Indirect(reflect.ValueOf(data)).Type()
|
||
|
|
||
|
if t.Kind() != reflect.Struct {
|
||
|
log.Print("Non-struct type not allowed.")
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
_, b := t.FieldByName(field)
|
||
|
return b
|
||
|
}
|
||
|
|
||
|
// Dict allows to send more than one variable into a template
|
||
|
func Dict(values ...interface{}) (map[string]interface{}, error) {
|
||
|
if len(values)%2 != 0 {
|
||
|
return nil, errors.New("invalid dict call")
|
||
|
}
|
||
|
dict := make(map[string]interface{}, len(values)/2)
|
||
|
for i := 0; i < len(values); i += 2 {
|
||
|
key, ok := values[i].(string)
|
||
|
if !ok {
|
||
|
return nil, errors.New("dict keys must be strings")
|
||
|
}
|
||
|
dict[key] = values[i+1]
|
||
|
}
|
||
|
|
||
|
return dict, nil
|
||
|
}
|
||
|
|
||
|
// IsMap checks if some variable is a map
|
||
|
func IsMap(sth interface{}) bool {
|
||
|
return reflect.ValueOf(sth).Kind() == reflect.Map
|
||
|
}
|
||
|
|
||
|
// IsSlice checks if some variable is a slice
|
||
|
func IsSlice(sth interface{}) bool {
|
||
|
return reflect.ValueOf(sth).Kind() == reflect.Slice
|
||
|
}
|
||
|
|
||
|
// StringInSlice checks if a slice contains a string
|
||
|
func StringInSlice(a string, list []string) (bool, int) {
|
||
|
for i, b := range list {
|
||
|
if b == a {
|
||
|
return true, i
|
||
|
}
|
||
|
}
|
||
|
return false, 0
|
||
|
}
|
||
|
|
||
|
var splitCapitalizeExceptions = map[string]string{
|
||
|
"youtube": "YouTube",
|
||
|
"github": "GitHub",
|
||
|
"googleplus": "Google Plus",
|
||
|
"linkedin": "LinkedIn",
|
||
|
}
|
||
|
|
||
|
// SplitCapitalize splits a string by its uppercase letters and capitalize the
|
||
|
// first letter of the string
|
||
|
func SplitCapitalize(name string) string {
|
||
|
if val, ok := splitCapitalizeExceptions[strings.ToLower(name)]; ok {
|
||
|
return val
|
||
|
}
|
||
|
|
||
|
var words []string
|
||
|
l := 0
|
||
|
for s := name; s != ""; s = s[l:] {
|
||
|
l = strings.IndexFunc(s[1:], unicode.IsUpper) + 1
|
||
|
if l <= 0 {
|
||
|
l = len(s)
|
||
|
}
|
||
|
words = append(words, s[:l])
|
||
|
}
|
||
|
|
||
|
name = ""
|
||
|
|
||
|
for _, element := range words {
|
||
|
name += element + " "
|
||
|
}
|
||
|
|
||
|
name = strings.ToLower(name[:len(name)-1])
|
||
|
name = strings.ToUpper(string(name[0])) + name[1:]
|
||
|
|
||
|
return name
|
||
|
}
|