diff --git a/test/test_html.py b/test/test_html.py index 410c6ee..56b374c 100755 --- a/test/test_html.py +++ b/test/test_html.py @@ -166,6 +166,12 @@ __test_cases = { # Text rotation obfuscation. '[rot13]obawbhe[/rot13]': '

bonjour

', + + # Smileys. + ':)': '

', + ':):)': '

:):)

', + ':) :D': '

' \ + '

', } # Define the tests wrapper, and define the classes. diff --git a/textoutpc/_html.py b/textoutpc/_html.py index 0472e18..e0a206d 100755 --- a/textoutpc/_html.py +++ b/textoutpc/_html.py @@ -24,16 +24,19 @@ class SmileyConvertor: self._html.keys())) + ')(\\s|$)') def convert(self, text): - def sub_html(m): - return m.group(1) + '' + m.group(3) + cv = "" - # FIXME: this is to avoid the ":):)" case, but there probably - # is a cleaner way to fix this… + while text: + try: + m = next(self._re.finditer(text)) + except StopIteration: + break - text = self._re.sub(sub_html, text) - text = self._re.sub(sub_html, text) - return text + cv += text[:m.start()] + m.group(1) + cv += '' + text = m.group(3) + text[m.end():] + + return cv + text # --- # URLs. diff --git a/textoutpc/color/_read.py b/textoutpc/color/_read.py index 79c88af..0c800ed 100755 --- a/textoutpc/color/_read.py +++ b/textoutpc/color/_read.py @@ -71,10 +71,17 @@ def get_color(value): except: pass # Initialize the alpha. + alpha = 1.0 # Get the match. - match = _cr.fullmatch(value).groupdict() + + match = _cr.fullmatch(value) + if not match: + raise ValueError("invalid color string") + + match = match.groupdict() + if match['hex_digits'] or match['legacy_chars']: # Imitate the Netscape behaviour. Find more about this here: # https://stackoverflow.com/a/8333464 @@ -247,9 +254,9 @@ def get_color(value): r, g, b = map(lambda x: int(round(x * 255)), (r, g, b)) if r < 0 or r > 255 or g < 0 or g > 255 or b < 0 or b > 255: - raise Exception + raise ValueError("invalid color string") if alpha < 0.0 or alpha > 1.0: - raise Exception + raise ValueError("invalid color string") alpha = round(alpha, 4) return (r, g, b, alpha)