Edit book: Insert hyperlink: Add a few more variables for the link template: _SOURCE_FILENAME_, _DEST_FILENAME_ and _ANCHOR_

This commit is contained in:
Kovid Goyal 2019-01-04 10:38:09 +05:30
parent e190bdaf21
commit 8ef42dafc5
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 24 additions and 1 deletions

View File

@ -898,7 +898,7 @@ class Boss(QObject):
self.commit_all_editors_to_container() self.commit_all_editors_to_container()
d = InsertLink(current_container(), edname, initial_text=ed.get_smart_selection(), parent=self.gui) d = InsertLink(current_container(), edname, initial_text=ed.get_smart_selection(), parent=self.gui)
if d.exec_() == d.Accepted: 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': elif action[0] == 'insert_tag':
d = InsertTag(parent=self.gui) d = InsertTag(parent=self.gui)
if d.exec_() == d.Accepted: if d.exec_() == d.Accepted:

View File

@ -675,6 +675,13 @@ class InsertLink(Dialog):
tl.addRow(_('Tem&plate:'), t) tl.addRow(_('Tem&plate:'), t)
from calibre.gui2.tweak_book.editor.smarts.html import DEFAULT_LINK_TEMPLATE 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.setText(tprefs.get('insert-hyperlink-template', None) or DEFAULT_LINK_TEMPLATE)
t.setToolTip('<p>' + _('''
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) l.addWidget(self.bb)
@ -740,6 +747,22 @@ class InsertLink(Dialog):
def template(self): def template(self):
return self.template_edit.text().strip() or None 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 @classmethod
def test(cls): def test(cls):
import sys import sys