aboutsummaryrefslogtreecommitdiffstats
path: root/generate
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2025-01-30 07:42:00 -0800
committerRose Hogenson <rosehogenson@posteo.net>2025-01-30 07:42:00 -0800
commit83e5ac714eca9613af7d866724d09d2bb12fb025 (patch)
tree71b14e98cc2d715d0f9e27c84eb3f441a11aff0f /generate
parentAdd a script to generate the list. (diff)
downloademoji-83e5ac714eca9613af7d866724d09d2bb12fb025.tar.zst
Clean up and simplify the code.
Diffstat (limited to 'generate')
-rw-r--r--generate/generate.go46
1 files changed, 25 insertions, 21 deletions
diff --git a/generate/generate.go b/generate/generate.go
index cfbf2dd..fe1caa3 100644
--- a/generate/generate.go
+++ b/generate/generate.go
@@ -132,12 +132,7 @@ func emojis() ([]string, error) {
return append(sequences, zwjSequences...), nil
}
-type emojiAnnotation struct {
- name string
- annotations []string
-}
-
-func annotationsInFile(f io.Reader) (map[string]*emojiAnnotation, error) {
+func annotationsInFile(f io.Reader) (map[string]string, error) {
contents, err := io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("read CLDR data: %s", err)
@@ -153,23 +148,37 @@ func annotationsInFile(f io.Reader) (map[string]*emojiAnnotation, error) {
return nil, fmt.Errorf("parse CLDR data: %s", err)
}
- emojiAnnotations := make(map[string]*emojiAnnotation)
+ type annotationData struct {
+ name string
+ annotations []string
+ }
+ emojiAnnotations := make(map[string]annotationData)
for _, annotation := range annotations.Annotations {
- annotationData, ok := emojiAnnotations[annotation.CP]
- if !ok {
- annotationData = new(emojiAnnotation)
- emojiAnnotations[annotation.CP] = annotationData
- }
+ annotationData := emojiAnnotations[annotation.CP]
if annotation.Type == "tts" {
annotationData.name = annotation.Annotation
} else {
- annotationData.annotations = strings.FieldsFunc(annotation.Annotation, func(r rune) bool { return r == ' ' || r == '|' })
+ annotations := strings.Split(annotation.Annotation, "|")
+ for i, a := range annotations {
+ annotations[i] = strings.TrimSpace(a)
+ }
+ annotationData.annotations = annotations
}
+ emojiAnnotations[annotation.CP] = annotationData
}
- return emojiAnnotations, nil
+ annotationsCombined := make(map[string]string, len(emojiAnnotations))
+ for emoji, annotation := range emojiAnnotations {
+ annotation.annotations = slices.DeleteFunc(annotation.annotations, func(s string) bool { return strings.Contains(annotation.name, s) })
+ annotationsStr := annotation.name
+ if len(annotation.annotations) > 0 {
+ annotationsStr += " " + strings.Join(annotation.annotations, " ")
+ }
+ annotationsCombined[emoji] = annotationsStr
+ }
+ return annotationsCombined, nil
}
-func annotations(cldrData *zip.Reader) (map[string]*emojiAnnotation, error) {
+func annotations(cldrData *zip.Reader) (map[string]string, error) {
annotationsFile, err := cldrData.Open("common/annotations/en.xml")
if err != nil {
return nil, fmt.Errorf("read CLDR data: %s", err)
@@ -303,12 +312,7 @@ func generate() error {
if !ok {
return fmt.Errorf("emoji %q has no annotation", emoji)
}
- annotation.annotations = slices.DeleteFunc(annotation.annotations, func(s string) bool { return strings.Contains(annotation.name, s) })
- fmt.Print(emoji, " ", annotation.name)
- if annotations := strings.Join(annotation.annotations, " "); annotations != "" {
- fmt.Print(" ", annotations)
- }
- fmt.Println()
+ fmt.Println(emoji, annotation)
}
return nil
}