You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

generate-emoji.go 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // Copyright 2015 Kenneth Shaw
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. // +build ignore
  6. package main
  7. import (
  8. "flag"
  9. "fmt"
  10. "go/format"
  11. "io/ioutil"
  12. "log"
  13. "net/http"
  14. "regexp"
  15. "sort"
  16. "strconv"
  17. "strings"
  18. "unicode/utf8"
  19. jsoniter "github.com/json-iterator/go"
  20. )
  21. const (
  22. gemojiURL = "https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json"
  23. maxUnicodeVersion = 12
  24. )
  25. var (
  26. flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out")
  27. )
  28. // Gemoji is a set of emoji data.
  29. type Gemoji []Emoji
  30. // Emoji represents a single emoji and associated data.
  31. type Emoji struct {
  32. Emoji string `json:"emoji"`
  33. Description string `json:"description,omitempty"`
  34. Aliases []string `json:"aliases"`
  35. UnicodeVersion string `json:"unicode_version,omitempty"`
  36. SkinTones bool `json:"skin_tones,omitempty"`
  37. }
  38. // Don't include some fields in JSON
  39. func (e Emoji) MarshalJSON() ([]byte, error) {
  40. type emoji Emoji
  41. x := emoji(e)
  42. x.UnicodeVersion = ""
  43. x.Description = ""
  44. x.SkinTones = false
  45. json := jsoniter.ConfigCompatibleWithStandardLibrary
  46. return json.Marshal(x)
  47. }
  48. func main() {
  49. var err error
  50. flag.Parse()
  51. // generate data
  52. buf, err := generate()
  53. if err != nil {
  54. log.Fatal(err)
  55. }
  56. // write
  57. err = ioutil.WriteFile(*flagOut, buf, 0644)
  58. if err != nil {
  59. log.Fatal(err)
  60. }
  61. }
  62. var replacer = strings.NewReplacer(
  63. "main.Gemoji", "Gemoji",
  64. "main.Emoji", "\n",
  65. "}}", "},\n}",
  66. ", Description:", ", ",
  67. ", Aliases:", ", ",
  68. ", UnicodeVersion:", ", ",
  69. ", SkinTones:", ", ",
  70. )
  71. var emojiRE = regexp.MustCompile(`\{Emoji:"([^"]*)"`)
  72. func generate() ([]byte, error) {
  73. var err error
  74. // load gemoji data
  75. res, err := http.Get(gemojiURL)
  76. if err != nil {
  77. return nil, err
  78. }
  79. defer res.Body.Close()
  80. // read all
  81. body, err := ioutil.ReadAll(res.Body)
  82. if err != nil {
  83. return nil, err
  84. }
  85. // unmarshal
  86. var data Gemoji
  87. json := jsoniter.ConfigCompatibleWithStandardLibrary
  88. err = json.Unmarshal(body, &data)
  89. if err != nil {
  90. return nil, err
  91. }
  92. var skinTones = make(map[string]string)
  93. skinTones["\U0001f3fb"] = "Light Skin Tone"
  94. skinTones["\U0001f3fc"] = "Medium-Light Skin Tone"
  95. skinTones["\U0001f3fd"] = "Medium Skin Tone"
  96. skinTones["\U0001f3fe"] = "Medium-Dark Skin Tone"
  97. skinTones["\U0001f3ff"] = "Dark Skin Tone"
  98. var tmp Gemoji
  99. //filter out emoji that require greater than max unicode version
  100. for i := range data {
  101. val, _ := strconv.ParseFloat(data[i].UnicodeVersion, 64)
  102. if int(val) <= maxUnicodeVersion {
  103. tmp = append(tmp, data[i])
  104. }
  105. }
  106. data = tmp
  107. sort.Slice(data, func(i, j int) bool {
  108. return data[i].Aliases[0] < data[j].Aliases[0]
  109. })
  110. aliasMap := make(map[string]int, len(data))
  111. for i, e := range data {
  112. if e.Emoji == "" || len(e.Aliases) == 0 {
  113. continue
  114. }
  115. for _, a := range e.Aliases {
  116. if a == "" {
  117. continue
  118. }
  119. aliasMap[a] = i
  120. }
  121. }
  122. // gitea customizations
  123. i, ok := aliasMap["tada"]
  124. if ok {
  125. data[i].Aliases = append(data[i].Aliases, "hooray")
  126. }
  127. i, ok = aliasMap["laughing"]
  128. if ok {
  129. data[i].Aliases = append(data[i].Aliases, "laugh")
  130. }
  131. // write a JSON file to use with tribute (write before adding skin tones since we can't support them there yet)
  132. file, _ := json.Marshal(data)
  133. _ = ioutil.WriteFile("assets/emoji.json", file, 0644)
  134. // Add skin tones to emoji that support it
  135. var (
  136. s []string
  137. newEmoji string
  138. newDescription string
  139. newData Emoji
  140. )
  141. for i := range data {
  142. if data[i].SkinTones {
  143. for k, v := range skinTones {
  144. s = strings.Split(data[i].Emoji, "")
  145. if utf8.RuneCountInString(data[i].Emoji) == 1 {
  146. s = append(s, k)
  147. } else {
  148. // insert into slice after first element because all emoji that support skin tones
  149. // have that modifier placed at this spot
  150. s = append(s, "")
  151. copy(s[2:], s[1:])
  152. s[1] = k
  153. }
  154. newEmoji = strings.Join(s, "")
  155. newDescription = data[i].Description + ": " + v
  156. newAlias := data[i].Aliases[0] + "_" + strings.ReplaceAll(v, " ", "_")
  157. newData = Emoji{newEmoji, newDescription, []string{newAlias}, "12.0", false}
  158. data = append(data, newData)
  159. }
  160. }
  161. }
  162. // add header
  163. str := replacer.Replace(fmt.Sprintf(hdr, gemojiURL, data))
  164. // change the format of the unicode string
  165. str = emojiRE.ReplaceAllStringFunc(str, func(s string) string {
  166. var err error
  167. s, err = strconv.Unquote(s[len("{Emoji:"):])
  168. if err != nil {
  169. panic(err)
  170. }
  171. return "{" + strconv.QuoteToASCII(s)
  172. })
  173. // format
  174. return format.Source([]byte(str))
  175. }
  176. const hdr = `
  177. // Copyright 2020 The Gitea Authors. All rights reserved.
  178. // Use of this source code is governed by a MIT-style
  179. // license that can be found in the LICENSE file.
  180. package emoji
  181. // Code generated by gen.go. DO NOT EDIT.
  182. // Sourced from %s
  183. //
  184. var GemojiData = %#v
  185. `