Cache Stylizer stylesheets based on Manifest.Item, not book-internal path

This commit is contained in:
Marshall T. Vandegrift 2009-02-08 09:04:00 -05:00
parent 65887df713
commit c2527cebe3

View File

@ -17,6 +17,7 @@ import types
import re import re
import copy import copy
from itertools import izip from itertools import izip
from weakref import WeakKeyDictionary
from xml.dom import SyntaxErr as CSSSyntaxError from xml.dom import SyntaxErr as CSSSyntaxError
import cssutils import cssutils
from cssutils.css import CSSStyleRule, CSSPageRule, CSSStyleDeclaration, \ from cssutils.css import CSSStyleRule, CSSPageRule, CSSStyleDeclaration, \
@ -107,7 +108,7 @@ class CSSSelector(etree.XPath):
class Stylizer(object): class Stylizer(object):
STYLESHEETS = {} STYLESHEETS = WeakKeyDictionary()
def __init__(self, tree, path, oeb, profile=PROFILES['PRS505']): def __init__(self, tree, path, oeb, profile=PROFILES['PRS505']):
self.oeb = oeb self.oeb = oeb
@ -132,18 +133,19 @@ class Stylizer(object):
and elem.get('type', CSS_MIME) in OEB_STYLES: and elem.get('type', CSS_MIME) in OEB_STYLES:
href = urlnormalize(elem.attrib['href']) href = urlnormalize(elem.attrib['href'])
path = item.abshref(href) path = item.abshref(href)
if path not in oeb.manifest.hrefs: sitem = oeb.manifest.hrefs.get(path, None)
if sitem is None:
self.logger.warn( self.logger.warn(
'Stylesheet %r referenced by file %r not in manifest' % 'Stylesheet %r referenced by file %r not in manifest' %
(path, item.href)) (path, item.href))
continue continue
if path in self.STYLESHEETS: if sitem in self.STYLESHEETS:
stylesheet = self.STYLESHEETS[path] stylesheet = self.STYLESHEETS[sitem]
else: else:
data = self._fetch_css_file(path)[1] data = self._fetch_css_file(path)[1]
stylesheet = parser.parseString(data, href=path) stylesheet = parser.parseString(data, href=path)
stylesheet.namespaces['h'] = XHTML_NS stylesheet.namespaces['h'] = XHTML_NS
self.STYLESHEETS[path] = stylesheet self.STYLESHEETS[sitem] = stylesheet
stylesheets.append(stylesheet) stylesheets.append(stylesheet)
rules = [] rules = []
index = 0 index = 0