Kobo driver CSS modification should use mimetype

This commit is contained in:
David Forrester 2013-08-01 14:53:42 +10:00 committed by Kovid Goyal
parent 7cbad00c25
commit 08819cb340

View File

@ -1917,34 +1917,37 @@ class KOBOTOUCH(KOBO):
else: else:
debug_print("KoboTouch:_modify_epub: received container") debug_print("KoboTouch:_modify_epub: received container")
cssnames = [n for n in container.name_path_map if n.endswith('.css')] # cssnames = [n for n in container.name_path_map if n.endswith('.css')]
for cssname in cssnames: # for cssname in cssnames:
newsheet = container.parsed(cssname) from calibre.ebooks.oeb.base import OEB_STYLES
oldrules = len(newsheet.cssRules) for cssname, mt in container.mime_map.iteritems():
# remove any existing @page rules in epub css if mt in OEB_STYLES:
# if css to be appended contains an @page rule newsheet = container.parsed(cssname)
if self.extra_sheet and len([r for r in self.extra_sheet if r.type == r.PAGE_RULE]): oldrules = len(newsheet.cssRules)
page_rules = [r for r in newsheet if r.type == r.PAGE_RULE] # remove any existing @page rules in epub css
if len(page_rules) > 0: # if css to be appended contains an @page rule
debug_print("KoboTouch:_modify_epub:Removing existing @page rules") if self.extra_sheet and len([r for r in self.extra_sheet if r.type == r.PAGE_RULE]):
for rule in page_rules: page_rules = [r for r in newsheet if r.type == r.PAGE_RULE]
rule.style = '' if len(page_rules) > 0:
# remove any existing widow/orphan settings in epub css debug_print("KoboTouch:_modify_epub:Removing existing @page rules")
# if css to be appended contains a widow/orphan rule or we there is no extra CSS file for rule in page_rules:
if (len([r for r in self.extra_sheet if r.type == r.STYLE_RULE \ rule.style = ''
and (r.style['widows'] or r.style['orphans'])]) > 0): # remove any existing widow/orphan settings in epub css
widow_orphan_rules = [r for r in newsheet if r.type == r.STYLE_RULE \ # if css to be appended contains a widow/orphan rule or we there is no extra CSS file
and (r.style['widows'] or r.style['orphans'])] if (len([r for r in self.extra_sheet if r.type == r.STYLE_RULE \
if len(widow_orphan_rules) > 0: and (r.style['widows'] or r.style['orphans'])]) > 0):
debug_print("KoboTouch:_modify_epub:Removing existing widows/orphans attribs") widow_orphan_rules = [r for r in newsheet if r.type == r.STYLE_RULE \
for rule in widow_orphan_rules: and (r.style['widows'] or r.style['orphans'])]
rule.style.removeProperty('widows') if len(widow_orphan_rules) > 0:
rule.style.removeProperty('orphans') debug_print("KoboTouch:_modify_epub:Removing existing widows/orphans attribs")
# append all rules from kobo extra css stylesheet for rule in widow_orphan_rules:
for addrule in [r for r in self.extra_sheet.cssRules]: rule.style.removeProperty('widows')
newsheet.insertRule(addrule, len(newsheet.cssRules)) rule.style.removeProperty('orphans')
debug_print("KoboTouch:_modify_epub:CSS rules {0} -> {1} ({2})".format(oldrules, len(newsheet.cssRules), cssname)) # append all rules from kobo extra css stylesheet
container.dirty(cssname) for addrule in [r for r in self.extra_sheet.cssRules]:
newsheet.insertRule(addrule, len(newsheet.cssRules))
debug_print("KoboTouch:_modify_epub:CSS rules {0} -> {1} ({2})".format(oldrules, len(newsheet.cssRules), cssname))
container.dirty(cssname)
if commit_container: if commit_container:
debug_print("KoboTouch:_modify_epub: committing container.") debug_print("KoboTouch:_modify_epub: committing container.")