Edit book: Handle epub files which contain opf files with no <metadata> tags

This commit is contained in:
Kovid Goyal 2013-12-20 23:31:53 +05:30
parent 512d608a95
commit 72d2a7e372

View File

@ -692,21 +692,25 @@ class Container(object): # {{{
return item return item
def format_opf(self): def format_opf(self):
mdata = self.opf_xpath('//opf:metadata')[0] try:
mdata.text = '\n ' mdata = self.opf_xpath('//opf:metadata')[0]
remove = set() except IndexError:
for child in mdata: pass
child.tail = '\n ' else:
try: mdata.text = '\n '
if (child.get('name', '').startswith('calibre:') and remove = set()
child.get('content', '').strip() in {'{}', ''}): for child in mdata:
remove.add(child) child.tail = '\n '
except AttributeError: try:
continue # Happens for XML comments if (child.get('name', '').startswith('calibre:') and
for child in remove: child.get('content', '').strip() in {'{}', ''}):
mdata.remove(child) remove.add(child)
if len(mdata) > 0: except AttributeError:
mdata[-1].tail = '\n ' continue # Happens for XML comments
for child in remove:
mdata.remove(child)
if len(mdata) > 0:
mdata[-1].tail = '\n '
# Ensure name comes before content, needed for Nooks # Ensure name comes before content, needed for Nooks
for meta in self.opf_xpath('//opf:meta[@name="cover"]'): for meta in self.opf_xpath('//opf:meta[@name="cover"]'):
if 'content' in meta.attrib: if 'content' in meta.attrib: