Handle CW prosigns correctly in renderer/morse.py
Prosigns get split into individual letters with an (incorrect) gap between them. E.g. 'SK' is rendered as '... -.-' but shoud be '...-.-'. This also preserves `[morse:correction]` by producing a series of nine dits. The patch proposed by HB9ARK fixes that. Thank you!
This commit is contained in:
+28
-3
@@ -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
|
||||
|
||||
|
||||
Reference in New Issue
Block a user