48 lines
1.2 KiB
Go
48 lines
1.2 KiB
Go
package ersteller
|
|
|
|
import (
|
|
"bytes"
|
|
"fmt"
|
|
mathRand "math/rand"
|
|
"regexp"
|
|
"strings"
|
|
"text/template"
|
|
)
|
|
|
|
func FirstLetterToUpper(name string) string {
|
|
return strings.ToUpper(name[:1]) + name[1:]
|
|
}
|
|
|
|
func FirstLetterToLower(name string) string {
|
|
return strings.ToLower(name[:1]) + name[1:]
|
|
}
|
|
|
|
func InlineTemplate(templateString string, data any) string {
|
|
dollarRegex := regexp.MustCompile(`\$(\w+)\$`)
|
|
templateString = dollarRegex.ReplaceAllStringFunc(templateString, func(s string) string {
|
|
if strings.HasPrefix(s, "$.") || strings.HasPrefix(s, "$end") || strings.HasPrefix(s, "$else") {
|
|
return s
|
|
}
|
|
return dollarRegex.ReplaceAllString(s, "$.$1$")
|
|
})
|
|
tmpl, err := template.New("inline").Delims("$", "$").Parse(templateString)
|
|
if err != nil {
|
|
return fmt.Sprint("Failed to parse template ", err)
|
|
}
|
|
var buf bytes.Buffer
|
|
err = tmpl.Execute(&buf, data)
|
|
if err != nil {
|
|
return fmt.Sprint("Failed to execute template ", err)
|
|
}
|
|
return buf.String()
|
|
}
|
|
|
|
func RandomString(n int) string {
|
|
const letters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
|
b := make([]byte, n)
|
|
for i := range b {
|
|
b[i] = letters[int(mathRand.Int63()%int64(len(letters)))]
|
|
}
|
|
return string(b)
|
|
}
|