Skip to content

Commit

Permalink
Conversion: Fix incorrect resolution of references to resources in HT…
Browse files Browse the repository at this point in the history
…ML files that exist in a folder level above the OPF file. This could lead to styles being incorrectly processed in such HTML files.
  • Loading branch information
kovidgoyal committed Oct 1, 2016
1 parent 18c9891 commit 89b6941
Showing 1 changed file with 35 additions and 18 deletions.
53 changes: 35 additions & 18 deletions src/calibre/ebooks/oeb/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1062,24 +1062,7 @@ def relhref(self, href):
"""Convert the URL provided in :param:`href` from a book-absolute
reference to a reference relative to this manifest item.
"""
if urlparse(href).scheme:
return href
if '/' not in self.href:
return href
base = filter(None, os.path.dirname(os.path.normpath(self.href)).replace(os.sep, '/').split('/'))
target, frag = urldefrag(href)
target = target.split('/')
index = 0
for index in xrange(min(len(base), len(target))):
if base[index] != target[index]:
break
else:
index += 1
relhref = (['..'] * (len(base) - index)) + target[index:]
relhref = '/'.join(relhref)
if frag:
relhref = '#'.join((relhref, frag))
return relhref
return rel_href(self.href, href)

def abshref(self, href):
"""Convert the URL provided in :param:`href` from a reference
Expand Down Expand Up @@ -1932,3 +1915,37 @@ def to_opf2(self, page_map=False):
if self.spine.page_progression_direction in {'ltr', 'rtl'}:
spine.attrib['page-progression-direction'] = self.spine.page_progression_direction
return results


def rel_href(base_href, href):
"""Convert the URL provided in :param:`href` to a URL relative to the URL
in :param:`base_href` """
if urlparse(href).scheme:
return href
if '/' not in base_href:
return href
base = filter(lambda x: x and x != '.', os.path.dirname(os.path.normpath(base_href)).replace(os.sep, '/').split('/'))
while True:
try:
idx = base.index('..')
except ValueError:
break
if idx > 0:
del base[idx-1:idx+1]
else:
break
if not base:
return href
target, frag = urldefrag(href)
target = target.split('/')
index = 0
for index in xrange(min(len(base), len(target))):
if base[index] != target[index]:
break
else:
index += 1
relhref = (['..'] * (len(base) - index)) + target[index:]
relhref = '/'.join(relhref)
if frag:
relhref = '#'.join((relhref, frag))
return relhref

0 comments on commit 89b6941

Please sign in to comment.