From 8ba51d0303eadb9f1ef2bd95da5fe2531066ac0b Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sat, 9 Aug 2014 09:25:57 +0530 Subject: [PATCH] Conversion: Fix the word @page in a comment inside a stylesheet with no following {} causing the rest of the stylesheet to be ignored. --- src/calibre/ebooks/conversion/preprocess.py | 2 -- src/calibre/ebooks/oeb/base.py | 3 +++ src/calibre/ebooks/oeb/stylizer.py | 4 +++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/conversion/preprocess.py b/src/calibre/ebooks/conversion/preprocess.py index d28cd7058b..4cd233dac5 100644 --- a/src/calibre/ebooks/conversion/preprocess.py +++ b/src/calibre/ebooks/conversion/preprocess.py @@ -282,7 +282,6 @@ class Dehyphenator(object): class CSSPreProcessor(object): - PAGE_PAT = re.compile(r'@page[^{]*?{[^}]*?}') # Remove some of the broken CSS Microsoft products # create MS_PAT = re.compile(r''' @@ -304,7 +303,6 @@ class CSSPreProcessor(object): def __call__(self, data, add_namespace=False): from calibre.ebooks.oeb.base import XHTML_CSS_NAMESPACE - data = self.PAGE_PAT.sub('', data) data = self.MS_PAT.sub(self.ms_sub, data) if not add_namespace: return data diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 6eb6d1b341..0fcdf2ea25 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -913,6 +913,7 @@ class Manifest(object): def _parse_css(self, data): from cssutils import CSSParser, log, resolveImports + from cssutils.css import CSSRule log.setLevel(logging.WARN) log.raiseExceptions = False self.oeb.log.debug('Parsing', self.href, '...') @@ -924,6 +925,8 @@ class Manifest(object): data = parser.parseString(data, href=self.href, validate=False) data = resolveImports(data) data.namespaces['h'] = XHTML_NS + for rule in tuple(data.cssRules.rulesOfType(CSSRule.PAGE_RULE)): + data.cssRules.remove(rule) return data def _fetch_css(self, path): diff --git a/src/calibre/ebooks/oeb/stylizer.py b/src/calibre/ebooks/oeb/stylizer.py index 6241ddd07f..eb4b6ac915 100644 --- a/src/calibre/ebooks/oeb/stylizer.py +++ b/src/calibre/ebooks/oeb/stylizer.py @@ -12,7 +12,7 @@ import os, re, logging, copy, unicodedata from weakref import WeakKeyDictionary from xml.dom import SyntaxErr as CSSSyntaxError from cssutils.css import (CSSStyleRule, CSSPageRule, CSSFontFaceRule, - cssproperties) + cssproperties, CSSRule) from cssutils import (profile as cssprofiles, parseString, parseStyle, log as cssutils_log, CSSParser, profiles, replaceUrls) from lxml import etree @@ -216,6 +216,8 @@ class Stylizer(object): self.logger.warn('CSS @import of non-CSS file %r' % rule.href) continue stylesheets.append(sitem.data) + for rule in tuple(stylesheet.cssRules.rulesOfType(CSSRule.PAGE_RULE)): + stylesheet.cssRules.remove(rule) # Make links to resources absolute, since these rules will # be folded into a stylesheet at the root replaceUrls(stylesheet, item.abshref,