feature: add morse code

This commit is contained in:
Prof. Dr. Matthias Jung DL9MJ
2024-12-08 12:40:46 +01:00
parent c46e9163e9
commit 0f8af3defa
3 changed files with 142 additions and 1 deletions
+36 -1
View File
@@ -5,6 +5,7 @@ from mistletoe import HtmlRenderer
from renderer.comment import BlockComment, SpanComment
from renderer.dash import Dash
from renderer.halfwidth_spaces import HalfwidthSpaces
from renderer.morse import Morse
from renderer.nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
from renderer.quote import Quote
from renderer.tag import Tag
@@ -17,7 +18,18 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
margin_id = 0
def __init__(self):
super().__init__(Dash, BlockComment, SpanComment, Quote, Underline, Tag, HalfwidthSpaces, NonbreakingSpaces, NonbreakingSpacesDots)
super().__init__(
Dash,
BlockComment,
SpanComment,
Quote,
Underline,
Morse,
Tag,
HalfwidthSpaces,
NonbreakingSpaces,
NonbreakingSpacesDots,
)
def render_dash(self, token):
return " – "
@@ -38,6 +50,29 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
self.margin_anchor_id += 1
return f'<a id="margin_{self.margin_anchor_id}"></a>'
@staticmethod
def render_morse_helper(morse_code):
result = '<span class="morse">'
for char in morse_code:
result += '<span class="morse_char">\n'
for symbol in char:
result += '<span class="morse_char">\n'
if symbol == 1:
result += '▄'
elif symbol == 2:
result += '▄▄▄'
elif symbol == 3:
result += '&nbsp;'
result += '</span>\n'
result += '</span>\n'
result += '</span>'
return result
def render_morse(self, token):
morse_code = Morse.convert_to_morse_code(token.content)
return self.render_morse_helper(morse_code)
@staticmethod
def render_tag_helper(type, content, margin_id, margin_anchor_id):
""" This function is used to render the different types of tags. It is
+89
View File
@@ -0,0 +1,89 @@
import re
from mistletoe.span_token import SpanToken
class Morse(SpanToken):
"""
Morse code span token ("[morse:moe]").
Generates morse code from a text.
"""
@staticmethod
def convert_to_morse_code(text):
dit = 1
dah = 2
space = 3
morse_code = {
"a": [dit, dah],
"b": [dah, dit, dit, dit],
"c": [dah, dit, dah, dit],
"d": [dah, dit, dit],
"e": [dit],
"f": [dit, dit, dah, dit],
"g": [dah, dah, dit],
"h": [dit, dit, dit, dit],
"i": [dit, dit],
"j": [dit, dah, dah, dah],
"k": [dah, dit, dah],
"l": [dit, dah, dit, dit],
"m": [dah, dah],
"n": [dah, dit],
"o": [dah, dah, dah],
"p": [dit, dah, dah, dit],
"q": [dah, dah, dit, dah],
"r": [dit, dah, dit],
"s": [dit, dit, dit],
"t": [dah],
"u": [dit, dit, dah],
"v": [dit, dit, dit, dah],
"w": [dit, dah, dah],
"x": [dah, dit, dit, dah],
"y": [dah, dit, dah, dah],
"z": [dah, dah, dit, dit],
"ä": [dit, dah, dit, dah],
"ö": [dah, dah, dah, dit],
"ü": [dit, dit, dah, dah],
"ß": [dit, dit, dit, dah, dah, dit, dit],
"0": [dah, dah, dah, dah, dah],
"1": [dit, dah, dah, dah, dah],
"2": [dit, dit, dah, dah, dah],
"3": [dit, dit, dit, dah, dah],
"4": [dit, dit, dit, dit, dah],
"5": [dit, dit, dit, dit, dit],
"6": [dah, dit, dit, dit, dit],
"7": [dah, dah, dit, dit, dit],
"8": [dah, dah, dah, dit, dit],
"9": [dah, dah, dah, dah, dit],
".": [dit, dah, dit, dah, dit, dah],
",": [dah, dah, dit, dit, dah, dah],
":": [dah, dah, dah, dit, dit, dit],
";": [dah, dit, dah, dit, dah, dit],
"?": [dit, dit, dah, dah, dit, dit],
"!": [dah, dit, dah, dit, dah, dah],
"-": [dah, dit, dit, dit, dit, dah],
"_": [dit, dit, dah, dah, dit, dah],
"(": [dah, dit, dah, dah, dit],
")": [dah, dit, dah, dah, dit, dah],
"'": [dit, dah, dah, dah, dah, dit],
"=": [dah, dit, dit, dit, dah],
"+": [dit, dah, dit, dah, dit],
"/": [dah, dit, dit, dah, dit],
"@": [dit, dah, dah, dit, dah, dit],
"ar": [dit, dah, dit, dah, dit],
"bk": [dah, dit, dit, dit, dah, dit, dah],
"sk": [dit, dit, dit, dah, dit, dah],
"correction": [dit, dit, dit, dit, dit, dit, dit, dit],
" ": [space]
}
result = []
for char in text:
if char.lower() in morse_code:
result.append(morse_code[char.lower()])
return result
parse_inner = False
pattern = re.compile(r'"\[morse:\s*(.+)\]"')
+17
View File
@@ -0,0 +1,17 @@
import mistletoe
from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer
def test_morse_html() :
assertions = {
'"[morse:a]"' : "<p>"+FiftyOhmHtmlRenderer.render_morse_helper([[1,2]])+"</p>\n",
'"[morse: a]"' : "<p>"+FiftyOhmHtmlRenderer.render_morse_helper([[1,2]])+"</p>\n",
'"[morse: aaa]"' : "<p>"+FiftyOhmHtmlRenderer.render_morse_helper([[1,2],[1,2],[1,2]])+"</p>\n",
'"[morse: a a]"' : "<p>"+FiftyOhmHtmlRenderer.render_morse_helper([[1,2],[3],[1,2]])+"</p>\n",
'"[morse: db0]"' : "<p>"+FiftyOhmHtmlRenderer.render_morse_helper([[2,1,1],[2,1,1,1],[2,2,2,2,2]])+"</p>\n",
}
for assertion in assertions:
assert mistletoe.markdown(assertion, FiftyOhmHtmlRenderer) == assertions[assertion]