From f56abca9dfe97516443fa8df3c8ebb9ecf71bcee Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 16 Jun 2011 10:56:37 -0600 Subject: [PATCH] MOBI Output: Allow setting of background color on tables. Fixes #797580 (Private bug) --- src/calibre/ebooks/mobi/mobiml.py | 6 ++++- src/calibre/ebooks/oeb/stylizer.py | 39 ++++++++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/mobi/mobiml.py b/src/calibre/ebooks/mobi/mobiml.py index 493767e233..d108742f3c 100644 --- a/src/calibre/ebooks/mobi/mobiml.py +++ b/src/calibre/ebooks/mobi/mobiml.py @@ -443,11 +443,15 @@ class MobiMLizer(object): tag = 'span' if tag == 'td' else 'div' if tag == 'table': + col = style.backgroundColor + if col: + elem.set('bgcolor', col) css = style.cssdict() if 'border' in css or 'border-width' in css: elem.set('border', '1') if tag in TABLE_TAGS: - for attr in ('rowspan', 'colspan', 'width', 'border', 'scope'): + for attr in ('rowspan', 'colspan', 'width', 'border', 'scope', + 'bgcolor'): if attr in elem.attrib: istate.attrib[attr] = elem.attrib[attr] if tag == 'q': diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index fc7a27b5cd..5ade915e4f 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -473,6 +473,7 @@ class Style(object): self._width = None self._height = None self._lineHeight = None + self._bgcolor = None stylizer._styles[element] = self def set(self, prop, val): @@ -533,6 +534,44 @@ class Style(object): def pt_to_px(self, value): return (self._profile.dpi / 72.0) * value + @property + def backgroundColor(self): + ''' + Return the background color by parsing both the background-color and + background shortcut properties. Note that inheritance/default values + are not used. + ''' + + def validate_color(col): + return cssutils.profile.validateWithProfile('color', + col, + profiles=[cssutils.profiles.Profiles.CSS_LEVEL_2])[1] + + if self._bgcolor is None: + val = self._style.get('background-color', None) + if val and validate_color(val): + col = val + else: + val = self._style.get('background', None) + if val is not None: + try: + style = cssutils.parseStyle('background: '+val) + val = style.getProperty('background').cssValue + try: + val = list(val) + except: + # val is CSSPrimitiveValue + val = [val] + for c in val: + c = c.cssText + if validate_color(c): + col = c + break + except: + pass + self._bgcolor = col + return self._bgcolor + @property def fontSize(self): def normalize_fontsize(value, base):