mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
LIT output:Fix bug when handling relative references
This commit is contained in:
commit
906b282a15
@ -266,6 +266,14 @@ class Manifest(object):
|
|||||||
if result != 0:
|
if result != 0:
|
||||||
return result
|
return result
|
||||||
return cmp(self.id, other.id)
|
return cmp(self.id, other.id)
|
||||||
|
|
||||||
|
def abshref(self, href):
|
||||||
|
if '/' not in self.href:
|
||||||
|
return href
|
||||||
|
dirname = os.path.dirname(self.href)
|
||||||
|
href = os.path.join(dirname, href)
|
||||||
|
href = os.path.normpath(href).replace('\\', '/')
|
||||||
|
return href
|
||||||
|
|
||||||
def __init__(self, oeb):
|
def __init__(self, oeb):
|
||||||
self.oeb = oeb
|
self.oeb = oeb
|
||||||
|
@ -143,17 +143,16 @@ def warn(x):
|
|||||||
class ReBinary(object):
|
class ReBinary(object):
|
||||||
NSRMAP = {'': None, XML_NS: 'xml'}
|
NSRMAP = {'': None, XML_NS: 'xml'}
|
||||||
|
|
||||||
def __init__(self, root, path, oeb, map=HTML_MAP, logger=FauxLogger()):
|
def __init__(self, root, item, oeb, map=HTML_MAP, logger=FauxLogger()):
|
||||||
self.path = path
|
self.item = item
|
||||||
self.logger = logger
|
self.logger = logger
|
||||||
self.dir = os.path.dirname(path)
|
|
||||||
self.manifest = oeb.manifest
|
self.manifest = oeb.manifest
|
||||||
self.tags, self.tattrs = map
|
self.tags, self.tattrs = map
|
||||||
self.buf = StringIO()
|
self.buf = StringIO()
|
||||||
self.anchors = []
|
self.anchors = []
|
||||||
self.page_breaks = []
|
self.page_breaks = []
|
||||||
self.is_html = is_html = map is HTML_MAP
|
self.is_html = is_html = map is HTML_MAP
|
||||||
self.stylizer = Stylizer(root, path, oeb) if is_html else None
|
self.stylizer = Stylizer(root, item.href, oeb) if is_html else None
|
||||||
self.tree_to_binary(root)
|
self.tree_to_binary(root)
|
||||||
self.content = self.buf.getvalue()
|
self.content = self.buf.getvalue()
|
||||||
self.ahc = self.build_ahc() if is_html else None
|
self.ahc = self.build_ahc() if is_html else None
|
||||||
@ -210,6 +209,8 @@ class ReBinary(object):
|
|||||||
if attr in ('href', 'src'):
|
if attr in ('href', 'src'):
|
||||||
value = urlnormalize(value)
|
value = urlnormalize(value)
|
||||||
path, frag = urldefrag(value)
|
path, frag = urldefrag(value)
|
||||||
|
if self.item:
|
||||||
|
path = self.item.abshref(path)
|
||||||
prefix = unichr(3)
|
prefix = unichr(3)
|
||||||
if path in self.manifest.hrefs:
|
if path in self.manifest.hrefs:
|
||||||
prefix = unichr(2)
|
prefix = unichr(2)
|
||||||
@ -222,7 +223,7 @@ class ReBinary(object):
|
|||||||
elif attr.startswith('ms--'):
|
elif attr.startswith('ms--'):
|
||||||
attr = '%' + attr[4:]
|
attr = '%' + attr[4:]
|
||||||
elif tag == 'link' and attr == 'type' and value in OEB_STYLES:
|
elif tag == 'link' and attr == 'type' and value in OEB_STYLES:
|
||||||
value = OEB_CSS_MIME
|
value = CSS_MIME
|
||||||
if attr in tattrs:
|
if attr in tattrs:
|
||||||
self.write(tattrs[attr])
|
self.write(tattrs[attr])
|
||||||
else:
|
else:
|
||||||
@ -275,7 +276,7 @@ class ReBinary(object):
|
|||||||
def build_ahc(self):
|
def build_ahc(self):
|
||||||
if len(self.anchors) > 6:
|
if len(self.anchors) > 6:
|
||||||
self.logger.log_warn("More than six anchors in file %r. " \
|
self.logger.log_warn("More than six anchors in file %r. " \
|
||||||
"Some links may not work properly." % self.path)
|
"Some links may not work properly." % self.item.href)
|
||||||
data = StringIO()
|
data = StringIO()
|
||||||
data.write(unichr(len(self.anchors)).encode('utf-8'))
|
data.write(unichr(len(self.anchors)).encode('utf-8'))
|
||||||
for anchor, offset in self.anchors:
|
for anchor, offset in self.anchors:
|
||||||
@ -479,7 +480,7 @@ class LitWriter(object):
|
|||||||
secnum = 0
|
secnum = 0
|
||||||
if not isinstance(data, basestring):
|
if not isinstance(data, basestring):
|
||||||
self._add_folder(name)
|
self._add_folder(name)
|
||||||
rebin = ReBinary(data, item.href, self._oeb, map=HTML_MAP,
|
rebin = ReBinary(data, item, self._oeb, map=HTML_MAP,
|
||||||
logger=self._logger)
|
logger=self._logger)
|
||||||
self._add_file(name + '/ahc', rebin.ahc, 0)
|
self._add_file(name + '/ahc', rebin.ahc, 0)
|
||||||
self._add_file(name + '/aht', rebin.aht, 0)
|
self._add_file(name + '/aht', rebin.aht, 0)
|
||||||
@ -559,7 +560,7 @@ class LitWriter(object):
|
|||||||
meta.attrib['ms--minimum_level'] = '0'
|
meta.attrib['ms--minimum_level'] = '0'
|
||||||
meta.attrib['ms--attr5'] = '1'
|
meta.attrib['ms--attr5'] = '1'
|
||||||
meta.attrib['ms--guid'] = '{%s}' % str(uuid.uuid4()).upper()
|
meta.attrib['ms--guid'] = '{%s}' % str(uuid.uuid4()).upper()
|
||||||
rebin = ReBinary(meta, 'content.opf', self._oeb, map=OPF_MAP,
|
rebin = ReBinary(meta, None, self._oeb, map=OPF_MAP,
|
||||||
logger=self._logger)
|
logger=self._logger)
|
||||||
meta = rebin.content
|
meta = rebin.content
|
||||||
self._meta = meta
|
self._meta = meta
|
||||||
|
Loading…
x
Reference in New Issue
Block a user