-
Notifications
You must be signed in to change notification settings - Fork 1
/
emojify
executable file
·130 lines (121 loc) · 2.77 KB
/
emojify
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/bin/bash
set -o nounset
set -o errexit
shopt -s inherit_errexit
shopt -s lastpipe
function install-cldr {
local state_dir=$1
printf 'Installing cldr data...\n' >&2
cd "$state_dir"
if ! cldr_url=$(
curl -s https://api.github.com/repos/unicode-org/cldr-json/releases/latest |
jq -r '
.assets[] |
select(.name | test("modern.zip$")) |
.browser_download_url'
); then
printf 'ERROR: Unable to determine download url\n' >&2
return 1
fi
if [[ -z "$cldr_url" ]]; then
printf 'ERROR: Unable to determine download url\n' >&2
return 1
fi
if ! curl -Ls "$cldr_url" >cldr-json.zip; then
printf 'ERROR: Unable to download cldr zip\n' >&2
return 1
fi
unzip -q cldr-json.zip
rm cldr-json.zip
return 0
}
function short-code-emoji-daemon {
local short_code
local cldr_file
local tts
local emoji
declare -a query
declare -A emoji_cache
while IFS=$'\t' read -ra query; do
short_code="${query[0]}"
cldr_file="${query[1]}"
tts=${short_code//_/ }
if ! [[ -v "emoji_cache[$short_code]" ]]; then
if ! emoji=$(
jq -r --arg tts "${tts}" '
.annotations.annotations |
to_entries[] |
select(.value.tts[0] == $tts) |
.key' <"$cldr_file"
); then
printf 'ERROR: Unable to parse cldr json\n' >&2
printf '%d\t%s\n' 1 ""
fi
if [[ -z "$emoji" ]]; then
emoji_cache[$short_code]="?${short_code}?"
else
emoji_cache[$short_code]=$emoji
fi
fi
printf '%d\t%s\n' 0 "${emoji_cache[$short_code]}"
done
}
short-code-emoji() {
local IFS=$'\t'
local ret
local resp
printf '%s\n' "$*" >&"${SHORT_CODE_EMOJI[1]}"
read -r ret resp <&"${SHORT_CODE_EMOJI[0]}"
printf '%s\n' "$resp"
return "$ret"
}
function parse-text {
local cldr_file=$1
local emoji
local char
local parsing_code='false'
local code_accum=''
while IFS= read -r -N1 char; do
if [[ $char != ':' ]]; then
if [[ $parsing_code == 'false' ]]; then
printf '%s' "$char"
continue
else
if [[ $char =~ [a-z_-] ]]; then
code_accum+=$char
continue
else
printf ':%s%s' "$code_accum" "$char"
parsing_code='false'
continue
fi
fi
else
if [[ $parsing_code == 'true' ]]; then
if ! emoji=$(short-code-emoji "$code_accum" "$cldr_file"); then
printf 'ERROR: Unable to get emoji :%s:\n' "$code_accum" >&2
return 1
fi
printf '%s' "$emoji"
parsing_code='false'
continue
else
parsing_code='true'
code_accum=''
continue
fi
fi
done
return 0
}
function main {
local state_dir="${XDG_DATA_HOME:-$HOME}/.local/share/emojify"
local cldr_file="${state_dir}/cldr-annotations-modern/annotations/en/annotations.json"
if ! [[ -e "$cldr_file" ]]; then
mkdir -p "$state_dir"
install-cldr "$state_dir"
fi
coproc SHORT_CODE_EMOJI { short-code-emoji-daemon; }
parse-text "$cldr_file"
}
main "$@"