Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emoji data source #36

Merged
merged 1 commit into from
Apr 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions cmd/generateEmojiCodeMap/emoji_data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package main

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strconv"
"strings"
)

const emojiDataJsonURL = "https://github.com/iamcal/emoji-data/raw/master/emoji.json"

// EmojiData json parse struct
type EmojiData struct {
Unified string `json:"unified"`
ShortName string `json:"short_name"`
}

// UnifiedToChar renders a character from its hexadecimal codepoint
func UnifiedToChar(unified string) (string, error) {
codes := strings.Split(unified, "-")
var sb strings.Builder
for _, code := range codes {
s, err := strconv.ParseInt(code, 16, 32)
if err != nil {
return "", err
}
sb.WriteRune(rune(s))
}
return sb.String(), nil
}

func createEmojiDataCodeMap() (map[string]string, error) {
res, err := http.Get(emojiDataJsonURL)
if err != nil {
return nil, err
}
defer res.Body.Close()

emojiFile, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, err
}

var data []EmojiData
if err := json.Unmarshal(emojiFile, &data); err != nil {
return nil, err
}

emojiCodeMap := make(map[string]string)
for _, emoji := range data {
if len(emoji.ShortName) == 0 || len(emoji.Unified) == 0 {
continue
}
code, err := UnifiedToChar(emoji.Unified)
if err != nil {
return nil, err
}
emojiCodeMap[emoji.ShortName] = fmt.Sprintf("%+q", strings.ToLower(code))
}

return emojiCodeMap, nil
}
18 changes: 15 additions & 3 deletions cmd/generateEmojiCodeMap/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ var fileName string
func init() {
log.SetFlags(log.Llongfile)

flag.StringVar(&pkgName, "pkg", "main", "output package")
flag.StringVar(&fileName, "o", "emoji_codemap.go", "output file")
flag.StringVar(&pkgName, "pkg", "emoji", "output package")
flag.StringVar(&fileName, "o", "../../emoji_codemap.go", "output file")
flag.Parse()
}

Expand All @@ -42,11 +42,13 @@ var emojiCodeMap = map[string]string{
`

func createCodeMap() (map[string]string, error) {
log.Printf("creating gemoji code map")
gemojiCodeMap, err := createGemojiCodeMap()
if err != nil {
return nil, err
}

log.Printf("creating emojo code map")
emojoCodeMap, err := createEmojoCodeMap()
if err != nil {
return nil, err
Expand All @@ -55,6 +57,7 @@ func createCodeMap() (map[string]string, error) {
gemojiCodeMap[k] = v
}

log.Printf("creating unicode code map")
unicodeorgCodeMap, err := createUnicodeorgMap()
if err != nil {
return nil, err
Expand All @@ -63,6 +66,15 @@ func createCodeMap() (map[string]string, error) {
gemojiCodeMap[k] = v
}

log.Printf("creating emoji code map")
emojiDataCodeMap, err := createEmojiDataCodeMap()
if err != nil {
return nil, err
}
for k, v := range emojiDataCodeMap {
gemojiCodeMap[k] = v
}

return gemojiCodeMap, nil
}

Expand All @@ -79,7 +91,7 @@ func createCodeMapSource(pkgName string, emojiCodeMap map[string]string) ([]byte

bts, err := format.Source(buf.Bytes())
if err != nil {
fmt.Printf(string(buf.Bytes()))
fmt.Print(buf.String())
return nil, fmt.Errorf("gofmt: %s", err)
}

Expand Down
Loading