-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxcompose_generator.sh
More file actions
executable file
·114 lines (94 loc) · 3.5 KB
/
xcompose_generator.sh
File metadata and controls
executable file
·114 lines (94 loc) · 3.5 KB
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
#!/usr/bin/env bash
# CGS Custom Keyboards | .XCompose File Generator
# [AUTHOR]: Carlos González Sanz
# Script constants
KSC_XCOMPOSE="$HOME/.XCompose"
KSC_COMPOSE_DIR="$HOME/smk_compose"
KSC_COMPOSE_LOCAL_INCLUDE='include "%H/smk_compose/'
# Rules directories
KSC_COMPOSE_SEQS='sequences/compose_seqs'
KSC_DEAD_KEYS='sequences/dead_keys'
# .XCompose Headers
KSC_COMPOSE_HEADER=$(cat <<EOF
# Skorpian Master Keyboard | .XCompose Rules
# [AUTHOR]: Carlos González Sanz
#
# [NOTE]: This auto-generated file stores all dead keys and Compose rules
# designed for SMK Keyboards (SMK 16.0 onwards).
#
# Feel free to (un)comment (out) any "include" statement you want.
#
# Skorpian Master Keyboard (SMK):
# - [CODENAME]: Penguin Land
# - [VERSION]: 16.0
#
# Mandatory locale rules
include "%L"
# Additional writing system rules (SMK 16.0+)
EOF
)
KSC_COMPOSE_FOOTER=$(cat <<EOF
##################
# [END OF RULES] #
##################
EOF
)
# Script functions
compose_rule_compression() {
RULES_DIR=$1
RULES_SUBDIR=$2
COMPOSE_PREFIX=$3
# [RULE ONE-WAY COMPRESSION ALGORITHM]
#
# This "compression" is done in 3 steps as follows:
# - 1: Remove all comments from the current directory's rule modules.
# - 2: Remove every blank line from the aforementioned modules.
# - 3: Leave only a single space before and after each rule definition (" : ").
#
# After that, we just simply sort and discard any duplicate rules
# before writing them to our current local .XCompose file.
#
cat "$RULES_SUBDIR"/* | sed 's/^#[[:space:]]*.*$//' | sed '/^$/d' | sed 's/[[:space:]]*:[[:space:]]*/ : /' | sort | uniq > "$KSC_COMPOSE_DIR/$COMPOSE_PREFIX$(basename "$RULES_DIR")-$(basename "$RULES_SUBDIR")"
echo -e "Added new rules submodule on $KSC_COMPOSE_DIR/$COMPOSE_PREFIX$(basename "$1")-$(basename "$2")"
}
# Create XCompose folders
mkdir -p "$KSC_COMPOSE_DIR"
# Traverse all dead key rules first
for CUR_DIR in "$KSC_DEAD_KEYS"/*; do
# Check if we have our rules inside writing system subdirectories
RULE_FILE_COUNT=$(find "$CUR_DIR" -maxdepth 1 -type f -printf '.' | wc -c)
echo -e "Searching for rules on $CUR_DIR..."
if [[ $RULE_FILE_COUNT -eq 0 ]]; then
for SUBDIR in "$CUR_DIR"/*; do
if [[ -d "$SUBDIR" ]]; then
echo -e "Searching for rules on $SUBDIR..."
compose_rule_compression "$CUR_DIR" "$SUBDIR" ""
fi
done
else
compose_rule_compression "$CUR_DIR" "$CUR_DIR" ""
fi
done
# Compile all XCompose rules starting with "<Multi_key>"
for CUR_DIR in "$KSC_COMPOSE_SEQS"/*; do
# Check if we have our rules inside writing system subdirectories
RULE_FILE_COUNT=$(find "$CUR_DIR" -maxdepth 1 -type f -printf '.' | wc -c)
echo -e "Searching for <Multi_key> rules on $CUR_DIR..."
if [[ $RULE_FILE_COUNT -eq 0 ]]; then
for SUBDIR in "$CUR_DIR"/*; do
if [[ -d "$SUBDIR" ]]; then
echo -e "Searching for <Multi_key> rules on $SUBDIR..."
compose_rule_compression "$CUR_DIR" "$SUBDIR" "compose-"
fi
done
else
compose_rule_compression "$CUR_DIR" "$CUR_DIR" "compose-"
fi
done
# Write out the final .XCompose file
echo -e "$KSC_COMPOSE_HEADER" > "$KSC_XCOMPOSE"
for RULE_FILE in "$KSC_COMPOSE_DIR"/*; do
echo -e "$KSC_COMPOSE_LOCAL_INCLUDE$(basename "$RULE_FILE")\"" >> "$KSC_XCOMPOSE"
done
echo -e "$KSC_COMPOSE_FOOTER" >> "$KSC_XCOMPOSE"
echo -e "> [INFO]: Autogenerated the local .XCompose file for SMK v16.0"