f76d184173
Replaced the hardcoded height attribute in the emote img tags with a class attribute. This allows the emotes to be resized with the change of a CSS value instead of recompiling the server. Resolves #150
88 lines
1.8 KiB
Go
88 lines
1.8 KiB
Go
package common
|
|
|
|
import (
|
|
"fmt"
|
|
"path/filepath"
|
|
"regexp"
|
|
"strings"
|
|
)
|
|
|
|
type EmotesMap map[string]string
|
|
|
|
var Emotes EmotesMap
|
|
var WrappedEmotesOnly bool = false
|
|
|
|
var (
|
|
reStripStatic = regexp.MustCompile(`^(\\|/)?static`)
|
|
reWrappedEmotes = regexp.MustCompile(`[:\[][^\s:\/\\\?=#\]\[]+[:\]]`)
|
|
)
|
|
|
|
func init() {
|
|
Emotes = NewEmotesMap()
|
|
}
|
|
|
|
func NewEmotesMap() EmotesMap {
|
|
return map[string]string{}
|
|
}
|
|
|
|
func (em EmotesMap) Add(fullpath string) EmotesMap {
|
|
fullpath = reStripStatic.ReplaceAllLiteralString(fullpath, "")
|
|
|
|
base := filepath.Base(fullpath)
|
|
code := base[0 : len(base)-len(filepath.Ext(base))]
|
|
|
|
_, exists := em[code]
|
|
|
|
num := 0
|
|
for exists {
|
|
num += 1
|
|
_, exists = em[fmt.Sprintf("%s-%d", code, num)]
|
|
}
|
|
|
|
if num > 0 {
|
|
code = fmt.Sprintf("%s-%d", code, num)
|
|
}
|
|
|
|
em[code] = fullpath
|
|
return em
|
|
}
|
|
|
|
func EmoteToHtml(file, title string) string {
|
|
return fmt.Sprintf(`<img src="%s" class="emote" title="%s" />`, file, title)
|
|
}
|
|
|
|
// Used with a regexp.ReplaceAllStringFunc() call. Needs to lookup the value as it
|
|
// cannot be passed in with the regex function call.
|
|
func emoteToHmtl2(key string) string {
|
|
key = strings.Trim(key, ":[]")
|
|
if val, ok := Emotes[key]; ok {
|
|
return fmt.Sprintf(`<img src="%s" class="emote" title="%s" />`, val, key)
|
|
}
|
|
return key
|
|
}
|
|
|
|
func ParseEmotesArray(words []string) []string {
|
|
newWords := []string{}
|
|
for _, word := range words {
|
|
found := false
|
|
if !WrappedEmotesOnly {
|
|
if val, ok := Emotes[word]; ok {
|
|
newWords = append(newWords, EmoteToHtml(val, word))
|
|
found = true
|
|
}
|
|
}
|
|
|
|
if !found {
|
|
word = reWrappedEmotes.ReplaceAllStringFunc(word, emoteToHmtl2)
|
|
newWords = append(newWords, word)
|
|
}
|
|
}
|
|
|
|
return newWords
|
|
}
|
|
|
|
func ParseEmotes(msg string) string {
|
|
words := ParseEmotesArray(strings.Split(msg, " "))
|
|
return strings.Join(words, " ")
|
|
}
|