2
0
Fork 0

Corrected smiley parsing.

This commit is contained in:
Thomas Touhey 2018-07-28 20:08:06 +02:00
parent 3b3b54312a
commit a6d6f62d11
No known key found for this signature in database
GPG Key ID: 2ECEB0517AD947FB
3 changed files with 27 additions and 11 deletions

View File

@ -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.

View File

@ -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.

View File

@ -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)