diff --git a/renderer/morse.py b/renderer/morse.py index e47ba86..050b0c5 100644 --- a/renderer/morse.py +++ b/renderer/morse.py @@ -78,11 +78,36 @@ class Morse(SpanToken): " ": [space], } + # Prosigns (e.g. "ar", "bk", "sk", "correction") are stored as multi- + # character keys whose dit/dah list already has no gap between the + # individual letters - that's what makes them a prosign rather than + # two separate letters. They must therefore be matched as a whole + # "word", not character by character, otherwise they're never found + # and get split into individual letters with an (incorrect) gap + # between them. + prosigns = {key: value for key, value in morse_code.items() if len(key) > 1} + result = [] - for char in text: - if char.lower() in morse_code: - result.append(morse_code[char.lower()]) + # Split into words and whitespace, keeping the whitespace as its own + # token so word gaps are preserved exactly as before. + for token in re.split(r"(\s)", text): + if not token: + continue + + if token.isspace(): + for char in token: + if char in morse_code: + result.append(morse_code[char]) + continue + + if token.lower() in prosigns: + result.append(prosigns[token.lower()]) + continue + + for char in token: + if char.lower() in morse_code: + result.append(morse_code[char.lower()]) return result