Fix bug in parsing OPF <guide/> elements.

Add relhref method for building hrefs relative to a manifest item.
This commit is contained in:
Marshall T. Vandegrift 2008-12-18 17:19:23 -05:00
parent 75d2291233
commit 46c52b3bb5

View File

@ -119,7 +119,7 @@ class DirContainer(AbstractContainer):
def exists(self, path):
path = os.path.join(self.rootdir, path)
return os.path.isfile(path)
return os.path.isfile(urlunquote(path))
class Metadata(object):
@ -275,6 +275,22 @@ class Manifest(object):
return result
return cmp(self.id, other.id)
def relhref(self, href):
if '/' not in self.href:
return href
base = os.path.dirname(self.href).split('/')
target, frag = urldefrag(href)
target = target.split('/')
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
def __init__(self, oeb):
self.oeb = oeb
self.ids = {}
@ -297,6 +313,7 @@ class Manifest(object):
self.oeb.spine.remove(item)
def generate(self, id, href):
href = urlnormalize(href)
base = id
index = 1
while id in self.ids:
@ -640,7 +657,8 @@ class OEBBook(object):
self.guide = guide = Guide(self)
for elem in xpath(opf, '/o2:package/o2:guide/o2:reference'):
href = elem.get('href')
if href not in self.manifest.hrefs:
path, frag = urldefrag(href)
if path not in self.manifest.hrefs:
self.logger.log_warn(u'Guide reference %r not found' % href)
continue
guide.add(elem.get('type'), elem.get('title'), href)