diff options
author | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-07-28 20:08:06 +0200 |
---|---|---|
committer | Thomas "Cakeisalie5" Touhey <thomas@touhey.fr> | 2018-07-28 20:08:06 +0200 |
commit | a6d6f62d115f66a159cf7cd5759b9d5ffe68288f (patch) | |
tree | 50c5d43f4e5cd0a9d0ed342e82bd75311ba83c4d | |
parent | 3b3b54312a85e73df5a18859e1d6382faeea9c80 (diff) |
Corrected smiley parsing.
-rwxr-xr-x | test/test_html.py | 6 | ||||
-rwxr-xr-x | textoutpc/_html.py | 19 | ||||
-rwxr-xr-x | textoutpc/color/_read.py | 13 |
3 files changed, 27 insertions, 11 deletions
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]': '<p>bonjour</p>', + + # Smileys. + ':)': '<p><img src="/images/smileys/smile.gif"></p>', + ':):)': '<p>:):)</p>', + ':) :D': '<p><img src="/images/smileys/smile.gif"> ' \ + '<img src="/images/smileys/grin.gif"></p>', } # 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) + '<img src="' + self._html[m.group(2)] \ - + '">' + 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 += '<img src="' + self._html[m.group(2)] + '">' + 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) |