Skip to content

Instantly share code, notes, and snippets.

@nickcoutsos
Created November 6, 2023 23:02
Show Gist options
  • Save nickcoutsos/1f5ff63f7bec9572196a41dc0cae16f4 to your computer and use it in GitHub Desktop.
Save nickcoutsos/1f5ff63f7bec9572196a41dc0cae16f4 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import json
from pathlib import Path
import sys
REPO_PATH = Path(__file__).parent.parent
sys.path.insert(0, str(REPO_PATH))
from keyboards import get_keyboards, Keyboard
from zmk_locale_generator import cldr
from zmk_locale_generator import LayoutHeaderGenerator
from zmk_locale_generator.codepoints import get_codepoint_names, is_visible_character
from zmk_locale_generator.keys import HidUsage, KeyAlias, Modifier, parse_zmk_keys, get_zmk_name
from zmk_locale_generator.util import unique
def main():
generator = LayoutHeaderGenerator(None)
for keyboard in get_keyboards():
out_path = Path("exported") / keyboard.filename.replace(".h", ".json")
with keyboard.path.open("r", encoding="utf-8") as infile:
with out_path.open("w", encoding="utf-8") as outfile:
print(keyboard.filename)
layout = cldr.parse_cldr_keyboard(infile)
defs = generator._get_key_definitions(layout)
keys = []
for usage, value in defs:
if names := generator._get_key_names(keyboard.prefix, value):
keys.append(dict(
names=names,
symbol=value if is_visible_character(value) else None,
modifiers=[
mod.value for mod in list(usage.modifiers)
],
))
outfile.write(json.dumps(dict(
locale=keyboard.prefix,
keys=keys
)))
with Path("exported/catalog.json").open("w", encoding="utf-8") as f:
f.write(json.dumps({
keyboard.filename: dict(
filename=keyboard.filename.replace(".h", ".json"),
prefix=keyboard.prefix
)
for keyboard
in list(get_keyboards())
}))
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment