-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.sh
executable file
·137 lines (129 loc) · 3.35 KB
/
generate.sh
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
131
132
133
134
135
136
137
#!/bin/sh
# Developer Dvorak layout.
#
# Author: Pascal Morin <pm98zz.c@gmail.com>
# Source: github
GIT_DIR=$(git rev-parse --show-toplevel)
cd "$GIT_DIR" || exit 1
GIT_DIR=$(pwd -P)
OWN_DIR="$GIT_DIR/mac"
# Markers
DELIMITER=' <keyMapSet id="DeveloperDvorak">'
SRC_FILE="$GIT_DIR/matrix.csv"
# Main layout
TPL_FILE="$OWN_DIR/dvorak-dev.tpl.keylayout"
DEST_FILE="$OWN_DIR/DeveloperDvorak.bundle/Contents/Resources/DeveloperDvorak.keylayout"
TMP_FILE="/tmp/dvorak-dev.tmp"
# Numeric layout
NUM_TPL_FILE="$OWN_DIR/dvorak-num-dev.tpl.keylayout"
NUM_DEST_FILE="$OWN_DIR/dvorak-num-dev.keylayout"
NUM_TMP_FILE="/tmp/dvorak-num-dev.tmp"
# Remove tmp and artifacts.
cleanup(){
for FILE in $DEST_FILE $NUM_DEST_FILE $TMP_FILE $NUM_TMP_FILE; do
if [ -f "$FILE" ]; then
rm "$FILE"
fi
done
}
# Convert char to output.
# @param $1 string char
# @param $2 int keycode
# @param $3 string filepath
convert_to_code(){
STRING=""
case $1 in
'NoSymbol')
return
;;
[[:alpha:]])
echo " <key code=\"$2\" action=\"$1\"/>" >> "$3"
;;
[[:digit:]])
echo " <key code=\"$2\" output=\"$1\"/>" >> "$3"
;;
#@todo Something is wrong with the hyphen
'hyphen')
STRING="‐"
echo " <key code=\"$2\" output=\"$STRING\"/>" >> "$3"
;;
'dead'*)
echo " <key code=\"$2\" action=\"$1\"/>" >> "$3"
;;
'comma')
echo " <key code=\"$2\" output=\",\"/>" >> "$3"
;;
'Space')
echo " <key code=\"$2\" output=\" \"/>" >> "$3"
;;
'NoBreakSpace')
STRING=" "
echo " <key code=\"$2\" output=\"$STRING\"/>" >> "$3"
;;
'NarrowNoBreakSpace')
STRING=" "
echo " <key code=\"$2\" output=\"$STRING\"/>" >> "$3"
;;
'BackSpace')
STRING=""
echo " <key code=\"$2\" output=\"$STRING\"/>" >> "$3"
;;
*)
STRING="$(printf "%s" "$1" | iconv -f utf8 -t utf32be | xxd -p | sed -r 's/^0+/0x/' | xargs printf '&#x%04X;\n')"
echo " <key code=\"$2\" output=\"$STRING\"/>" >> "$3"
;;
esac
# ISO/JIS
if [ "$2" = "10" ]; then
convert_to_code "$1" "50" "$3"
convert_to_code "$1" "93" "$3"
fi
}
# Generate the keys definition from CSV.
# @param $1 int level to start with.
# @param $2 string filepath
generate_keys(){
COUNT=1
LEVEL=0
while [ $COUNT -lt 7 ]; do
if [ "$COUNT" -gt "$1" ]; then
echo "<keyMap index=\"$LEVEL\" baseMapSet=\"static\" baseIndex=\"$LEVEL\">" >> "$2"
#echo "<keyMap index=\"$LEVEL\">" >> "$2"
while read -r LINE; do
KEY=$(echo "$LINE" | cut -d ',' -f10 )
CHAR=$(echo "$LINE" | cut -d ',' -f$COUNT )
convert_to_code "$CHAR" "$KEY" "$2"
done < "$SRC_FILE"
echo "</keyMap>" >> "$2"
fi
COUNT=$((COUNT + 1))
LEVEL=$((LEVEL + 1))
done
}
# Generate the layout definitions.
# @param $1 src template
# @param $2 dest file
# @param $3 key definition file
generate_layouts(){
IFS=''
while read -r LINE; do
case $LINE in
"$DELIMITER")
echo "$LINE" >> "$2"
cat "$3" >> "$2"
;;
*)
echo "$LINE" >> "$2"
;;
esac
done < "$1"
}
# Remove target files
cleanup
# Generate key definitions
generate_keys 0 "$TMP_FILE"
generate_keys 4 "$NUM_TMP_FILE"
# Generate main layout
generate_layouts "$TPL_FILE" "$DEST_FILE" "$TMP_FILE"
# Generate numeric layout
generate_layouts "$NUM_TPL_FILE" "$NUM_DEST_FILE" "$NUM_TMP_FILE"