Pull request #9: Feature/spaces
Merge in FRAG/50ohm.de from feature/spaces to main * commit '641d1c94ded5b817c613cd38738dbe0b51e3b55c': refactor: Fixed desired ... handling and more tests feat: Renderer add nonbreakable spaces and halfwidth spaces
This commit is contained in:
@@ -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
|
||||
)
|
||||
|
||||
|
||||
|
||||
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]}"
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
|
||||
|
||||
@@ -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)
|
||||
@@ -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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user