diff --git a/src/calibre/customize/builtins.py b/src/calibre/customize/builtins.py index 70a08590f5..25ffe32d87 100644 --- a/src/calibre/customize/builtins.py +++ b/src/calibre/customize/builtins.py @@ -856,7 +856,7 @@ class TemplateFunctions(PreferencesPlugin): category_order = 5 name_order = 4 config_widget = 'calibre.gui2.preferences.template_functions' - description = _('Define and explore template functions') + description = _('Create your own template functions') class Email(PreferencesPlugin): name = 'Email' diff --git a/src/calibre/customize/profiles.py b/src/calibre/customize/profiles.py index 0c27069df3..763460d2ef 100644 --- a/src/calibre/customize/profiles.py +++ b/src/calibre/customize/profiles.py @@ -441,7 +441,7 @@ class TabletOutput(iPadOutput): class SamsungGalaxy(TabletOutput): name = 'Samsung Galaxy' - shortname = 'galaxy' + short_name = 'galaxy' description = _('Intended for the Samsung Galaxy and similar tablet devices with ' 'a resolution of 600x1280') screen_size = comic_screen_size = (600, 1280) diff --git a/src/calibre/gui2/preferences/template_functions.py b/src/calibre/gui2/preferences/template_functions.py index d049f7dae4..8416c5a581 100644 --- a/src/calibre/gui2/preferences/template_functions.py +++ b/src/calibre/gui2/preferences/template_functions.py @@ -10,7 +10,6 @@ import traceback from calibre.gui2 import error_dialog from calibre.gui2.preferences import ConfigWidgetBase, test_widget from calibre.gui2.preferences.template_functions_ui import Ui_Form -from calibre.utils.config import prefs from calibre.utils.formatter_functions import formatter_functions, compile_user_function @@ -20,6 +19,44 @@ class ConfigWidget(ConfigWidgetBase, Ui_Form): self.gui = gui self.db = gui.library_view.model().db self.current_plugboards = self.db.prefs.get('plugboards',{}) + help_text = _(''' +

Here you can add and remove functions used in template processing. A + template function is written in python. It takes information from the + book, processes it in some way, then returns a string result. Functions + defined here are usable in templates in the same way that builtin + functions are usable. The function must be named evaluate, and must + have the signature shown below.

+

evaluate(self, formatter, kwargs, mi, locals, your_arguments) + → returning a unicode string

+

The arguments to evaluate are: +

+

+ The following example function looks for various values in the tags + metadata field, returning those values that appear in tags. +

+        def evaluate(self, formatter, kwargs, mi, locals, val):
+            awards=['allbooks', 'PBook', 'ggff']
+            return ', '.join([t for t in kwargs.get('tags') if t in awards])
+        
+

+ ''') + self.textBrowser.setHtml(help_text) def initialize(self): self.funcs = formatter_functions.get_functions() diff --git a/src/calibre/gui2/preferences/template_functions.ui b/src/calibre/gui2/preferences/template_functions.ui index 2662d50dd7..d323a8dc7e 100644 --- a/src/calibre/gui2/preferences/template_functions.ui +++ b/src/calibre/gui2/preferences/template_functions.ui @@ -14,48 +14,7 @@ Form - - - - - 0 - 0 - - - - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">Here you can add and remove functions used in template processing. A template function is writen in python. It takes information from the book, processes it in some way, then returns a string result. Functions defined here are usable in templates in the same way that builtin functions are usable. The function must be named </span><span style=" font-size:8pt; font-weight:600;">evaluate</span><span style=" font-size:8pt;">, and must have the signature shown below. </span></p> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt; font-weight:600;">evaluate(self, formatter, kwargs, mi, locals, your_arguments) </span><span style=" font-size:8pt;">returning a</span><span style=" font-size:8pt; font-weight:600;"> unicode </span><span style=" font-size:8pt;">string</span></p> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">The arguments to evaluate are:</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">- formatter: the instance of the formatter being used to evaluate the current template. You can use this to do recursive template evaluation.</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">- kwargs: a dictionary of metadata. Field values are in this dictionary.</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">- mi: a Metadata instance. Used to get field information. This parameter can be None in some cases, such as when evaluating non-book templates.</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">- locals: the local variables assigned to by the current template program.</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">- Your_arguments must be one or more parameter (number matching the arg count box), or the value *args for a variable number of arguments. These are values passed into the function. One argument is required, and is usually the value of the field being operated upon. Note that when writing in basic template mode, the user does not provide this first argument. Instead it is the value of the field the function is operating upon.</span></p> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">The following example function looks for various values in the tags metadata field, returning those values that appear in tags.</span></p> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">def evaluate(self, formatter, kwargs, mi, locals, val):</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;"> awards=['allbooks', 'PBook', 'ggff']</span></p> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;"> return ', '.join([t for t in kwargs.get('tags') if t in awards])</span></p></body></html> - - - Qt::RichText - - - Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop - - - true - - - - + Qt::Horizontal @@ -69,7 +28,10 @@ p, li { white-space: pre-wrap; } - Function: + &Function: + + + function_name @@ -86,29 +48,20 @@ p, li { white-space: pre-wrap; } - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> -<p style="-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:8pt;"></p></body></html> + - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">Argument count:</span></p></body></html> + Arg &count: + + + argument_count - <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"> -<html><head><meta name="qrichtext" content="1" /><style type="text/css"> -p, li { white-space: pre-wrap; } -</style></head><body style=" font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;"> -<p style=" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;"><span style=" font-size:8pt;">Set this to -1 if the function takes a variable number of arguments</span></p></body></html> + Set this to -1 if the function takes a variable number of arguments -1 @@ -121,11 +74,14 @@ p, li { white-space: pre-wrap; } - Documentation: + &Documentation: Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop + + documentation + @@ -133,21 +89,21 @@ p, li { white-space: pre-wrap; } - Clear + &Clear - Delete + &Delete - Create + C&reate @@ -160,7 +116,10 @@ p, li { white-space: pre-wrap; } - Program Code: (be sure to follow python indenting rules) + &Program Code: (be sure to follow python indenting rules) + + + program @@ -184,6 +143,10 @@ p, li { white-space: pre-wrap; } + + + + diff --git a/src/calibre/utils/formatter_functions.py b/src/calibre/utils/formatter_functions.py index ae95b9ad07..a17903004f 100644 --- a/src/calibre/utils/formatter_functions.py +++ b/src/calibre/utils/formatter_functions.py @@ -467,4 +467,4 @@ def load_user_template_functions(funcs): cls = compile_user_function(*func) formatter_functions.register_function(cls) except: - traceback.print_exc() \ No newline at end of file + traceback.print_exc()