mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-07 10:14:46 -04:00
Stored templates
This commit is contained in:
parent
e08fb5f231
commit
f839ac96e3
@ -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.
|
* ``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
|
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.
|
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``:
|
Examples of ``call``, assuming the stored template is named ``foo``:
|
||||||
|
|
||||||
|
@ -303,6 +303,8 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
from calibre.gui2.ui import get_gui
|
from calibre.gui2.ui import get_gui
|
||||||
self.mi.set_all_user_metadata(
|
self.mi.set_all_user_metadata(
|
||||||
get_gui().current_db.new_api.field_metadata.custom_field_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
|
# Remove help icon on title bar
|
||||||
icon = self.windowIcon()
|
icon = self.windowIcon()
|
||||||
@ -322,6 +324,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
if text is not None:
|
if text is not None:
|
||||||
if text_is_placeholder:
|
if text_is_placeholder:
|
||||||
self.textbox.setPlaceholderText(text)
|
self.textbox.setPlaceholderText(text)
|
||||||
|
self.textbox.clear()
|
||||||
else:
|
else:
|
||||||
self.textbox.setPlainText(text)
|
self.textbox.setPlainText(text)
|
||||||
self.buttonBox.button(QDialogButtonBox.Ok).setText(_('&OK'))
|
self.buttonBox.button(QDialogButtonBox.Ok).setText(_('&OK'))
|
||||||
@ -492,7 +495,7 @@ class TemplateDialog(QDialog, Ui_TemplateDialog):
|
|||||||
class EmbeddedTemplateDialog(TemplateDialog):
|
class EmbeddedTemplateDialog(TemplateDialog):
|
||||||
|
|
||||||
def __init__(self, parent):
|
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)
|
dialog_is_st_editor=True)
|
||||||
self.setParent(parent)
|
self.setParent(parent)
|
||||||
self.setWindowFlags(Qt.Widget)
|
self.setWindowFlags(Qt.Widget)
|
||||||
|
@ -80,14 +80,15 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
self.textBrowser.setHtml(help_text)
|
self.textBrowser.setHtml(help_text)
|
||||||
help_text = '<p>' + _('''
|
help_text = '<p>' + _('''
|
||||||
Here you can add and remove stored templates used in template processing.
|
Here you can add and remove stored templates used in template processing.
|
||||||
You use a stored template in another template with the 'call' template
|
You use a stored template in another template with the '{0}' template
|
||||||
function, as in 'call(somename, arguments...). Stored templates must use
|
function, as in '{0}(some_name, arguments...). Stored templates must use
|
||||||
General Program Mode -- they must begin with the text 'program:'.
|
General Program Mode -- they must begin with the text '{1}'.
|
||||||
In the stored template you get the arguments using the 'arguments()'
|
In the stored template you retrieve the arguments using the '{2}()'
|
||||||
template function, as in arguments(var1, var2, ...). The calling arguments
|
template function, as in '{2}(var1, var2, ...)'. The calling arguments
|
||||||
are copied to the named variables.
|
are copied to the named variables. See the template language tutorial
|
||||||
|
for more information.
|
||||||
''') + '</p>'
|
''') + '</p>'
|
||||||
self.st_textBrowser.setHtml(help_text)
|
self.st_textBrowser.setHtml(help_text.format('call', 'program:', 'arguments'))
|
||||||
self.st_textBrowser.adjustSize()
|
self.st_textBrowser.adjustSize()
|
||||||
|
|
||||||
def initialize(self):
|
def initialize(self):
|
||||||
@ -127,8 +128,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
self.st_build_function_names_box()
|
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.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.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_create_button.clicked.connect(self.st_create_button_clicked)
|
||||||
self.st_delete_button.clicked.connect(self.st_delete_button_clicked)
|
self.st_delete_button.clicked.connect(self.st_delete_button_clicked)
|
||||||
self.st_create_button.setEnabled(False)
|
self.st_create_button.setEnabled(False)
|
||||||
@ -264,9 +263,6 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
|
|
||||||
# Stored template tab
|
# 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):
|
def st_clear_button_clicked(self):
|
||||||
self.st_build_function_names_box()
|
self.st_build_function_names_box()
|
||||||
self.template_editor.textbox.clear()
|
self.template_editor.textbox.clear()
|
||||||
@ -323,8 +319,10 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
det_msg=traceback.format_exc())
|
det_msg=traceback.format_exc())
|
||||||
|
|
||||||
def st_template_name_edited(self, txt):
|
def st_template_name_edited(self, txt):
|
||||||
self.st_create_button.setEnabled(True)
|
b = txt in self.st_funcs
|
||||||
self.st_replace_button.setEnabled(False)
|
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)
|
self.template_editor.textbox.setReadOnly(False)
|
||||||
|
|
||||||
def st_function_index_changed(self, txt):
|
def st_function_index_changed(self, txt):
|
||||||
@ -337,9 +335,7 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form):
|
|||||||
func = self.st_funcs[txt]
|
func = self.st_funcs[txt]
|
||||||
self.template_editor.new_doc.setPlainText(func.doc)
|
self.template_editor.new_doc.setPlainText(func.doc)
|
||||||
self.template_editor.textbox.setPlainText(func.program_text)
|
self.template_editor.textbox.setPlainText(func.program_text)
|
||||||
self.st_delete_button.setEnabled(True)
|
self.st_template_name_edited(txt)
|
||||||
self.template_editor.textbox.setReadOnly(False)
|
|
||||||
self.st_replace_button.setEnabled(False)
|
|
||||||
|
|
||||||
def st_replace_button_clicked(self):
|
def st_replace_button_clicked(self):
|
||||||
name = unicode_type(self.template_editor.template_name.currentText())
|
name = unicode_type(self.template_editor.template_name.currentText())
|
||||||
|
Loading…
x
Reference in New Issue
Block a user