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

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