Storing rules in oeb and rules comparison function

This commit is contained in:
Claire 2019-05-05 13:21:25 +02:00
parent 2094df04c3
commit f3f741a1fa

View File

@ -167,6 +167,19 @@ class StylizerRules(object):
text = self.opts.change_justification text = self.opts.change_justification
return text return text
def same_rules(self, opts, profile, stylesheets):
if self.opts != opts:
# it's unlikely to happen, but better safe than sorry
return False
if self.profile != profile:
return False
if len(self.stylesheets) != len(stylesheets):
return False
for index, stylesheet in enumerate(self.stylesheets):
if stylesheet != stylesheets[index]:
return False
return True
class Stylizer(object): class Stylizer(object):
STYLESHEETS = WeakKeyDictionary() STYLESHEETS = WeakKeyDictionary()
@ -271,13 +284,11 @@ class Stylizer(object):
self.logger.debug('Bad css: ') self.logger.debug('Bad css: ')
self.logger.debug(x) self.logger.debug(x)
# using the profile to store the rules, page rule and font face rules # using oeb to store the rules, page rule and font face rules
# and generating them again if stylesheets are different # and generating them again if opts, profile or stylesheets are different
# they should depend on opts too, but opts shouldn't change during if (not hasattr(self.oeb, 'stylizer_rules')) \
# a conversion process, so it should be safe... or not self.oeb.stylizer_rules.same_rules(self.opts, self.profile, stylesheets):
if (not hasattr(self.profile, 'stylizer_rules')) \ self.oeb.stylizer_rules = StylizerRules(self.opts, self.profile, stylesheets)
or set(self.profile.stylizer_rules.stylesheets) != set(stylesheets):
self.profile.stylizer_rules = StylizerRules(self.opts, self.profile, stylesheets)
self._styles = {} self._styles = {}
pseudo_pat = re.compile(u':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I) pseudo_pat = re.compile(u':{1,2}(%s)' % ('|'.join(INAPPROPRIATE_PSEUDO_CLASSES)), re.I)
@ -350,18 +361,18 @@ class Stylizer(object):
@property @property
def rules(self): def rules(self):
return self.profile.stylizer_rules.rules return self.oeb.stylizer_rules.rules
@property @property
def page_rule(self): def page_rule(self):
return self.profile.stylizer_rules.page_rule return self.oeb.stylizer_rules.page_rule
@property @property
def font_face_rules(self): def font_face_rules(self):
return self.profile.stylizer_rules.font_face_rules return self.oeb.stylizer_rules.font_face_rules
def flatten_style(self, cssstyle): def flatten_style(self, cssstyle):
return self.profile.stylizer_rules.flatten_style(cssstyle) return self.oeb.stylizer_rules.flatten_style(cssstyle)
def _fetch_css_file(self, path): def _fetch_css_file(self, path):
hrefs = self.oeb.manifest.hrefs hrefs = self.oeb.manifest.hrefs