From 668e2086118beaf2468fd7feca4f5f0154b4a2c2 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Wed, 8 Aug 2018 13:21:49 +0530 Subject: [PATCH] Edit book: Allow removing the currently highlighted tag (while keeping its contents) by pressing Ctrl+>. You can also add a tool to do it via Preferences->Toolbars --- src/calibre/gui2/tweak_book/editor/smarts/html.py | 14 ++++++++++++++ src/calibre/gui2/tweak_book/editor/text.py | 4 ++++ src/calibre/gui2/tweak_book/editor/widget.py | 3 +++ 3 files changed, 21 insertions(+) diff --git a/src/calibre/gui2/tweak_book/editor/smarts/html.py b/src/calibre/gui2/tweak_book/editor/smarts/html.py index e571f1472b06..9ee63c403cf8 100644 --- a/src/calibre/gui2/tweak_book/editor/smarts/html.py +++ b/src/calibre/gui2/tweak_book/editor/smarts/html.py @@ -350,6 +350,20 @@ def jump_to_enclosing_tag(self, editor, start=True): editor.setTextCursor(c) return True + def remove_tag(self, editor): + editor.highlighter.join() + + def erase_tag(tag): + c = editor.textCursor() + c.setPosition(tag.start_block.position() + tag.start_offset) + c.setPosition(tag.end_block.position() + tag.end_offset + 1, c.KeepAnchor) + c.removeSelectedText() + + if self.last_matched_closing_tag: + erase_tag(self.last_matched_closing_tag) + if self.last_matched_tag: + erase_tag(self.last_matched_tag) + def rename_block_tag(self, editor, new_name): editor.highlighter.join() c = editor.textCursor() diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py index e76c0b34b012..405901c9161b 100644 --- a/src/calibre/gui2/tweak_book/editor/text.py +++ b/src/calibre/gui2/tweak_book/editor/text.py @@ -877,6 +877,10 @@ def insert_tag(self, tag): if hasattr(self.smarts, 'insert_tag'): self.smarts.insert_tag(self, tag) + def remove_tag(self): + if hasattr(self.smarts, 'remove_tag'): + self.smarts.remove_tag(self) + def keyPressEvent(self, ev): if ev.key() == Qt.Key_X and ev.modifiers() == Qt.AltModifier: if self.replace_possible_unicode_sequence(): diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py index 376608d903f2..3db90cebbb66 100644 --- a/src/calibre/gui2/tweak_book/editor/widget.py +++ b/src/calibre/gui2/tweak_book/editor/widget.py @@ -112,6 +112,9 @@ def reg(*args, **kw): ac = reg('code.png', _('Insert &tag'), ('insert_tag',), 'insert-tag', ('Ctrl+<'), _('Insert tag'), syntaxes=('html', 'xml')) ac.setToolTip(_('

Insert tag

Insert a tag, if some text is selected the tag will be inserted around the selected text')) + ac = reg('trash.png', _('Remove &tag'), ('remove_tag',), 'remove-tag', ('Ctrl+>'), _('Remove tag'), syntaxes=('html', 'xml')) + ac.setToolTip(_('

Remove tag

Remove the currently highlighted tag')) + editor_toolbar_actions['html']['fix-html-current'] = actions['fix-html-current'] for s in ('xml', 'html', 'css'): editor_toolbar_actions[s]['pretty-current'] = actions['pretty-current']