diff --git a/src/calibre/ebooks/epub/from_html.py b/src/calibre/ebooks/epub/from_html.py index 6341519c61..ca50fe7a5d 100644 --- a/src/calibre/ebooks/epub/from_html.py +++ b/src/calibre/ebooks/epub/from_html.py @@ -454,12 +454,9 @@ def convert(htmlfile, opts, notification=None, create_epub=True, cpath = os.path.join(tdir, 'content', 'resources', '_cover_.jpg') if os.path.exists(cpath): - opf.add_path_to_manifest(cpath, 'image/jpeg') + opf.add_path_to_manifest(cpath, 'image/jpeg') with open(opf_path, 'wb') as f: - raw = opf.render() - if not raw.startswith('\n'+raw - f.write(raw) + f.write(opf.render()) ncx_path = os.path.join(os.path.dirname(opf_path), 'toc.ncx') if os.path.exists(ncx_path) and os.stat(ncx_path).st_size > opts.profile.flow_size: logger.info('Condensing NCX from %d bytes...'%os.stat(ncx_path).st_size) @@ -475,7 +472,7 @@ def convert(htmlfile, opts, notification=None, create_epub=True, logger.info(_('Output written to ')+opts.output) if opts.show_opf: - print open(os.path.join(tdir, 'metadata.opf')).read() + print open(opf_path, 'rb').read() if opts.extract_to is not None: if os.path.exists(opts.extract_to): diff --git a/src/calibre/ebooks/html.py b/src/calibre/ebooks/html.py index 6d1b405a28..9a273c42ce 100644 --- a/src/calibre/ebooks/html.py +++ b/src/calibre/ebooks/html.py @@ -1007,7 +1007,6 @@ def merge_metadata(htmlfile, opf, opts): mi = get_metadata(open(htmlfile, 'rb'), 'html') except: mi = MetaInformation(None, None) - if opts.from_opf is not None and os.access(opts.from_opf, os.R_OK): mi.smart_update(OPF(open(opts.from_opf, 'rb'), os.path.abspath(os.path.dirname(opts.from_opf)))) for attr in ('title', 'authors', 'publisher', 'tags', 'comments'): diff --git a/src/calibre/ebooks/metadata/__init__.py b/src/calibre/ebooks/metadata/__init__.py index c7d667c831..f97b413486 100644 --- a/src/calibre/ebooks/metadata/__init__.py +++ b/src/calibre/ebooks/metadata/__init__.py @@ -247,6 +247,12 @@ class MetaInformation(object): if getattr(mi, 'cover_data', None) and mi.cover_data[0] is not None: self.cover_data = mi.cover_data + def format_series_index(self): + try: + x = float(self.series_index) + except ValueError: + x = 1.0 + return '%d'%x if int(x) == x else '%.2f'%x def __unicode__(self): ans = u'' @@ -267,7 +273,7 @@ class MetaInformation(object): if self.tags: ans += u'Tags : ' + u', '.join([unicode(t) for t in self.tags]) + '\n' if self.series: - ans += u'Series : '+unicode(self.series) + ' #%d\n'%self.series_index + ans += u'Series : '+unicode(self.series) + ' #%s\n'%self.format_series_index() if self.language: ans += u'Language : ' + unicode(self.language) + u'\n' return ans.strip() @@ -277,11 +283,11 @@ class MetaInformation(object): ans += [(_('Author(s)'), (authors_to_string(self.authors) if self.authors else _('Unknown')))] ans += [(_('Publisher'), unicode(self.publisher))] ans += [(_('Producer'), unicode(self.book_producer))] - ans += [(_('Category'), unicode(self.category))] ans += [(_('Comments'), unicode(self.comments))] ans += [('ISBN', unicode(self.isbn))] ans += [(_('Tags'), u', '.join([unicode(t) for t in self.tags]))] - ans += [(_('Series'), unicode(self.series))] + if self.series: + ans += [(_('Series'), unicode(self.series))+ ' #%s'%self.format_series_index()] ans += [(_('Language'), unicode(self.language))] for i, x in enumerate(ans): ans[i] = u'%s%s'%x diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index f7282abfcd..b24dc7795e 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -789,7 +789,10 @@ class OPF(object): return elem def render(self, encoding='utf-8'): - return etree.tostring(self.root, encoding='utf-8', pretty_print=True) + raw = etree.tostring(self.root, encoding=encoding, pretty_print=True) + if not raw.lstrip().startswith('\n'%encoding.upper()+raw + return raw def smart_update(self, mi): for attr in ('author_sort', 'title_sort', 'comments', 'category', @@ -877,7 +880,8 @@ class OPFCreator(MetaInformation): self.guide = Guide.from_opf_guide(guide_element, self.base_path) self.guide.set_basedir(self.base_path) - def render(self, opf_stream, ncx_stream=None, ncx_manifest_entry=None): + def render(self, opf_stream=sys.stdout, ncx_stream=None, + ncx_manifest_entry=None): from calibre.resources import opf_template from calibre.utils.genshi.template import MarkupTemplate template = MarkupTemplate(opf_template) diff --git a/src/calibre/gui2/dialogs/epub.py b/src/calibre/gui2/dialogs/epub.py index 1e38e4d879..387a495f87 100644 --- a/src/calibre/gui2/dialogs/epub.py +++ b/src/calibre/gui2/dialogs/epub.py @@ -18,7 +18,7 @@ from calibre.gui2 import error_dialog, choose_images, pixmap_to_data, ResizableD from calibre.ebooks.epub.from_any import SOURCE_FORMATS, config as epubconfig from calibre.ebooks.metadata import MetaInformation from calibre.ptempfile import PersistentTemporaryFile -from calibre.ebooks.metadata.opf import OPFCreator +from calibre.ebooks.metadata.opf2 import OPFCreator from calibre.ebooks.metadata import authors_to_string, string_to_authors