Merge pull request #419 from akshayaurora/rst

Replace invalid input with sane ones for color and bb codes in color-pa...
This commit is contained in:
Mathieu Virbel 2012-03-19 03:08:39 -07:00
commit ec1b470242
2 changed files with 29 additions and 5 deletions

View File

@ -93,7 +93,7 @@ class MarkupLabel(MarkupLabelBase):
self._style_stack[k].append(self.options[k])
def _pop_style(self, k):
if len(self._style_stack[k]) == 0:
if k not in self._style_stack or len(self._style_stack[k]) == 0:
Logger.warning('Label: pop style stack without push')
return
v = self._style_stack[k].pop()
@ -135,7 +135,10 @@ class MarkupLabel(MarkupLabelBase):
spop('italic')
self.resolve_font_name()
elif item[:6] == '[size=':
size = int(item[6:-1])
try:
size = int(item[6:-1])
except ValueError:
size = options['font_size']
spush('font_size')
options['font_size'] = size
elif item == '[/size]':

View File

@ -29,6 +29,12 @@ def parse_filename(filename):
return result or filename
def color_error(text):
#show warning and return a sane value
Logger.warning(text)
return (0, 0, 0, 1)
def parse_color(text):
'''Parse a string to a kivy color. Supported formats :
* rgb(r, g, b)
@ -41,7 +47,18 @@ def parse_color(text):
value = [1, 1, 1, 1]
if text.startswith('rgb'):
res = re.match('rgba?\((.*)\)', text)
value = [int(x) / 255. for x in re.split(',\ ?', res.groups()[0])]
if res:
try:
# default r/g/b values to 1 if greater than 255 else x/255
value = [1 if int(x) > 255. else (int(x) / 255.)
for x in re.split(',\ ?', res.groups()[0])]
if len(value) < 3:
#in case of invalid input like rgb()/rgb(r)/rgb(r, g)
raise ValueError
except ValueError, AttributeError:
return color_error('Color Parser:Invalid color for %r' % text)
else:
return color_error('Color Parser:Invalid color for %r' % text)
if len(value) == 3:
value.append(1.)
elif len(text):
@ -52,8 +69,12 @@ def parse_color(text):
if lres == 3 or lres == 4:
res = ''.join([x + x for x in res])
elif lres != 6 and lres != 8:
raise ColorException('Invalid color format for %r' % text)
value = [int(res[i:i+2], 16) / 255. for i in xrange(0, len(res), 2)]
#raise ColorException('Invalid color format for %r' % text)
return color_error('Color Parser:Invalid color format for %r' %text)
try:
value = [int(res[i:i+2], 16) / 255. for i in xrange(0, len(res), 2)]
except ValueError:
return color_error('Color Parser:Invalid color for %r' % text)
if lres == 6:
value.append(1.)
return value