Last active
September 17, 2023 04:25
-
-
Save delannoy/c435d635b16f5e3e22b65e1fefa5f993 to your computer and use it in GitHub Desktop.
Writes AutoHotkey hotkey-based script from JuliaLang tab completions for Unicode characters
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 python3 | |
| # Writes AutoHotkey hotkey-based script from JuliaLang tab completions for Unicode characters | |
| # Inspired by: | |
| # [I created a (Linux) script to easily type Unicode math everywhere](https://www.reddit.com/r/Physics/comments/pc08mg/i_created_a_linux_script_to_easily_type_unicode/) | |
| from lxml.html import fromstring | |
| from numpy import array_split | |
| from requests import get | |
| from typing import TextIO | |
| def parseUnicodeInput(): | |
| '''Parse html table of JuliaLang tab completions for Unicode characters''' | |
| eTree = fromstring(get('https://docs.julialang.org/en/v1/manual/unicode-input').content) | |
| unicodeTable = [e.text for e in eTree.xpath('//tr/td')] | |
| return array_split(unicodeTable, len(unicodeTable)/4) # [Split a list into evenly sized chunks](https://stackoverflow.com/a/16935535/13019084) | |
| def writeHotstring(ahk:TextIO, code:str, character:str, completion:str, name:str): | |
| '''Write hotstring for a give table entry | |
| [Hotstrings](https://lexikos.github.io/v2/docs/Hotstrings.htm#Options) | |
| ?: Hotstring will be triggered even when it is inside another word | |
| c: Case sensitive hotstring | |
| o: Omit the ending character of auto-replace hotstrings when the replacement is produced | |
| note that colon characters (:) in the {completion} string must be escaped by a backtick (`) | |
| ''' | |
| ahk.write(f':?co:{completion.replace(":","`:")}::{{{code}}} ; {character} ({name})\n') | |
| def main(): | |
| unicodeTable = parseUnicodeInput() | |
| with open('juliaUnicode.ahk', 'w') as ahk: | |
| ahk.write('#SingleInstance\n') # [Replace old instance automatically](https://lexikos.github.io/v2/docs/commands/_SingleInstance.htm) | |
| with open('juliaUnicode.ahk', 'a') as ahk: | |
| ahk.write('Hotstring("EndChars", "`t")\n\n') # [Trigger hotstring with {TAB} only](https://lexikos.github.io/v2/docs/Hotstrings.htm#EndChars) | |
| for row in unicodeTable: | |
| if ' + ' in row[0]: # merge multiple code points | |
| writeHotstring(ahk, row[0].replace(' + ', '}{'), row[1], row[2], row[3]) | |
| if ', ' in row[2]: # split multiple tab completion sequences into separate lines | |
| [writeHotstring(ahk, row[0], row[1], completion.strip(), row[3]) for completion in row[2].split(',')] | |
| else: | |
| writeHotstring(ahk, row[0], row[1], row[2], row[3]) | |
| if __name__ == '__main__': | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment