diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py
index 24e9305027..0dcb09d8b9 100644
--- a/src/calibre/gui2/tweak_book/boss.py
+++ b/src/calibre/gui2/tweak_book/boss.py
@@ -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:
diff --git a/src/calibre/gui2/tweak_book/editor/smarts/html.py b/src/calibre/gui2/tweak_book/editor/smarts/html.py
index 854774587a..1f32354002 100644
--- a/src/calibre/gui2/tweak_book/editor/smarts/html.py
+++ b/src/calibre/gui2/tweak_book/editor/smarts/html.py
@@ -26,6 +26,7 @@ from calibre.utils.icu import utf16_length
get_offset = itemgetter(0)
PARAGRAPH_SEPARATOR = '\u2029'
+DEFAULT_LINK_TEMPLATE = '_TEXT_'
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('' % prepare_string_for_xml(target, True))
- p = c.position()
- c.insertText('')
+ 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):
diff --git a/src/calibre/gui2/tweak_book/editor/text.py b/src/calibre/gui2/tweak_book/editor/text.py
index 084dbbbb49..e4b6deb9bd 100644
--- a/src/calibre/gui2/tweak_book/editor/text.py
+++ b/src/calibre/gui2/tweak_book/editor/text.py
@@ -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'):
diff --git a/src/calibre/gui2/tweak_book/editor/widget.py b/src/calibre/gui2/tweak_book/editor/widget.py
index 3db90cebbb..4f5827b632 100644
--- a/src/calibre/gui2/tweak_book/editor/widget.py
+++ b/src/calibre/gui2/tweak_book/editor/widget.py
@@ -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
diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py
index bfc7c11bec..e81f70c394 100644
--- a/src/calibre/gui2/tweak_book/widgets.py
+++ b/src/calibre/gui2/tweak_book/widgets.py
@@ -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