|
| 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 | + |
| 6 | +// +build ignore |
| 7 | + |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "encoding/json" |
| 12 | + "flag" |
| 13 | + "fmt" |
| 14 | + "go/format" |
| 15 | + "io/ioutil" |
| 16 | + "log" |
| 17 | + "net/http" |
| 18 | + "regexp" |
| 19 | + "sort" |
| 20 | + "strconv" |
| 21 | + "strings" |
| 22 | +) |
| 23 | + |
| 24 | +const ( |
| 25 | + gemojiURL = "https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json" |
| 26 | + maxUnicodeVersion = 12 |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + flagOut = flag.String("o", "modules/emoji/emoji_data.go", "out") |
| 31 | +) |
| 32 | + |
| 33 | +// Gemoji is a set of emoji data. |
| 34 | +type Gemoji []Emoji |
| 35 | + |
| 36 | +// Emoji represents a single emoji and associated data. |
| 37 | +type Emoji struct { |
| 38 | + Emoji string `json:"emoji"` |
| 39 | + Description string `json:"description,omitempty"` |
| 40 | + Aliases []string `json:"aliases"` |
| 41 | + UnicodeVersion string `json:"unicode_version,omitempty"` |
| 42 | +} |
| 43 | + |
| 44 | +// Don't include some fields in JSON |
| 45 | +func (e Emoji) MarshalJSON() ([]byte, error) { |
| 46 | + type emoji Emoji |
| 47 | + x := emoji(e) |
| 48 | + x.UnicodeVersion = "" |
| 49 | + x.Description = "" |
| 50 | + return json.Marshal(x) |
| 51 | +} |
| 52 | + |
| 53 | +func main() { |
| 54 | + var err error |
| 55 | + |
| 56 | + flag.Parse() |
| 57 | + |
| 58 | + // generate data |
| 59 | + buf, err := generate() |
| 60 | + if err != nil { |
| 61 | + log.Fatal(err) |
| 62 | + } |
| 63 | + |
| 64 | + // write |
| 65 | + err = ioutil.WriteFile(*flagOut, buf, 0644) |
| 66 | + if err != nil { |
| 67 | + log.Fatal(err) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +var replacer = strings.NewReplacer( |
| 72 | + "main.Gemoji", "Gemoji", |
| 73 | + "main.Emoji", "\n", |
| 74 | + "}}", "},\n}", |
| 75 | + ", Description:", ", ", |
| 76 | + ", Aliases:", ", ", |
| 77 | + ", UnicodeVersion:", ", ", |
| 78 | +) |
| 79 | + |
| 80 | +var emojiRE = regexp.MustCompile(`\{Emoji:"([^"]*)"`) |
| 81 | + |
| 82 | +func generate() ([]byte, error) { |
| 83 | + var err error |
| 84 | + |
| 85 | + // load gemoji data |
| 86 | + res, err := http.Get(gemojiURL) |
| 87 | + if err != nil { |
| 88 | + return nil, err |
| 89 | + } |
| 90 | + defer res.Body.Close() |
| 91 | + |
| 92 | + // read all |
| 93 | + body, err := ioutil.ReadAll(res.Body) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + |
| 98 | + // unmarshal |
| 99 | + var data Gemoji |
| 100 | + err = json.Unmarshal(body, &data) |
| 101 | + if err != nil { |
| 102 | + return nil, err |
| 103 | + } |
| 104 | + |
| 105 | + var re = regexp.MustCompile(`keycap|registered|copyright`) |
| 106 | + tmp := data[:0] |
| 107 | + |
| 108 | + // filter out emoji that require greater than max unicode version |
| 109 | + for i := range data { |
| 110 | + val, _ := strconv.ParseFloat(data[i].UnicodeVersion, 64) |
| 111 | + if int(val) <= maxUnicodeVersion { |
| 112 | + // remove these keycaps for now they really complicate matching since |
| 113 | + // they include normal letters in them |
| 114 | + if re.MatchString(data[i].Description) { |
| 115 | + continue |
| 116 | + } |
| 117 | + tmp = append(tmp, data[i]) |
| 118 | + } |
| 119 | + } |
| 120 | + data = tmp |
| 121 | + |
| 122 | + sort.Slice(data, func(i, j int) bool { |
| 123 | + return data[i].Aliases[0] < data[j].Aliases[0] |
| 124 | + }) |
| 125 | + |
| 126 | + aliasPairs := make([]string, 0) |
| 127 | + aliasMap := make(map[string]int, len(data)) |
| 128 | + |
| 129 | + for i, e := range data { |
| 130 | + if e.Emoji == "" || len(e.Aliases) == 0 { |
| 131 | + continue |
| 132 | + } |
| 133 | + for _, a := range e.Aliases { |
| 134 | + if a == "" { |
| 135 | + continue |
| 136 | + } |
| 137 | + aliasMap[a] = i |
| 138 | + aliasPairs = append(aliasPairs, ":"+a+":", e.Emoji) |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + // gitea customizations |
| 143 | + i, ok := aliasMap["tada"] |
| 144 | + if ok { |
| 145 | + data[i].Aliases = append(data[i].Aliases, "hooray") |
| 146 | + } |
| 147 | + i, ok = aliasMap["laughing"] |
| 148 | + if ok { |
| 149 | + data[i].Aliases = append(data[i].Aliases, "laugh") |
| 150 | + } |
| 151 | + |
| 152 | + // add header |
| 153 | + str := replacer.Replace(fmt.Sprintf(hdr, gemojiURL, data)) |
| 154 | + |
| 155 | + // change the format of the unicode string |
| 156 | + str = emojiRE.ReplaceAllStringFunc(str, func(s string) string { |
| 157 | + var err error |
| 158 | + s, err = strconv.Unquote(s[len("{Emoji:"):]) |
| 159 | + if err != nil { |
| 160 | + panic(err) |
| 161 | + } |
| 162 | + return "{" + strconv.QuoteToASCII(s) |
| 163 | + }) |
| 164 | + |
| 165 | + // write a JSON file to use with tribute |
| 166 | + file, _ := json.Marshal(data) |
| 167 | + _ = ioutil.WriteFile("assets/emoji.json", file, 0644) |
| 168 | + |
| 169 | + // format |
| 170 | + return format.Source([]byte(str)) |
| 171 | +} |
| 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 | +
|
| 178 | +package emoji |
| 179 | +
|
| 180 | +// Code generated by gen.go. DO NOT EDIT. |
| 181 | +// Sourced from %s |
| 182 | +// |
| 183 | +var GemojiData = %#v |
| 184 | +` |
0 commit comments