mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-08 02:34:06 -04:00
Fix a couple of nasty regressions in EPUB output
This commit is contained in:
parent
a0c555706e
commit
5aa9fdb0e7
@ -391,7 +391,7 @@ def split(pathtoopf, opts, stylesheet_map):
|
|||||||
os.stat(content(f)).st_size > 5*opts.profile.flow_size)))
|
os.stat(content(f)).st_size > 5*opts.profile.flow_size)))
|
||||||
except (SplitError, RuntimeError):
|
except (SplitError, RuntimeError):
|
||||||
if not always_remove:
|
if not always_remove:
|
||||||
changes.append(Splitter(f, opts, always_remove=True))
|
changes.append(Splitter(f, opts, stylesheet_map, always_remove=True))
|
||||||
else:
|
else:
|
||||||
raise
|
raise
|
||||||
changes[-1].fix_opf(opf)
|
changes[-1].fix_opf(opf)
|
||||||
|
@ -111,6 +111,10 @@ def option_parser():
|
|||||||
parser.remove_option('--category')
|
parser.remove_option('--category')
|
||||||
parser.add_option('--tags', default=None,
|
parser.add_option('--tags', default=None,
|
||||||
help=_('A comma separated list of tags to set'))
|
help=_('A comma separated list of tags to set'))
|
||||||
|
parser.add_option('--series', default=None,
|
||||||
|
help=_('The series to which this book belongs'))
|
||||||
|
parser.add_option('--series-index', default=None,
|
||||||
|
help=_('The series index'))
|
||||||
return parser
|
return parser
|
||||||
|
|
||||||
def main(args=sys.argv):
|
def main(args=sys.argv):
|
||||||
@ -121,18 +125,31 @@ def main(args=sys.argv):
|
|||||||
return 1
|
return 1
|
||||||
stream = open(args[1], 'r+b')
|
stream = open(args[1], 'r+b')
|
||||||
mi = MetaInformation(OCFZipReader(stream, root=os.getcwdu()).opf)
|
mi = MetaInformation(OCFZipReader(stream, root=os.getcwdu()).opf)
|
||||||
|
changed = False
|
||||||
if opts.title:
|
if opts.title:
|
||||||
mi.title = opts.title
|
mi.title = opts.title
|
||||||
|
changed = True
|
||||||
if opts.authors:
|
if opts.authors:
|
||||||
mi.authors = opts.authors.split(',')
|
mi.authors = opts.authors.split(',')
|
||||||
|
changed = True
|
||||||
if opts.tags:
|
if opts.tags:
|
||||||
mi.tags = opts.tags.split(',')
|
mi.tags = opts.tags.split(',')
|
||||||
|
changed = True
|
||||||
if opts.comment:
|
if opts.comment:
|
||||||
mi.comments = opts.comment
|
mi.comments = opts.comment
|
||||||
|
changed = True
|
||||||
|
if opts.series:
|
||||||
|
mi.series = opts.series
|
||||||
|
changed = True
|
||||||
|
if opts.series_index:
|
||||||
|
mi.series_index = opts.series_index
|
||||||
|
changed = True
|
||||||
|
|
||||||
|
if changed:
|
||||||
|
stream.seek(0)
|
||||||
|
set_metadata(stream, mi)
|
||||||
stream.seek(0)
|
stream.seek(0)
|
||||||
set_metadata(stream, mi)
|
print unicode(MetaInformation(OCFZipReader(stream, root=os.getcwdu()).opf))
|
||||||
print unicode(mi)
|
|
||||||
stream.close()
|
stream.close()
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
|
@ -362,12 +362,13 @@ class Guide(ResourceCollection):
|
|||||||
|
|
||||||
class MetadataField(object):
|
class MetadataField(object):
|
||||||
|
|
||||||
def __init__(self, name, is_dc=True, formatter=None):
|
def __init__(self, name, is_dc=True, formatter=None, none_is=None):
|
||||||
self.name = name
|
self.name = name
|
||||||
self.is_dc = is_dc
|
self.is_dc = is_dc
|
||||||
self.formatter = formatter
|
self.formatter = formatter
|
||||||
|
self.none_is = none_is
|
||||||
|
|
||||||
def __get__(self, obj, type=None):
|
def __real_get__(self, obj, type=None):
|
||||||
ans = obj.get_metadata_element(self.name)
|
ans = obj.get_metadata_element(self.name)
|
||||||
if ans is None:
|
if ans is None:
|
||||||
return None
|
return None
|
||||||
@ -381,6 +382,12 @@ class MetadataField(object):
|
|||||||
return None
|
return None
|
||||||
return ans
|
return ans
|
||||||
|
|
||||||
|
def __get__(self, obj, type=None):
|
||||||
|
ans = self.__real_get__(obj, type)
|
||||||
|
if ans is None:
|
||||||
|
ans = self.none_is
|
||||||
|
return ans
|
||||||
|
|
||||||
def __set__(self, obj, val):
|
def __set__(self, obj, val):
|
||||||
elem = obj.get_metadata_element(self.name)
|
elem = obj.get_metadata_element(self.name)
|
||||||
if elem is None:
|
if elem is None:
|
||||||
@ -393,9 +400,7 @@ class OPF(object):
|
|||||||
NAMESPACES = {
|
NAMESPACES = {
|
||||||
None : "http://www.idpf.org/2007/opf",
|
None : "http://www.idpf.org/2007/opf",
|
||||||
'dc' : "http://purl.org/dc/elements/1.1/",
|
'dc' : "http://purl.org/dc/elements/1.1/",
|
||||||
'dc1' : 'http://purl.org/dc/elements/1.0/',
|
|
||||||
'opf' : "http://www.idpf.org/2007/opf",
|
'opf' : "http://www.idpf.org/2007/opf",
|
||||||
'oebpackage' : 'http://openebook.org/namespaces/oeb-package/1.0/',
|
|
||||||
}
|
}
|
||||||
xpn = NAMESPACES.copy()
|
xpn = NAMESPACES.copy()
|
||||||
xpn.pop(None)
|
xpn.pop(None)
|
||||||
@ -406,6 +411,7 @@ class OPF(object):
|
|||||||
|
|
||||||
metadata_path = XPath('descendant::*[re:match(name(), "metadata", "i")]')
|
metadata_path = XPath('descendant::*[re:match(name(), "metadata", "i")]')
|
||||||
metadata_elem_path = XPath('descendant::*[re:match(name(), $name, "i")]')
|
metadata_elem_path = XPath('descendant::*[re:match(name(), $name, "i")]')
|
||||||
|
series_path = XPath('descendant::*[re:match(name(), "series(?!_)", "i")]')
|
||||||
authors_path = XPath('descendant::*[re:match(name(), "creator", "i") and (@role="aut" or @opf:role="aut")]')
|
authors_path = XPath('descendant::*[re:match(name(), "creator", "i") and (@role="aut" or @opf:role="aut")]')
|
||||||
bkp_path = XPath('descendant::*[re:match(name(), "contributor", "i") and (@role="bkp" or @opf:role="bkp")]')
|
bkp_path = XPath('descendant::*[re:match(name(), "contributor", "i") and (@role="bkp" or @opf:role="bkp")]')
|
||||||
tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]')
|
tags_path = XPath('descendant::*[re:match(name(), "subject", "i")]')
|
||||||
@ -420,8 +426,7 @@ class OPF(object):
|
|||||||
language = MetadataField('language')
|
language = MetadataField('language')
|
||||||
comments = MetadataField('description')
|
comments = MetadataField('description')
|
||||||
category = MetadataField('category')
|
category = MetadataField('category')
|
||||||
series = MetadataField('series', is_dc=False)
|
series_index = MetadataField('series_index', is_dc=False, formatter=int, none_is=1)
|
||||||
series_index = MetadataField('series_index', is_dc=False, formatter=int)
|
|
||||||
rating = MetadataField('rating', is_dc=False, formatter=int)
|
rating = MetadataField('rating', is_dc=False, formatter=int)
|
||||||
|
|
||||||
|
|
||||||
@ -632,8 +637,26 @@ class OPF(object):
|
|||||||
matches = [self.create_metadata_element('identifier', ns='dc',
|
matches = [self.create_metadata_element('identifier', ns='dc',
|
||||||
attrib={'{%s}scheme'%self.NAMESPACES['opf']:'ISBN'})]
|
attrib={'{%s}scheme'%self.NAMESPACES['opf']:'ISBN'})]
|
||||||
matches[0].text = unicode(val)
|
matches[0].text = unicode(val)
|
||||||
|
|
||||||
return property(fget=fget, fset=fset)
|
return property(fget=fget, fset=fset)
|
||||||
|
|
||||||
|
@apply
|
||||||
|
def series():
|
||||||
|
|
||||||
|
def fget(self):
|
||||||
|
for match in self.series_path(self.metadata):
|
||||||
|
return match.text if match.text else None
|
||||||
|
|
||||||
|
def fset(self, val):
|
||||||
|
matches = self.series_path(self.metadata)
|
||||||
|
if not matches:
|
||||||
|
matches = [self.create_metadata_element('series')]
|
||||||
|
matches[0].text = unicode(val)
|
||||||
|
|
||||||
|
return property(fget=fget, fset=fset)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@apply
|
@apply
|
||||||
def book_producer():
|
def book_producer():
|
||||||
|
|
||||||
@ -682,7 +705,6 @@ class OPF(object):
|
|||||||
|
|
||||||
return property(fget=fget, fset=fset)
|
return property(fget=fget, fset=fset)
|
||||||
|
|
||||||
|
|
||||||
def get_metadata_element(self, name):
|
def get_metadata_element(self, name):
|
||||||
matches = self.metadata_elem_path(self.metadata, name=name)
|
matches = self.metadata_elem_path(self.metadata, name=name)
|
||||||
if matches:
|
if matches:
|
||||||
@ -700,11 +722,12 @@ class OPF(object):
|
|||||||
def smart_update(self, mi):
|
def smart_update(self, mi):
|
||||||
for attr in ('author_sort', 'title_sort', 'comments', 'category',
|
for attr in ('author_sort', 'title_sort', 'comments', 'category',
|
||||||
'publisher', 'series', 'series_index', 'rating',
|
'publisher', 'series', 'series_index', 'rating',
|
||||||
'isbn', 'language', 'tags'):
|
'isbn', 'language', 'tags', 'title', 'authors'):
|
||||||
val = getattr(mi, attr, None)
|
val = getattr(mi, attr, None)
|
||||||
if val or val == []:
|
if val is not None and val != [] and val != (None, None):
|
||||||
setattr(self, attr, val)
|
setattr(self, attr, val)
|
||||||
|
|
||||||
|
|
||||||
class OPFCreator(MetaInformation):
|
class OPFCreator(MetaInformation):
|
||||||
|
|
||||||
def __init__(self, base_path, *args, **kwargs):
|
def __init__(self, base_path, *args, **kwargs):
|
||||||
|
Loading…
x
Reference in New Issue
Block a user