Linking stylesheets: Add option to remove existing links

This commit is contained in:
Kovid Goyal 2013-12-23 11:59:26 +05:30
parent d72cfadb6f
commit 677dc14286
4 changed files with 18 additions and 7 deletions

View File

@ -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:

View File

@ -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

View File

@ -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()

View File

@ -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): # {{{