diff --git a/src/calibre/ebooks/oeb/polish/utils.py b/src/calibre/ebooks/oeb/polish/utils.py index 33b5b4b5ad..c66ef02d04 100644 --- a/src/calibre/ebooks/oeb/polish/utils.py +++ b/src/calibre/ebooks/oeb/polish/utils.py @@ -37,7 +37,7 @@ class CommentFinder(object): q = bisect(self.starts, offset) - 1 return q >= 0 and self.starts[q] <= offset <= self.ends[q] -def link_stylesheets(container, names, sheets, mtype='text/css'): +def link_stylesheets(container, names, sheets, remove=False, mtype='text/css'): from calibre.ebooks.oeb.base import XPath, XHTML changed_names = set() snames = set(sheets) @@ -45,6 +45,12 @@ def link_stylesheets(container, names, sheets, mtype='text/css'): hp = XPath('//h:head') for name in names: root = container.parsed(name) + if remove: + for link in lp(root): + if (link.get('type', mtype) or mtype) == mtype: + container.remove_from_xml(link) + changed_names.add(name) + container.dirty(name) existing = {container.href_to_name(l.get('href'), name) for l in lp(root) if (l.get('type', mtype) or mtype) == mtype} extra = snames - existing if extra: diff --git a/src/calibre/gui2/tweak_book/__init__.py b/src/calibre/gui2/tweak_book/__init__.py index 8540808468..17d33f8df0 100644 --- a/src/calibre/gui2/tweak_book/__init__.py +++ b/src/calibre/gui2/tweak_book/__init__.py @@ -35,6 +35,7 @@ d['preview_standard_font_family'] = 'serif' d['preview_base_font_size'] = 18 d['preview_mono_font_size'] = 14 d['preview_minimum_font_size'] = 8 +d['remove_existing_links_when_linking_sheets'] = True del d diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 7d9caa5d19..dc6da3ae92 100644 --- a/src/calibre/gui2/tweak_book/boss.py +++ b/src/calibre/gui2/tweak_book/boss.py @@ -834,10 +834,10 @@ class Boss(QObject): self.show_editor(master) @in_thread_job - def link_stylesheets_requested(self, names, sheets): + def link_stylesheets_requested(self, names, sheets, remove): self.commit_all_editors_to_container() self.add_savepoint(_('Link stylesheets')) - changed_names = link_stylesheets(current_container(), names, sheets) + changed_names = link_stylesheets(current_container(), names, sheets, remove) if changed_names: self.update_editors_from_container(names=changed_names) self.set_modified() diff --git a/src/calibre/gui2/tweak_book/file_list.py b/src/calibre/gui2/tweak_book/file_list.py index ddaf43cce6..5d60a5e451 100644 --- a/src/calibre/gui2/tweak_book/file_list.py +++ b/src/calibre/gui2/tweak_book/file_list.py @@ -16,7 +16,7 @@ from PyQt4.Qt import ( QWidget, QTreeWidget, QGridLayout, QSize, Qt, QTreeWidgetItem, QIcon, QFont, QStyledItemDelegate, QStyle, QPixmap, QPainter, pyqtSignal, QMenu, QTimer, QDialogButtonBox, QDialog, QLabel, QLineEdit, QVBoxLayout, QScrollArea, - QRadioButton, QFormLayout, QSpinBox, QListWidget, QListWidgetItem) + QRadioButton, QFormLayout, QSpinBox, QListWidget, QListWidgetItem, QCheckBox) from calibre import human_readable, sanitize_file_name_unicode from calibre.ebooks.oeb.base import OEB_STYLES, OEB_DOCS @@ -24,7 +24,7 @@ from calibre.ebooks.oeb.polish.container import guess_type, OEB_FONTS from calibre.ebooks.oeb.polish.cover import ( get_cover_page_name, get_raster_cover_name, is_raster_image) from calibre.gui2 import error_dialog, choose_files, question_dialog, elided_text, choose_save_file -from calibre.gui2.tweak_book import current_container +from calibre.gui2.tweak_book import current_container, tprefs from calibre.gui2.tweak_book.editor import syntax_from_mime from calibre.gui2.tweak_book.templates import template_for from calibre.utils.icu import sort_key @@ -149,7 +149,7 @@ class FileList(QTreeWidget): mark_requested = pyqtSignal(object, object) export_requested = pyqtSignal(object, object) replace_requested = pyqtSignal(object, object, object, object) - link_stylesheets_requested = pyqtSignal(object, object) + link_stylesheets_requested = pyqtSignal(object, object, object) def __init__(self, parent=None): QTreeWidget.__init__(self, parent) @@ -652,13 +652,17 @@ class FileList(QTreeWidget): flags = Qt.ItemIsEnabled | Qt.ItemIsUserCheckable | Qt.ItemIsDragEnabled | Qt.ItemIsSelectable i.setFlags(flags) i.setCheckState(Qt.Checked) + d.r = r = QCheckBox(_('Remove existing links to stylesheets')) + r.setChecked(tprefs['remove_existing_links_when_linking_sheets']) + l.addWidget(r) d.bb = bb = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel) bb.accepted.connect(d.accept), bb.rejected.connect(d.reject) l.addWidget(bb) if d.exec_() == d.Accepted: + tprefs['remove_existing_links_when_linking_sheets'] = r.isChecked() sheets = [unicode(s.item(i).text()) for i in xrange(s.count()) if s.item(i).checkState() == Qt.Checked] if sheets: - self.link_stylesheets_requested.emit(names, sheets) + self.link_stylesheets_requested.emit(names, sheets, r.isChecked()) class NewFileDialog(QDialog): # {{{