text/emoji/emoji.go
package emoji
/*
https://emojipedia.org/github/
http://emojicodes.com/
https://emojipedia.org/emoji/%F0%9F%86%98/
https://unicode.org/emoji/charts/full-emoji-list.html
*/
import (
"regexp"
"strings"
)
const gomojiRaw string = `
:angry: ๐ :@
:anguished: ๐ง
:astonished: ๐ฒ
:confused: ๐ >:\
:cry: ๐ข :'(
:disappointed: ๐ :(
:dizzy_face: ๐ต #)
:expressionless: ๐ -_-
:fearful: ๐จ D:
:flushed: ๐ณ :$
:frowning: ๐ :(
:innocent: ๐ O:)
:joy: ๐ :')
:kissing_heart: :^*
:laughing: ๐ >:)
:no_mouth: :X
:ok_woman: *\0/*
:open_mouth: ๐ฎ >:O
:persevere: ๐ฃ >.<
:relaxed: ๐
:relieved: ๐
:scream: ๐ฑ
:slight_smile: ๐ :)
:smile: ๐ :)
:smiley: ๐ :D
:snowflake: โ
:snowman: โ
:stuck_out_tongue: ๐ :P
:stuck_out_tongue_winking_eye: ๐ >:P
:sunglasses: ๐ B)
:sweat: ๐ฐ ':(
:sweat_smile: ๐
':)
:victory_hand: โ
:wink: ๐ ;)
:+1: ๐ +1
:-1: ๐ -1
:love_you_gesture: ๐ค
:ok_hand: ๐
:alien: ๐ฝ
:ghost: ๐ป
:goblin: ๐บ
:ogre: ๐น
:robot: ๐ค
:skull: ๐
:beer: ๐บ
:beers: ๐ป
:boom: ๐ฅ
:chopsticks: ๐ฅข
:droplet: ๐ง
:exclamation: โ !
:fire: ๐ฅ
:minus_sign: โ
:no_entry: โ
:sos: ๐ SOS
:spoon: ๐ฅ
:sun:,:sunny: โ๏ธ
:umbrella: โ๏ธ
:white_check_mark: โ
:black_heart: ๐ค
:blue_heart; ๐
:broken_heart: ๐ </3
:green_heart: ๐
:heart: ๐งก <3
:heart_declaration: ๐
:heart_exclamation: โฃ
:orange_heart: ๐งก
:purple_heart: ๐
:red_heart: โค
:revolving_heart: ๐
:two_hearts: ๐
:yellow_heart: ๐
:kiss_mark: ๐
:love_letter: ๐
:crying_cat: ๐ฟ
:grinning_cat: ๐บ
:pouting_cat: ๐พ
:weary_cat: ๐
:black_flag: ๐ด
:checkered_flag: ๐
:crossed_flags: ๐
:rainbow_flag: ๐ณ๏ธโ๐
:triangular_flag: ๐ฉ
:white_flag: ๐ณ
`
/*
:relaxed: Y
:smiley: Y
:relieved: Y
:green_heart: Y
:+1: Y
:ok_hand: Y
:sunny: Y
:beers: Y
:white_check_mark: Y
:frowning: Y
:anguished: Y
:open_mouth: Y
:confused: Y
:scream: Y
:broken_heart: Y
:boom: Y
:exclamation: Y
:fire: Y
:-1: Y
:umbrella: Y
:sos: Y
> DOWN_EMOJI = %w(:frowning: :anguished: :open_mouth: :confused: :scream: :broken_heart: :boom: :exclamation: :fire: :-1: :umbrella: :sos:)
> UP_EMOJI = %w(:relaxed: :smiley: :relieved: :green_heart: :+1: :ok_hand: :sunny: :beers: :white_check_mark:)
*/
var rxEmojiShortcode *regexp.Regexp = regexp.MustCompile(`:[\+\-]?[0-9a-z_]+:`)
func GetEmojiDataShortcodeMap() map[string]Emoji {
data := map[string]Emoji{}
rx := regexp.MustCompile(`\s+`)
lines := strings.Split(gomojiRaw, "\n")
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.Index(line, ":") != 0 {
continue
}
line = rx.ReplaceAllString(line, " ")
parts := strings.Split(line, " ")
if len(parts) == 2 || len(parts) == 3 {
emo := Emoji{
Shortcode: strings.TrimSpace(parts[0]),
Unicode: strings.TrimSpace(parts[1]),
ShortcodeRx: regexp.MustCompile(regexp.QuoteMeta(parts[0]))}
if len(parts) == 3 {
emo.ASCII = strings.TrimSpace(parts[2])
}
data[parts[0]] = emo
} else if len(parts) > 3 {
panic("E_BAD_FORMATTING")
}
}
return data
}
type Emoji struct {
ASCII string
Shortcode string
Unicode string
ShortcodeRx *regexp.Regexp
}
type EmojiType int
const (
Shortcode EmojiType = iota
ASCII
Unicode
)
type Converter struct {
data map[string]Emoji
}
func NewConverter() Converter { return Converter{data: GetEmojiDataShortcodeMap()} }
func (conv *Converter) ConvertShortcodesString(input string, emoType EmojiType) string {
if emoType == Shortcode {
return input
}
matches := rxEmojiShortcode.FindAllString(input, -1)
output := input
for _, emoShortcode := range matches {
if einfo, ok := conv.data[emoShortcode]; ok {
if emoType == Unicode {
if len(einfo.Unicode) > 0 {
output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.Unicode)
} else {
output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.ASCII)
}
} else {
if len(einfo.ASCII) > 0 {
output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.ASCII)
} else {
output = einfo.ShortcodeRx.ReplaceAllString(output, einfo.Unicode)
}
}
}
}
return output
}