Created
April 29, 2025 22:29
-
-
Save tyzbit/ad02f146e1f473007d54cf1f9800db82 to your computer and use it in GitHub Desktop.
Convert Revolt TOML themes to JSON for importing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
set -euo pipefail | |
INPUT="${1:-theme.toml}" | |
# Bash 4+ for associative arrays | |
declare -A output defaults | |
# ————————————————————————————————————————————————————————————— | |
# 1. Defaults | |
# ————————————————————————————————————————————————————————————— | |
defaults=( | |
[tooltip]="#000000" | |
[status-focus]="#4799F0" | |
[font]="Open Sans" | |
[monospaceFont]="Fira Code" | |
) | |
# ————————————————————————————————————————————————————————————— | |
# 2. Parse TOML | |
# ————————————————————————————————————————————————————————————— | |
section="" | |
while IFS= read -r raw; do | |
# trim whitespace | |
line="${raw#"${raw%%[![:space:]]*}"}" | |
line="${line%"${line##*[![:space:]]}"}" | |
# skip blanks/comments | |
[[ -z "$line" || "${line:0:1}" == "#" ]] && continue | |
# section headers | |
if [[ "$line" =~ ^\[(.+)\]$ ]]; then | |
sec="${BASH_REMATCH[1]}" | |
if [[ "$sec" == "variables" ]]; then section="variables" | |
elif [[ "$sec" =~ ^variables\.([^]]+)$ ]]; then section="${BASH_REMATCH[1]}" | |
else section="" | |
fi | |
continue | |
fi | |
# only process when inside [variables] or its subtables | |
[[ -z "$section" ]] && continue | |
# key = value | |
IFS='=' read -r raw_key raw_val <<< "$line" | |
key="${raw_key// /}" # strip spaces | |
val="${raw_val#"${raw_val%%[![:space:]]*}"}" # trim leading | |
val="${val%"${val##*[![:space:]]}"}" # trim trailing | |
# strip quotes around strings | |
if [[ "$val" =~ ^\".*\"$ ]]; then | |
val="${val:1:-1}" | |
fi | |
# uppercase any hex codes | |
if [[ "$val" =~ ^\# ]]; then | |
val="$(echo "$val" | tr '[:lower:]' '[:upper:]')" | |
fi | |
# build JSON key | |
if [[ "$section" == "variables" ]]; then | |
jkey="$key" | |
else | |
jkey="$section-$key" | |
fi | |
output["$jkey"]="$val" | |
done < "$INPUT" | |
# ————————————————————————————————————————————————————————————— | |
# 3. Emit JSON in the same key-order | |
# ————————————————————————————————————————————————————————————— | |
order=( | |
accent background foreground block message-box mention | |
success warning tooltip error hover | |
scrollbar-thumb scrollbar-track | |
primary-background primary-header | |
secondary-background secondary-foreground secondary-header | |
tertiary-background tertiary-foreground | |
status-online status-away status-focus status-busy status-streaming status-invisible | |
light font monospaceFont | |
) | |
echo "{" | |
for idx in "${!order[@]}"; do | |
k="${order[idx]}" | |
# pick from parsed output or defaults | |
if [[ -n "${output[$k]:-}" ]]; then | |
v="${output[$k]}" | |
elif [[ -n "${defaults[$k]:-}" ]]; then | |
v="${defaults[$k]}" | |
else | |
# skip any key we didn't actually populate | |
continue | |
fi | |
# determine whether to quote | |
if [[ "$v" == "true" || "$v" == "false" ]]; then | |
printf ' "%s": %s' "$k" "$v" | |
else | |
# escape any embedded quotes (unlikely in your use-case) | |
esc="${v//\"/\\\"}" | |
printf ' "%s": "%s"' "$k" "$esc" | |
fi | |
# comma unless last printed entry | |
if (( idx < ${#order[@]}-1 )); then | |
echo "," | |
else | |
echo | |
fi | |
done | |
echo "}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Adjust the defaults before running