Skip to content

Commit

Permalink
Fix codepoint updater
Browse files Browse the repository at this point in the history
- Fixed errors in the generated file header.
- Fixed generated file not having a trailing new line.
- Changed file header to be generated by YAML dump instead of manually
  prepended.
- Changed Prettier config to allow single quotes.
  • Loading branch information
joelspadin committed Feb 4, 2023
1 parent d67683f commit e5ddaa2
Show file tree
Hide file tree
Showing 3 changed files with 2,516 additions and 2,519 deletions.
3 changes: 2 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{
"endOfLine": "auto"
"endOfLine": "auto",
"singleQuote": true
}
23 changes: 9 additions & 14 deletions scripts/update_codepoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@
)

yaml = YAML()
yaml.version = (1, 2)

UNICODE_BLOCKS_URL = "https://www.unicode.org/Public/UCD/latest/ucd/Blocks.txt"
UNICODE_BLOCK_RE = re.compile(r"^([0-9A-F]+)..([0-9A-F]+); (.+)")

YAML_HEADER = """\
%YAML 1.2
# This document maps Unicode codepoints to key names for ZMK.
# Do not manually add new codepoitns to this file. Instead, add locales to
# scripts/locales.yaml and run scripts/update_codepoints.py. Then you can
# Do not manually add new codepoints to this file. Instead, add entries to
# keyboards/keyboards.yaml and run scripts/update_codepoints.py. Then you can
# edit this file to assign names to the codepoints it adds.
#
# Each value is either a single string or a list of strings which must be valid
Expand All @@ -39,7 +39,6 @@
#
# If a name matches a name from ZMK's keys.h, any aliases for that key will
# automatically be added, e.g. for a German layout, ESCAPE -> DE_ESCAPE, DE_ESC.
---
"""


Expand Down Expand Up @@ -175,13 +174,7 @@ def get_char_comments(item: CommentedMap, c: str):
item: CommentedMap
block = codepoint_to_block(first_key(item), blocks)

# ruamel.yaml parses the first block's comment as being a start comment
# for the whole sequence, so don't set it on the list item too or it
# will duplicate the comment.
if block.start == "\0":
codepoints.yaml_set_start_comment(block.name)
else:
codepoints.yaml_set_comment_before_after_key(i, before="\n" + block.name)
codepoints.yaml_set_comment_before_after_key(i, before="\n" + block.name)

for c in item.keys():
if comment := " ".join(get_char_comments(item, c)):
Expand Down Expand Up @@ -217,8 +210,8 @@ def main():
"out",
type=Path,
nargs="?",
default=Path("./codepoints.yaml"),
help="Output file name (default: codepoints.yaml)",
default=REPO_PATH / "zmk_locale_generator/codepoints.yaml",
help="Output file name (default: zmk_locale_generator/codepoints.yaml)",
)

args = parser.parse_args()
Expand All @@ -231,9 +224,11 @@ def main():
add_new_codepoint_placeholders(codepoints, blocks, used)
add_codepoint_comments(codepoints, blocks)

codepoints.yaml_set_start_comment(YAML_HEADER)

with args.out.open(mode="w", encoding="utf-8") as f:
f.write(YAML_HEADER)
yaml.dump(codepoints, f, transform=transform)
f.write("\n")


if __name__ == "__main__":
Expand Down
Loading

0 comments on commit e5ddaa2

Please sign in to comment.