mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
HTML Input: Handle @import directives in linked css files. Fixes #5135 (style import error during HTML conversion with ebook-convert.exe)
This commit is contained in:
parent
7ea679768e
commit
ffb9002c32
@ -107,9 +107,21 @@ class CSSPreProcessor(object):
|
||||
|
||||
PAGE_PAT = re.compile(r'@page[^{]*?{[^}]*?}')
|
||||
|
||||
def __call__(self, data):
|
||||
def __call__(self, data, add_namespace=False):
|
||||
from calibre.ebooks.oeb.base import XHTML_CSS_NAMESPACE
|
||||
data = self.PAGE_PAT.sub('', data)
|
||||
if not add_namespace:
|
||||
return data
|
||||
ans, namespaced = [], False
|
||||
for line in data.splitlines():
|
||||
ll = line.lstrip()
|
||||
if not (namespaced or ll.startswith('@import') or
|
||||
ll.startswith('@charset')):
|
||||
ans.append(XHTML_CSS_NAMESPACE.strip())
|
||||
namespaced = True
|
||||
ans.append(line)
|
||||
|
||||
return u'\n'.join(ans)
|
||||
|
||||
class HTMLPreProcessor(object):
|
||||
|
||||
|
@ -312,6 +312,7 @@ class HTMLInput(InputFormatPlugin):
|
||||
xpath
|
||||
from calibre import guess_type
|
||||
import cssutils
|
||||
self.OEB_STYLES = OEB_STYLES
|
||||
oeb = create_oebbook(log, None, opts, self,
|
||||
encoding=opts.input_encoding, populate=False)
|
||||
self.oeb = oeb
|
||||
@ -376,7 +377,7 @@ class HTMLInput(InputFormatPlugin):
|
||||
rewrite_links(item.data, partial(self.resource_adder, base=dpath))
|
||||
|
||||
for item in oeb.manifest.values():
|
||||
if item.media_type in OEB_STYLES:
|
||||
if item.media_type in self.OEB_STYLES:
|
||||
dpath = None
|
||||
for path, href in self.added_resources.items():
|
||||
if href == item.href:
|
||||
@ -414,25 +415,30 @@ class HTMLInput(InputFormatPlugin):
|
||||
oeb.container = DirContainer(os.getcwdu(), oeb.log)
|
||||
return oeb
|
||||
|
||||
|
||||
def resource_adder(self, link_, base=None):
|
||||
def link_to_local_path(self, link_, base=None):
|
||||
if not isinstance(link_, unicode):
|
||||
try:
|
||||
link_ = link_.decode('utf-8', 'error')
|
||||
except:
|
||||
self.log.warn('Failed to decode link %r. Ignoring'%link_)
|
||||
return link_
|
||||
return None, None
|
||||
try:
|
||||
l = Link(link_, base if base else os.path.getcwdu())
|
||||
l = Link(link_, base if base else os.getcwdu())
|
||||
except:
|
||||
self.log.exception('Failed to process link: %r'%link_)
|
||||
return link_
|
||||
return None, None
|
||||
if l.path is None:
|
||||
# Not a local resource
|
||||
return link_
|
||||
return None, None
|
||||
link = l.path.replace('/', os.sep).strip()
|
||||
frag = l.fragment
|
||||
if not link:
|
||||
return None, None
|
||||
return link, frag
|
||||
|
||||
def resource_adder(self, link_, base=None):
|
||||
link, frag = self.link_to_local_path(link_, base=base)
|
||||
if link is None:
|
||||
return link_
|
||||
try:
|
||||
if base and not os.path.isabs(link):
|
||||
@ -460,6 +466,9 @@ class HTMLInput(InputFormatPlugin):
|
||||
|
||||
item = self.oeb.manifest.add(id, href, media_type)
|
||||
item.html_input_href = bhref
|
||||
if guessed in self.OEB_STYLES:
|
||||
item.override_css_fetch = partial(
|
||||
self.css_import_handler, os.path.dirname(link))
|
||||
item.data
|
||||
self.added_resources[link] = href
|
||||
|
||||
@ -468,7 +477,17 @@ class HTMLInput(InputFormatPlugin):
|
||||
nlink = '#'.join((nlink, frag))
|
||||
return nlink
|
||||
|
||||
|
||||
def css_import_handler(self, base, href):
|
||||
link, frag = self.link_to_local_path(href, base=base)
|
||||
if link is None or not os.access(link, os.R_OK) or os.path.isdir(link):
|
||||
return (None, None)
|
||||
try:
|
||||
raw = open(link, 'rb').read().decode('utf-8', 'replace')
|
||||
raw = self.oeb.css_preprocessor(raw, add_namespace=True)
|
||||
except:
|
||||
self.log.exception('Failed to read CSS file: %r'%link)
|
||||
return (None, None)
|
||||
return (None, raw)
|
||||
|
||||
|
||||
|
||||
|
@ -17,6 +17,7 @@ from urlparse import urljoin
|
||||
|
||||
from lxml import etree, html
|
||||
from cssutils import CSSParser
|
||||
from cssutils.css import CSSRule
|
||||
|
||||
import calibre
|
||||
from calibre.constants import filesystem_encoding
|
||||
@ -762,6 +763,7 @@ class Manifest(object):
|
||||
self.href = self.path = urlnormalize(href)
|
||||
self.media_type = media_type
|
||||
self.fallback = fallback
|
||||
self.override_css_fetch = None
|
||||
self.spine_position = None
|
||||
self.linear = True
|
||||
if loader is None and data is None:
|
||||
@ -982,15 +984,40 @@ class Manifest(object):
|
||||
|
||||
|
||||
def _parse_css(self, data):
|
||||
|
||||
def get_style_rules_from_import(import_rule):
|
||||
ans = []
|
||||
if not import_rule.styleSheet:
|
||||
return ans
|
||||
rules = import_rule.styleSheet.cssRules
|
||||
for rule in rules:
|
||||
if rule.type == CSSRule.IMPORT_RULE:
|
||||
ans.extend(get_style_rules_from_import(rule))
|
||||
elif rule.type in (CSSRule.FONT_FACE_RULE,
|
||||
CSSRule.STYLE_RULE):
|
||||
ans.append(rule)
|
||||
return ans
|
||||
|
||||
self.oeb.log.debug('Parsing', self.href, '...')
|
||||
data = self.oeb.decode(data)
|
||||
data = self.oeb.css_preprocessor(data)
|
||||
data = XHTML_CSS_NAMESPACE + data
|
||||
data = self.oeb.css_preprocessor(data, add_namespace=True)
|
||||
parser = CSSParser(loglevel=logging.WARNING,
|
||||
fetcher=self._fetch_css,
|
||||
fetcher=self.override_css_fetch or self._fetch_css,
|
||||
log=_css_logger)
|
||||
data = parser.parseString(data, href=self.href)
|
||||
data.namespaces['h'] = XHTML_NS
|
||||
import_rules = list(data.cssRules.rulesOfType(CSSRule.IMPORT_RULE))
|
||||
rules_to_append = []
|
||||
insert_index = None
|
||||
for r in data.cssRules.rulesOfType(CSSRule.STYLE_RULE):
|
||||
insert_index = data.cssRules.index(r)
|
||||
break
|
||||
for rule in import_rules:
|
||||
rules_to_append.extend(get_style_rules_from_import(rule))
|
||||
for r in reversed(rules_to_append):
|
||||
data.insertRule(r, index=insert_index)
|
||||
for rule in import_rules:
|
||||
data.deleteRule(rule)
|
||||
return data
|
||||
|
||||
def _fetch_css(self, path):
|
||||
|
Loading…
x
Reference in New Issue
Block a user