diff --git a/src/calibre/gui2/tweak_book/boss.py b/src/calibre/gui2/tweak_book/boss.py index 6651552ad5..40ab7a165d 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, template=d.template) + ed.insert_hyperlink(d.href, d.text, template=d.rendered_template) elif action[0] == 'insert_tag': d = InsertTag(parent=self.gui) if d.exec_() == d.Accepted: diff --git a/src/calibre/gui2/tweak_book/widgets.py b/src/calibre/gui2/tweak_book/widgets.py index eab1c9091c..335e4413a1 100644 --- a/src/calibre/gui2/tweak_book/widgets.py +++ b/src/calibre/gui2/tweak_book/widgets.py @@ -675,6 +675,13 @@ class InsertLink(Dialog): 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) + t.setToolTip('
' + _(''' + The template to use for generating the link. In addition to {0} and {1} + you can also use {2}, {3} and {4} variables + in the template, they will be replaced by the source filename, the destination + filename and the anchor, respectively. + ''').format( + '_TITLE_', '_TARGET', '_SOURCE_FILENAME_', '_DEST_FILENAME_', '_ANCHOR_')) l.addWidget(self.bb) @@ -740,6 +747,22 @@ class InsertLink(Dialog): def template(self): return self.template_edit.text().strip() or None + @property + def rendered_template(self): + ans = self.template + if ans: + target = self.href + frag = target.partition('#')[-1] + if target.startswith('#'): + target = '' + else: + target = target.split('#', 1)[0] + target = self.container.href_to_name(target) + ans = ans.replace('_SOURCE_FILENAME_', self.source_name) + ans = ans.replace('_DEST_FILENAME_', target) + ans = ans.replace('_ANCHOR_', frag) + return ans + @classmethod def test(cls): import sys