Stored templates

This commit is contained in:
Charles Haley 2020-10-01 09:41:18 +01:00
parent e08fb5f231
commit f839ac96e3
3 changed files with 18 additions and 19 deletions

View File

@ -405,7 +405,7 @@ Program mode also supports the classic relational (comparison) operators: ``==``
* ``program: if '11' > '2' then 'yes' else 'no' fi`` returns 'no' because it is doing a lexical comparison. If you want numeric comparison instead of lexical comparison, use the operators ``==#``, ``!=#``, ``<#``, ``<=#``, ``>#``, ``>=#``. In this case the left and right values are set to zero if they are undefined or the empty string. If they are not numbers then an error is raised.
General Program Mode support saving General Program Mode templates and calling those templates from another template. You save
templates using :guilabel:`Preferences->Advanced->Stored templates`. More information is provided in that dialog. You call
templates using :guilabel:`Preferences->Advanced->Template functions`. More information is provided in that dialog. You call
a template using the ``call`` keyword, passing positional arguments if desired. An argument can be any expression.
Examples of ``call``, assuming the stored template is named ``foo``:

View File

@ -303,6 +303,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
from calibre.gui2.ui import get_gui
self.mi.set_all_user_metadata(
get_gui().current_db.new_api.field_metadata.custom_field_metadata())
for col in self.mi.get_all_user_metadata(False):
self.mi.set(col, (col,), 0)
# Remove help icon on title bar
icon = self.windowIcon()
@ -322,6 +324,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
if text is not None:
if text_is_placeholder:
self.textbox.setPlaceholderText(text)
self.textbox.clear()
else:
self.textbox.setPlainText(text)
self.buttonBox.button(QDialogButtonBox.Ok).setText(_('&OK'))
@ -492,7 +495,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
class EmbeddedTemplateDialog(TemplateDialog):
def __init__(self, parent):
TemplateDialog.__init__(self, parent, "Foo", text_is_placeholder=True,
TemplateDialog.__init__(self, parent, _('A General Program Mode Template'), text_is_placeholder=True,
dialog_is_st_editor=True)
self.setParent(parent)
self.setWindowFlags(Qt.Widget)

View File

@ -80,14 +80,15 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.textBrowser.setHtml(help_text)
help_text = '<p>' + _('''
Here you can add and remove stored templates used in template processing.
You use a stored template in another template with the 'call' template
function, as in 'call(somename, arguments...). Stored templates must use
General Program Mode -- they must begin with the text 'program:'.
In the stored template you get the arguments using the 'arguments()'
template function, as in arguments(var1, var2, ...). The calling arguments
are copied to the named variables.
You use a stored template in another template with the '{0}' template
function, as in '{0}(some_name, arguments...). Stored templates must use
General Program Mode -- they must begin with the text '{1}'.
In the stored template you retrieve the arguments using the '{2}()'
template function, as in '{2}(var1, var2, ...)'. The calling arguments
are copied to the named variables. See the template language tutorial
for more information.
''') + '</p>'
self.st_textBrowser.setHtml(help_text)
self.st_textBrowser.setHtml(help_text.format('call', 'program:', 'arguments'))
self.st_textBrowser.adjustSize()
def initialize(self):
@ -127,8 +128,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
self.st_build_function_names_box()
self.template_editor.template_name.currentIndexChanged[native_string_type].connect(self.st_function_index_changed)
self.template_editor.template_name.editTextChanged.connect(self.st_template_name_edited)
self.template_editor.new_doc.textChanged.connect(self.st_enable_replace_button)
self.template_editor.textbox.textChanged.connect(self.st_enable_replace_button)
self.st_create_button.clicked.connect(self.st_create_button_clicked)
self.st_delete_button.clicked.connect(self.st_delete_button_clicked)
self.st_create_button.setEnabled(False)
@ -264,9 +263,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
# Stored template tab
def st_enable_replace_button(self):
self.st_replace_button.setEnabled(self.st_delete_button.isEnabled())
def st_clear_button_clicked(self):
self.st_build_function_names_box()
self.template_editor.textbox.clear()
@ -323,8 +319,10 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
det_msg=traceback.format_exc())
def st_template_name_edited(self, txt):
self.st_create_button.setEnabled(True)
self.st_replace_button.setEnabled(False)
b = txt in self.st_funcs
self.st_create_button.setEnabled(not b)
self.st_replace_button.setEnabled(b)
self.st_delete_button.setEnabled(b)
self.template_editor.textbox.setReadOnly(False)
def st_function_index_changed(self, txt):
@ -337,9 +335,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
func = self.st_funcs[txt]
self.template_editor.new_doc.setPlainText(func.doc)
self.template_editor.textbox.setPlainText(func.program_text)
self.st_delete_button.setEnabled(True)
self.template_editor.textbox.setReadOnly(False)
self.st_replace_button.setEnabled(False)
self.st_template_name_edited(txt)
def st_replace_button_clicked(self):
name = unicode_type(self.template_editor.template_name.currentText())