diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py index 5fe08fe..7e8b1e7 100644 --- a/renderer/fifty_ohm_html_renderer.py +++ b/renderer/fifty_ohm_html_renderer.py @@ -4,6 +4,8 @@ from mistletoe import HtmlRenderer from renderer.comment import BlockComment, SpanComment from renderer.dash import Dash +from renderer.halfwidth_spaces import HalfwidthSpaces +from renderer.nonbreaking_spaces import NonbreakingSpaces, NonbreakingSpacesDots from renderer.quote import Quote from renderer.tag import Tag from renderer.underline import Underline @@ -15,7 +17,7 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): margin_id = 0 def __init__(self): - super().__init__(Dash, BlockComment, SpanComment, Quote, Underline, Tag) + super().__init__(Dash, BlockComment, SpanComment, Quote, Underline, Tag, HalfwidthSpaces, NonbreakingSpaces, NonbreakingSpacesDots) def render_dash(self, token): return " – " @@ -85,4 +87,17 @@ class FiftyOhmHtmlRenderer(HtmlRenderer): self.margin_id, self.margin_anchor_id ) - \ No newline at end of file + + + def render_halfwidth_spaces(self, token): + return f"{token.first}. {token.second}." + + def render_nonbreaking_spaces(self, token): + return f"{token.first} {token.second}" + + def render_nonbreaking_spaces_dots(self, token): + lookup = { + "" : "", + " " : " " + } + return f"{lookup[token.first]}{token.second}{lookup[token.third]}" \ No newline at end of file diff --git a/renderer/halfwidth_spaces.py b/renderer/halfwidth_spaces.py new file mode 100644 index 0000000..f9daf9b --- /dev/null +++ b/renderer/halfwidth_spaces.py @@ -0,0 +1,18 @@ +import re + +from mistletoe.span_token import SpanToken + + +class HalfwidthSpaces(SpanToken): + """ + Insert halfwidth space into span token (Z. B.) + Identifies "z.B.", "d.h." and "D.h." with and without space in between. + """ + pattern = re.compile(r"\b(z|d|D)\.(B|h)\.") + + def __init__(self, match_object): + self.first = match_object.group(1) + self.second = match_object.group(2) + + + diff --git a/renderer/nonbreaking_spaces.py b/renderer/nonbreaking_spaces.py new file mode 100644 index 0000000..932653f --- /dev/null +++ b/renderer/nonbreaking_spaces.py @@ -0,0 +1,30 @@ +import re + +from mistletoe.span_token import SpanToken + + +class NonbreakingSpaces(SpanToken): + """ + Insert unbreakable space into span token (§ 3) + Identifies "§$number", "Abs.$number", "Klasse [A,E,N]" w/ and w\ space between. + """ + pattern = re.compile(r"(§|Abs\.|Klasse)\s?(\d|[NEA])") + + def __init__(self, match_object): + self.first = match_object.group(1) + self.second = match_object.group(2) + +class NonbreakingSpacesDots(SpanToken): + """ + Insert unbreakable space before and after "..." if there is a space + Identifies "..." with leading and/or trailing space + """ + pattern = re.compile(r"(\s?)(\.\.\.)(\s?)") + + def __init__(self, match_object): + self.first = match_object.group(1) + self.second = match_object.group(2) + self.third = match_object.group(3) + + + diff --git a/test/test_halfwidth_spaces.py b/test/test_halfwidth_spaces.py new file mode 100644 index 0000000..0438674 --- /dev/null +++ b/test/test_halfwidth_spaces.py @@ -0,0 +1,10 @@ +import mistletoe + +from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer +from test.util import paragraph + +def test_halfwidth_space_html(): + input = "Dies ist z.B. ein Test. D.h. hier sollten d.h. Spaces ersetzt worden sein." + target = "Dies ist z. B. ein Test. D. h. hier sollten d. h. Spaces ersetzt worden sein." + + assert mistletoe.markdown(input, FiftyOhmHtmlRenderer) == paragraph(target) diff --git a/test/test_nonbreaking_spaces.py b/test/test_nonbreaking_spaces.py new file mode 100644 index 0000000..88fb8cb --- /dev/null +++ b/test/test_nonbreaking_spaces.py @@ -0,0 +1,36 @@ +import mistletoe + +from renderer.fifty_ohm_html_renderer import FiftyOhmHtmlRenderer +from test.util import paragraph + +def test_paragraph_html(): + input = "Der Test befindet sich unter §2 vielleicht aber auch § 3." + target = "Der Test befindet sich unter § 2 vielleicht aber auch § 3." + + assert mistletoe.markdown(input, FiftyOhmHtmlRenderer) == paragraph(target) + +def test_three_points_html(): + assertions = { + "Heute hatte ich ... zum Mittagessen." : "Heute hatte ich ... zum Mittagessen.", + "Niemals hätte ich gedacht, dass...ich ...nicht kann." : "Niemals hätte ich gedacht, dass...ich ...nicht kann.", + "Oh nein ...ein Pinguin." : "Oh nein ...ein Pinguin.", + "Haha .. . Hahaha" : "Haha .. . Hahaha" + } + + for key, value in assertions.items(): + assert mistletoe.markdown(key, FiftyOhmHtmlRenderer) == paragraph(value) + + +def test_absatz_html(): + input = "Der Test befindet sich unter Abs.2 vielleicht aber auch Abs. 3." + target = "Der Test befindet sich unter Abs. 2 vielleicht aber auch Abs. 3." + + assert mistletoe.markdown(input, FiftyOhmHtmlRenderer) == paragraph(target) + +def test_class_html(): + input = "Ich lerne für Klasse A, Klasse E und Klasse N." + target = "Ich lerne für Klasse A, Klasse E und Klasse N." + + assert mistletoe.markdown(input, FiftyOhmHtmlRenderer) == paragraph(target) + +