diff --git a/renderer/fifty_ohm_html_renderer.py b/renderer/fifty_ohm_html_renderer.py
index db41c4b..b568b0c 100644
--- a/renderer/fifty_ohm_html_renderer.py
+++ b/renderer/fifty_ohm_html_renderer.py
@@ -53,11 +53,13 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
return f"{self.render_inner(token)}"
@classmethod
- def render_unit(cls, token):
- unit = token.prefix + cls.format_unit(token.unit)
+ def render_unit(cls, token: Unit):
+ unit = token.prefix + cls.convert_unit_helper(token.unit)
if token.unit in ["°", "%"]:
+ # Special cases with no space between value and unit.
return f"{token.value}{unit}"
else:
+ # Default case is rendered with a narrow no-break space.
return f"{token.value} {unit}"
units = {
@@ -65,7 +67,11 @@ class FiftyOhmHtmlRenderer(HtmlRenderer):
}
@classmethod
- def format_unit(cls, unit):
+ def convert_unit_helper(cls, unit: str) -> str:
+ """Converts human-typable units to their preferred representation.
+
+ :param str unit: The unit to convert
+ """
if unit in cls.units.keys():
return cls.units[unit]
else:
diff --git a/renderer/unit.py b/renderer/unit.py
index 7364ff0..4a6b9f0 100644
--- a/renderer/unit.py
+++ b/renderer/unit.py
@@ -35,17 +35,21 @@ class Unit(SpanToken):
prefixes = ["p", "n", "μ", "m", "k", "M", "G"]
pattern = re.compile(
- r"([0-9,]+) ?("
+ r"(?P[0-9,]+) ?(?P"
# Group all prefixes together in a capture group
# Empty string at end of list is required so the group always exists in the resulting match
+ "|".join(prefixes + [""])
- + ")("
+ + ")(?P"
# Group all units together in a capture group.
+ "|".join(units)
+ r")(?!\w)"
)
- def __init__(self, match):
- self.value = match.group(1)
- self.prefix = match.group(2)
- self.unit = match.group(3)
+ value: str
+ prefix: str
+ unit: str
+
+ def __init__(self, match: re.Match):
+ self.value = match.group("value")
+ self.prefix = match.group("prefix") or ""
+ self.unit = match.group("unit")