mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Edit book: Insert hyperlink: Allow specifying a template to control the markup that is inserted for the hyperlink. Fixes #1804250 [[enhancement] editor, insert hyperlink add <sup> option](https://bugs.launchpad.net/calibre/+bug/1804250)
This commit is contained in:
parent
c327410f69
commit
ff059b3066
@ -898,7 +898,7 @@ class Boss(QObject):
|
||||
self.commit_all_editors_to_container()
|
||||
d = InsertLink(current_container(), edname, initial_text=ed.get_smart_selection(), parent=self.gui)
|
||||
if d.exec_() == d.Accepted:
|
||||
ed.insert_hyperlink(d.href, d.text)
|
||||
ed.insert_hyperlink(d.href, d.text, template=d.template)
|
||||
elif action[0] == 'insert_tag':
|
||||
d = InsertTag(parent=self.gui)
|
||||
if d.exec_() == d.Accepted:
|
||||
|
@ -26,6 +26,7 @@ from calibre.utils.icu import utf16_length
|
||||
|
||||
get_offset = itemgetter(0)
|
||||
PARAGRAPH_SEPARATOR = '\u2029'
|
||||
DEFAULT_LINK_TEMPLATE = '<a href="_TARGET_">_TEXT_</a>'
|
||||
|
||||
|
||||
class Tag(object):
|
||||
@ -407,18 +408,18 @@ class Smarts(NullSmarts):
|
||||
editor.setTextCursor(cursor)
|
||||
return editor.selected_text_from_cursor(cursor)
|
||||
|
||||
def insert_hyperlink(self, editor, target, text):
|
||||
def insert_hyperlink(self, editor, target, text, template=None):
|
||||
template = template or DEFAULT_LINK_TEMPLATE
|
||||
template = template.replace('_TARGET_', prepare_string_for_xml(target, True))
|
||||
offset = template.find('_TEXT_')
|
||||
editor.highlighter.join()
|
||||
c = editor.textCursor()
|
||||
if c.hasSelection():
|
||||
c.insertText('') # delete any existing selected text
|
||||
ensure_not_within_tag_definition(c)
|
||||
c.insertText('<a href="%s">' % prepare_string_for_xml(target, True))
|
||||
p = c.position()
|
||||
c.insertText('</a>')
|
||||
p = c.position() + offset
|
||||
c.insertText(template.replace('_TEXT_', text or ''))
|
||||
c.setPosition(p) # ensure cursor is positioned inside the newly created tag
|
||||
if text:
|
||||
c.insertText(text)
|
||||
editor.setTextCursor(c)
|
||||
|
||||
def insert_tag(self, editor, name):
|
||||
|
@ -869,9 +869,9 @@ version="1.1" width="100%%" height="100%%" viewBox="0 0 {w} {h}" preserveAspectR
|
||||
c.setPosition(left + len(text), c.KeepAnchor)
|
||||
self.setTextCursor(c)
|
||||
|
||||
def insert_hyperlink(self, target, text):
|
||||
def insert_hyperlink(self, target, text, template=None):
|
||||
if hasattr(self.smarts, 'insert_hyperlink'):
|
||||
self.smarts.insert_hyperlink(self, target, text)
|
||||
self.smarts.insert_hyperlink(self, target, text, template=template)
|
||||
|
||||
def insert_tag(self, tag):
|
||||
if hasattr(self.smarts, 'insert_tag'):
|
||||
|
@ -234,8 +234,8 @@ class Editor(QMainWindow):
|
||||
def insert_image(self, href, fullpage=False, preserve_aspect_ratio=False, width=-1, height=-1):
|
||||
self.editor.insert_image(href, fullpage=fullpage, preserve_aspect_ratio=preserve_aspect_ratio, width=width, height=height)
|
||||
|
||||
def insert_hyperlink(self, href, text):
|
||||
self.editor.insert_hyperlink(href, text)
|
||||
def insert_hyperlink(self, href, text, template=None):
|
||||
self.editor.insert_hyperlink(href, text, template=template)
|
||||
|
||||
def _build_insert_tag_button_menu(self):
|
||||
m = self.insert_tag_menu
|
||||
|
@ -671,8 +671,22 @@ class InsertLink(Dialog):
|
||||
t.setText(self.initial_text or '')
|
||||
t.setPlaceholderText(_('The (optional) text for the link'))
|
||||
|
||||
self.template_edit = t = QLineEdit(self)
|
||||
tl.addRow(_('Tem&plate:'), t)
|
||||
from calibre.gui2.tweak_book.editor.smarts.html import DEFAULT_LINK_TEMPLATE
|
||||
t.setText(tprefs.get('insert-hyperlink-template', None) or DEFAULT_LINK_TEMPLATE)
|
||||
|
||||
l.addWidget(self.bb)
|
||||
|
||||
def accept(self):
|
||||
from calibre.gui2.tweak_book.editor.smarts.html import DEFAULT_LINK_TEMPLATE
|
||||
t = self.template
|
||||
if t:
|
||||
if t == DEFAULT_LINK_TEMPLATE:
|
||||
t = None
|
||||
tprefs.set('insert-hyperlink-template', self.template)
|
||||
return Dialog.accept(self)
|
||||
|
||||
def selected_file_changed(self, *args):
|
||||
rows = list(self.file_names.selectionModel().selectedRows())
|
||||
if not rows:
|
||||
@ -722,6 +736,10 @@ class InsertLink(Dialog):
|
||||
def text(self):
|
||||
return unicode(self.text_edit.text()).strip()
|
||||
|
||||
@property
|
||||
def template(self):
|
||||
return self.template_edit.text().strip() or None
|
||||
|
||||
@classmethod
|
||||
def test(cls):
|
||||
import sys
|
||||
|
Loading…
x
Reference in New Issue
Block a user