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.7KB

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