mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Enhancement: allow template functions add() and multiply() to take a variable number of arguments.
This commit is contained in:
parent
e3cde00f98
commit
b7c4fcd711
@ -346,7 +346,7 @@ parameters can be statements (sequences of expressions). Note that the definitiv
|
||||
|
||||
* ``and(value, value, ...)`` -- returns the string "1" if all values are not empty, otherwise returns the empty string. This function works
|
||||
well with test or first_non_empty. You can have as many values as you want.
|
||||
* ``add(x, y)`` -- returns x + y. Throws an exception if either x or y are not numbers.
|
||||
* ``add(x, y, ...)`` -- returns the sum of its arguments. Throws an exception if an argument is not a number.
|
||||
* ``assign(id, val)`` -- assigns val to id, then returns val. id must be an identifier, not an expression
|
||||
* ``approximate_formats()`` -- return a comma-separated list of formats that at one point were associated with the book. There is no
|
||||
guarantee that the list is correct, although it probably is. This function can be called in Template Program Mode using the template
|
||||
@ -480,7 +480,7 @@ parameters can be statements (sequences of expressions). Note that the definitiv
|
||||
``separator``, as are the items in the returned list.
|
||||
* ``mod(x)`` -- returns the remainder of ``x / y``, where ``x``, ``y``, and the result are integers. Throws an exception if either ``x`` or
|
||||
``y`` is not a number.
|
||||
* ``multiply(x, y)`` -- returns x * y. Throws an exception if either x or y are not numbers.
|
||||
* ``multiply(x, y, ...)`` -- returns the product of its arguments. Throws an exception if any argument is not a number.
|
||||
* ``ondevice()`` -- return the string "Yes" if ``ondevice`` is set, otherwise return the empty string
|
||||
* ``or(value, value, ...)`` -- returns the string ``"1"`` if any value is not empty, otherwise returns the empty string. This function works
|
||||
well with test or `first_non_empty`. You can have as many values as you want.
|
||||
|
@ -241,14 +241,17 @@ class BuiltinStrlen(BuiltinFormatterFunction):
|
||||
|
||||
class BuiltinAdd(BuiltinFormatterFunction):
|
||||
name = 'add'
|
||||
arg_count = 2
|
||||
arg_count = -1
|
||||
category = 'Arithmetic'
|
||||
__doc__ = doc = _('add(x, y) -- returns x + y. Throws an exception if either x or y are not numbers.')
|
||||
__doc__ = doc = _('add(x, y, ...) -- returns the sum of its arguments. '
|
||||
'Throws an exception if an argument is not a number.')
|
||||
|
||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||
x = float(x if x and x != 'None' else 0)
|
||||
y = float(y if y and y != 'None' else 0)
|
||||
return unicode_type(x + y)
|
||||
def evaluate(self, formatter, kwargs, mi, locals, *args):
|
||||
res = 0
|
||||
for v in args:
|
||||
v = float(v if v and v != 'None' else 0)
|
||||
res += v
|
||||
return unicode_type(res)
|
||||
|
||||
|
||||
class BuiltinSubtract(BuiltinFormatterFunction):
|
||||
@ -265,14 +268,17 @@ class BuiltinSubtract(BuiltinFormatterFunction):
|
||||
|
||||
class BuiltinMultiply(BuiltinFormatterFunction):
|
||||
name = 'multiply'
|
||||
arg_count = 2
|
||||
arg_count = -1
|
||||
category = 'Arithmetic'
|
||||
__doc__ = doc = _('multiply(x, y) -- returns x * y. Throws an exception if either x or y are not numbers.')
|
||||
__doc__ = doc = _('multiply(x, y, ...) -- returns the product of its arguments. '
|
||||
'Throws an exception if any argument is not a number.')
|
||||
|
||||
def evaluate(self, formatter, kwargs, mi, locals, x, y):
|
||||
x = float(x if x and x != 'None' else 0)
|
||||
y = float(y if y and y != 'None' else 0)
|
||||
return unicode_type(x * y)
|
||||
def evaluate(self, formatter, kwargs, mi, locals, *args):
|
||||
res = 1
|
||||
for v in args:
|
||||
v = float(v if v and v != 'None' else 0)
|
||||
res *= v
|
||||
return unicode_type(res)
|
||||
|
||||
|
||||
class BuiltinDivide(BuiltinFormatterFunction):
|
||||
|
Loading…
x
Reference in New Issue
Block a user