Skip to content

Commit

Permalink
localization: adds HideEmbeds func; emoji: improves replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Seklfreak committed Aug 21, 2019
1 parent 6fdf6f2 commit bf46b06
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 22 deletions.
32 changes: 10 additions & 22 deletions discord/emoji/replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ var (
// Replace replaces all :emoji: in a text with their full ID
func Replace(input string) string {
// match all :emoji: in the text
colonIndexes := colonRegex.FindAllStringIndex(input, -1)

// reverse index slice, so that the index positions are always correct when injecting text in content
for i := len(colonIndexes)/2 - 1; i >= 0; i-- {
opp := len(colonIndexes) - 1 - i
colonIndexes[i], colonIndexes[opp] = colonIndexes[opp], colonIndexes[i]
}
indexes := colonRegex.FindAllStringIndex(input, -1)

// replace all :emoji: in the text
var colonContent string
for _, colonIndex := range colonIndexes {
colonContent = input[colonIndex[0]:colonIndex[1]]
input = input[:colonIndex[0]] + Get(colonContent) + input[colonIndex[1]:]
var index []int
for i := len(indexes) - 1; i >= 0; i-- {
index = indexes[i]
input = input[:index[0]] + Get(input[index[0]:index[1]]) + input[index[1]:]
}

// return result
Expand All @@ -33,19 +27,13 @@ func Replace(input string) string {
// ReplaceWithout replaces all :emoji: in a text with their full ID
func ReplaceWithout(input string) string {
// match all :emoji: in the text
colonIndexes := colonRegex.FindAllStringIndex(input, -1)

// reverse index slice, so that the index positions are always correct when injecting text in content
for i := len(colonIndexes)/2 - 1; i >= 0; i-- {
opp := len(colonIndexes) - 1 - i
colonIndexes[i], colonIndexes[opp] = colonIndexes[opp], colonIndexes[i]
}
indexes := colonRegex.FindAllStringIndex(input, -1)

// replace all :emoji: in the text
var colonContent string
for _, colonIndex := range colonIndexes {
colonContent = input[colonIndex[0]:colonIndex[1]]
input = input[:colonIndex[0]] + Get(colonContent) + input[colonIndex[1]:]
var index []int
for i := len(indexes) - 1; i >= 0; i-- {
index = indexes[i]
input = input[:index[0]] + GetWithout(input[index[0]:index[1]]) + input[index[1]:]
}

// return result
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,5 @@ require (
google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
google.golang.org/grpc v1.21.1 // indirect
gopkg.in/h2non/gock.v1 v1.0.14 // indirect
mvdan.cc/xurls/v2 v2.0.0
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,8 @@ honnef.co/go/tools v0.0.0-20180728063816-88497007e858/go.mod h1:rf3lG4BRIbNafJWh
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
mvdan.cc/xurls/v2 v2.0.0 h1:r1zSOSNS/kqtpmATyMMMvaZ4/djsesbYz5kr0+qMRWc=
mvdan.cc/xurls/v2 v2.0.0/go.mod h1:2/webFPYOXN9jp/lzuj0zuAVlF+9g4KPFJANH1oJhRU=
pack.ag/amqp v0.8.0/go.mod h1:4/cbmt4EJXSKlG6LCfWHoqmN0uFdy5i/+YFz+fTfhV4=
pack.ag/amqp v0.11.0/go.mod h1:4/cbmt4EJXSKlG6LCfWHoqmN0uFdy5i/+YFz+fTfhV4=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
14 changes: 14 additions & 0 deletions localization/funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
polr "github.com/Seklfreak/polr-go"
"github.com/bwmarrin/discordgo"
"github.com/pkg/errors"
"mvdan.cc/xurls/v2"

humanize "github.com/dustin/go-humanize"
)
Expand All @@ -23,6 +24,7 @@ var (
polrClient *polr.Polr
shortenedLinkCache = make(map[string]string)
shortenedLinkCacheLock sync.Mutex
xurlsStrict = xurls.Strict()
)

func init() {
Expand Down Expand Up @@ -198,5 +200,17 @@ var (
"RandIntn": func(n int) int {
return rand.Intn(n)
},

"HideEmbeds": func(text string) string {
indexes := xurlsStrict.FindAllStringIndex(text, -1)

var index []int
for i := len(indexes) - 1; i >= 0; i-- {
index = indexes[i]
text = text[:index[0]] + "<" + text[index[0]:index[1]] + ">" + text[index[1]:]
}

return text
},
}
)

0 comments on commit bf46b06

Please sign in to comment.