Pull request #16: feature: add morse code
Merge in FRAG/50ohm.de from feature/morse-new to main * commit '68048ff4d6315de44fe6a63692fff03e41adbb9e': feature: add morse code
This commit is contained in:
committed by
Tim Grelka DA2TG
commit
3f881b8c77
@@ -5,6 +5,7 @@ from mistletoe import HtmlRenderer
|
|||||||
from renderer.comment import BlockComment, SpanComment
|
from renderer.comment import BlockComment, SpanComment
|
||||||
from renderer.dash import Dash
|
from renderer.dash import Dash
|
||||||
from renderer.halfwidth_spaces import HalfwidthSpaces
|
from renderer.halfwidth_spaces import HalfwidthSpaces
|
||||||
|
from renderer.morse import Morse
|
||||||
from renderer.nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
from renderer.nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots
|
||||||
from renderer.quote import Quote
|
from renderer.quote import Quote
|
||||||
from renderer.references import References
|
from renderer.references import References
|
||||||
@@ -27,13 +28,11 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
Quote,
|
Quote,
|
||||||
Unit,
|
Unit,
|
||||||
Underline,
|
Underline,
|
||||||
|
Morse,
|
||||||
Tag,
|
Tag,
|
||||||
HalfwidthSpaces,
|
HalfwidthSpaces,
|
||||||
NonbreakingSpaces,
|
NonbreakingSpaces,
|
||||||
NonbreakingSpacesDots,
|
NonbreakingSpacesDots,
|
||||||
HalfwidthSpaces,
|
|
||||||
NonbreakingSpaces,
|
|
||||||
NonbreakingSpacesDots,
|
|
||||||
References,
|
References,
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -81,6 +80,29 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
|
|||||||
self.margin_anchor_id += 1
|
self.margin_anchor_id += 1
|
||||||
return f'<a id="margin_{self.margin_anchor_id}"></a>'
|
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 += " "
|
||||||
|
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
|
@staticmethod
|
||||||
def render_tag_helper(type, content, margin_id, margin_anchor_id):
|
def render_tag_helper(type, content, margin_id, margin_anchor_id):
|
||||||
"""This function is used to render the different types of tags. It is
|
"""This function is used to render the different types of tags. It is
|
||||||
|
|||||||
@@ -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*(.+)\]"')
|
||||||
@@ -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]
|
||||||
Reference in New Issue
Block a user