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