diff --git a/src/calibre/ebooks/metadata/opf2.py b/src/calibre/ebooks/metadata/opf2.py index c603fcd41f..dd4e7e41b1 100644 --- a/src/calibre/ebooks/metadata/opf2.py +++ b/src/calibre/ebooks/metadata/opf2.py @@ -24,6 +24,23 @@ from calibre import prints, guess_type from calibre.utils.cleantext import clean_ascii_chars, clean_xml_chars from calibre.utils.config import tweaks +pretty_print_opf = False + +class PrettyPrint(object): + def __enter__(self): + global pretty_print_opf + pretty_print_opf = True + + def __exit__(self, *args): + global pretty_print_opf + pretty_print_opf = False +pretty_print = PrettyPrint() + +def _pretty_print(root): + from calibre.ebooks.oeb.polish.pretty import pretty_opf, pretty_xml_tree + pretty_opf(root) + pretty_xml_tree(root) + class Resource(object): # {{{ ''' Represents a resource (usually a file on the filesystem or a URL pointing @@ -1210,6 +1227,8 @@ class OPF(object): # {{{ a['content'] = c self.write_user_metadata() + if pretty_print_opf: + _pretty_print(self.root) raw = etree.tostring(self.root, encoding=encoding, pretty_print=True) if not raw.lstrip().startswith('\n'%encoding.upper()+raw @@ -1562,6 +1581,9 @@ def metadata_to_opf(mi, as_string=True, default_lang=None): attrib={'type':'cover', 'title':_('Cover'), 'href':mi.cover}) r.tail = '\n' +(' '*4) guide.append(r) + if pretty_print_opf: + _pretty_print(root) + return etree.tostring(root, pretty_print=True, encoding='utf-8', xml_declaration=True) if as_string else root diff --git a/src/calibre/gui2/actions/tweak_epub.py b/src/calibre/gui2/actions/tweak_epub.py index 681ce04c4a..2f8eeb02d3 100755 --- a/src/calibre/gui2/actions/tweak_epub.py +++ b/src/calibre/gui2/actions/tweak_epub.py @@ -110,8 +110,8 @@ class TweakEpubAction(InterfaceAction): _('The book must be in the %s formats to tweak.' '\n\nFirst convert the book to one of these formats.') % (_(' or '.join(SUPPORTED))), show=True) + from calibre.gui2.tweak_book import tprefs if len(tweakable_fmts) > 1: - from calibre.gui2.tweak_book import tprefs if tprefs['choose_tweak_fmt']: d = Choose(sorted(tweakable_fmts, key=tprefs.defaults['tweak_fmt_order'].index), self.gui) if d.exec_() != d.Accepted: @@ -131,6 +131,12 @@ class TweakEpubAction(InterfaceAction): ' library maintenance.') % fmt, show=True) tweak = 'ebook-tweak' self.gui.setCursor(Qt.BusyCursor) + if tprefs['update_metadata_from_calibre']: + from calibre.ebooks.metadata.opf2 import pretty_print + from calibre.ebooks.metadata.meta import set_metadata + mi = db.new_api.get_metadata(book_id) + with pretty_print, open(path, 'r+b') as f: + set_metadata(f, mi, stream_type=fmt.lower()) try: self.gui.job_manager.launch_gui_app(tweak, kwargs=dict(args=[tweak, path])) time.sleep(2) diff --git a/src/calibre/gui2/tweak_book/__init__.py b/src/calibre/gui2/tweak_book/__init__.py index 72aec0661c..27c4549f1a 100644 --- a/src/calibre/gui2/tweak_book/__init__.py +++ b/src/calibre/gui2/tweak_book/__init__.py @@ -17,6 +17,7 @@ tprefs.defaults['editor_tab_stop_width'] = 2 tprefs.defaults['preview_refresh_time'] = 2 tprefs.defaults['choose_tweak_fmt'] = True tprefs.defaults['tweak_fmt_order'] = ['EPUB', 'AZW3'] +tprefs.defaults['update_metadata_from_calibre'] = True _current_container = None diff --git a/src/calibre/gui2/tweak_book/preferences.py b/src/calibre/gui2/tweak_book/preferences.py index 33607f6bfb..9ee4abaf05 100644 --- a/src/calibre/gui2/tweak_book/preferences.py +++ b/src/calibre/gui2/tweak_book/preferences.py @@ -178,6 +178,13 @@ class IntegrationSettings(BasicSettings): self.l = l = QFormLayout(self) self.setLayout(l) + um = self('update_metadata_from_calibre') + um.setText(_('Update metadata embedded in the book when opening')) + um.setToolTip('

' + _( + 'When the file is opened, update the metadata embedded in the book file to the current metadata' + ' in the calibre library.')) + l.addRow(um) + ask = self('choose_tweak_fmt') ask.setText(_('Ask which format to tweak if more than one format is available for the book')) l.addRow(ask)