Merge from trunk

This commit is contained in:
Charles Haley 2011-01-13 18:24:08 +00:00
commit 3a4ab7c3ca
5 changed files with 67 additions and 67 deletions

View File

@ -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'

View File

@ -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)

View File

@ -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 = _('''
<p>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.</p>
<p><code>evaluate(self, formatter, kwargs, mi, locals, your_arguments)
&rarr; returning a unicode string</code></p>
<p>The arguments to evaluate are:
<ul>
<li><b>formatter:</b> the instance of the formatter being used to
evaluate the current template. You can use this to do recursive
template evaluation.</li>
<li><b>kwargs:</b> a dictionary of metadata. Field values are in this
dictionary. mi: a Metadata instance. Used to get field information.
This parameter can be None in some cases, such as when evaluating
non-book templates.</li>
<li><b>locals:</b> the local variables assigned to by the current
template program. 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.</li>
</ul></p>
<p>
The following example function looks for various values in the tags
metadata field, returning those values that appear in tags.
<pre>
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])
</pre>
</p>
''')
self.textBrowser.setHtml(help_text)
def initialize(self):
self.funcs = formatter_functions.get_functions()

View File

@ -14,48 +14,7 @@
<string>Form</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<widget class="QLabel" name="label">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Maximum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;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 &lt;/span&gt;&lt;span style=&quot; font-size:8pt; font-weight:600;&quot;&gt;evaluate&lt;/span&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;, and must have the signature shown below. &lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-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;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt; font-weight:600;&quot;&gt;evaluate(self, formatter, kwargs, mi, locals, your_arguments) &lt;/span&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;returning a&lt;/span&gt;&lt;span style=&quot; font-size:8pt; font-weight:600;&quot;&gt; unicode &lt;/span&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;string&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-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;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;The arguments to evaluate are:&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;- formatter: the instance of the formatter being used to evaluate the current template. You can use this to do recursive template evaluation.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;- kwargs: a dictionary of metadata. Field values are in this dictionary.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;- mi: a Metadata instance. Used to get field information. This parameter can be None in some cases, such as when evaluating non-book templates.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;- locals: the local variables assigned to by the current template program.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;- 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.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-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;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;The following example function looks for various values in the tags metadata field, returning those values that appear in tags.&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot;-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;&quot;&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;def evaluate(self, formatter, kwargs, mi, locals, val):&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; awards=['allbooks', 'PBook', 'ggff']&lt;/span&gt;&lt;/p&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt; return ', '.join([t for t in kwargs.get('tags') if t in awards])&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="textFormat">
<enum>Qt::RichText</enum>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="wordWrap">
<bool>true</bool>
</property>
</widget>
</item>
<item row="1" column="0" colspan="3">
<item row="1" column="0" colspan="2">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
@ -69,7 +28,10 @@ p, li { white-space: pre-wrap; }
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Function:</string>
<string>&amp;Function:</string>
</property>
<property name="buddy">
<cstring>function_name</cstring>
</property>
</widget>
</item>
@ -86,29 +48,20 @@ p, li { white-space: pre-wrap; }
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="toolTip">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot;-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;&quot;&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string></string>
</property>
<property name="text">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;Argument count:&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>Arg &amp;count:</string>
</property>
<property name="buddy">
<cstring>argument_count</cstring>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QSpinBox" name="argument_count">
<property name="toolTip">
<string>&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0//EN&quot; &quot;http://www.w3.org/TR/REC-html40/strict.dtd&quot;&gt;
&lt;html&gt;&lt;head&gt;&lt;meta name=&quot;qrichtext&quot; content=&quot;1&quot; /&gt;&lt;style type=&quot;text/css&quot;&gt;
p, li { white-space: pre-wrap; }
&lt;/style&gt;&lt;/head&gt;&lt;body style=&quot; font-family:'MS Shell Dlg 2'; font-size:8.25pt; font-weight:400; font-style:normal;&quot;&gt;
&lt;p style=&quot; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;&quot;&gt;&lt;span style=&quot; font-size:8pt;&quot;&gt;Set this to -1 if the function takes a variable number of arguments&lt;/span&gt;&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
<string>Set this to -1 if the function takes a variable number of arguments</string>
</property>
<property name="minimum">
<number>-1</number>
@ -121,11 +74,14 @@ p, li { white-space: pre-wrap; }
<item row="2" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Documentation:</string>
<string>&amp;Documentation:</string>
</property>
<property name="alignment">
<set>Qt::AlignLeading|Qt::AlignLeft|Qt::AlignTop</set>
</property>
<property name="buddy">
<cstring>documentation</cstring>
</property>
</widget>
</item>
<item row="3" column="1">
@ -133,21 +89,21 @@ p, li { white-space: pre-wrap; }
<item>
<widget class="QPushButton" name="clear_button">
<property name="text">
<string>Clear</string>
<string>&amp;Clear</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="delete_button">
<property name="text">
<string>Delete</string>
<string>&amp;Delete</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="create_button">
<property name="text">
<string>Create</string>
<string>C&amp;reate</string>
</property>
</widget>
</item>
@ -160,7 +116,10 @@ p, li { white-space: pre-wrap; }
<item>
<widget class="QLabel" name="label_4">
<property name="text">
<string>Program Code: (be sure to follow python indenting rules)</string>
<string>&amp;Program Code: (be sure to follow python indenting rules)</string>
</property>
<property name="buddy">
<cstring>program</cstring>
</property>
</widget>
</item>
@ -184,6 +143,10 @@ p, li { white-space: pre-wrap; }
</item>
</layout>
</item>
<item row="0" column="0">
<widget class="QTextBrowser" name="textBrowser">
</widget>
</item>
</layout>
</widget>
<resources/>