Skip to content

Commit

Permalink
Fix support unicode v11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
kyokomi committed Feb 2, 2019
1 parent bc10b9e commit d293807
Show file tree
Hide file tree
Showing 8 changed files with 2,534 additions and 1,620 deletions.
5 changes: 3 additions & 2 deletions cmd/generateEmojiCodeMap/emoji_v5.go
Expand Up @@ -42,10 +42,11 @@ func createEmojoCodeMap() (map[string]string, error) {
emojiCodeMap := make(map[string]string)
for _, gemoji := range gs {
shortCode := strings.Replace(gemoji.Shortcode, ":", "", 2)
if len(shortCode) == 0 {
if len(shortCode) == 0 || len(gemoji.Emoji) == 0 {
continue
}
emojiCodeMap[shortCode] = fmt.Sprintf("%+q", gemoji.Emoji)
code := gemoji.Emoji
emojiCodeMap[shortCode] = fmt.Sprintf("%+q", strings.ToLower(code))
}

return emojiCodeMap, nil
Expand Down
7 changes: 6 additions & 1 deletion cmd/generateEmojiCodeMap/gemoji.go
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)

const gemojiDBJsonURL = "https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json"
Expand Down Expand Up @@ -37,7 +38,11 @@ func createGemojiCodeMap() (map[string]string, error) {
emojiCodeMap := make(map[string]string)
for _, gemoji := range gs {
for _, a := range gemoji.Aliases {
emojiCodeMap[a] = fmt.Sprintf("%+q", gemoji.Emoji)
if len(a) == 0 || len(gemoji.Emoji) == 0 {
continue
}
code := gemoji.Emoji
emojiCodeMap[a] = fmt.Sprintf("%+q", strings.ToLower(code))
}
}

Expand Down
8 changes: 8 additions & 0 deletions cmd/generateEmojiCodeMap/main.go
Expand Up @@ -55,6 +55,14 @@ func createCodeMap() (map[string]string, error) {
gemojiCodeMap[k] = v
}

unicodeorgCodeMap, err := createUnicodeorgMap()
if err != nil {
return nil, err
}
for k, v := range unicodeorgCodeMap {
gemojiCodeMap[k] = v
}

return gemojiCodeMap, nil
}

Expand Down
92 changes: 92 additions & 0 deletions cmd/generateEmojiCodeMap/unicodeorg.go
@@ -0,0 +1,92 @@
package main

import (
"fmt"
"io"
"log"
"net/http"
"strconv"
"strings"

"github.com/PuerkitoBio/goquery"
)

const unicodeorgURL = "http://www.unicode.org/emoji/charts/emoji-list.html"

func createUnicodeorgMap() (map[string]string, error) {
res, err := http.Get(unicodeorgURL)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return nil, fmt.Errorf("status code error: %d %s", res.StatusCode, res.Status)
}

return generateUnicodeorgCodeMap(res.Body)
}

// UnicodeorgEmoji unicode.org emoji
type UnicodeorgEmoji struct {
No int
Code string
ShortName string
OtherKeywords []string
}

var shortNameReplaces = []string{
":", "",
",", "",
"⊛", "", // \U+229B
"“", "", // \U+201C
"”", "", // \U+201D
}

func generateUnicodeorgCodeMap(body io.ReadCloser) (map[string]string, error) {
doc, err := goquery.NewDocumentFromReader(body)
if err != nil {
return nil, err
}

var emojis []*UnicodeorgEmoji
doc.Find("table").First().Find("tr").Each(func(i int, selection *goquery.Selection) {
var cols []string
selection.Find("td").Each(func(j int, s *goquery.Selection) {
cols = append(cols, s.Text())
})

if len(cols) != 5 {
return
}

unicodeEmoji := UnicodeorgEmoji{}
unicodeEmoji.No, err = strconv.Atoi(cols[0])
if err != nil {
log.Println("ERROR: no", err)
return
}
codes := strings.Fields(cols[1])
for _, code := range codes {
if len(code) == 6 {
unicodeEmoji.Code += strings.Replace(code, "+", "0000", 1)
} else {
unicodeEmoji.Code += strings.Replace(code, "+", "000", 1)
}
}
unicodeEmoji.Code = strings.Replace(unicodeEmoji.Code, "U", "\\U", -1)

shortName := strings.NewReplacer(shortNameReplaces...).Replace(cols[3])
unicodeEmoji.ShortName = strings.Replace(strings.TrimSpace(shortName), " ", "_", -1)

unicodeEmoji.OtherKeywords = strings.Fields(cols[4])

emojis = append(emojis, &unicodeEmoji)
})

emojiCodeMap := make(map[string]string)
for _, emoji := range emojis {
emojiCodeMap[emoji.ShortName] = fmt.Sprintf("\"%s\"", strings.Replace(strings.ToLower(emoji.Code), "\\u", "\\U", -1))
}

return emojiCodeMap, nil
}

0 comments on commit d293807

Please sign in to comment.