-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemojify-orig
executable file
·108 lines (102 loc) · 2.23 KB
/
emojify-orig
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
#!/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 {
local short_code=$1
local cldr_file=$2
local tts=${short_code//_/ }
local emoji
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
return 1
fi
if [[ -z "$emoji" ]]; then
printf '?%s?\n' "$short_code"
else
printf '%s\n' "$emoji"
fi
return 0
}
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
parse-text "$cldr_file"
}
main "$@"