@@ -37,6 +37,7 @@ from . import (
revset,
templatefilters,
templatekw,
+ templater,
ui as uimod,
util,
)
@@ -768,6 +769,7 @@ extraloaders = [
('filesetpredicate', fileset, 'loadpredicate'),
('revsetpredicate', revset, 'loadpredicate'),
('templatefilter', templatefilters, 'loadfilter'),
+ ('templatefunc', templater, 'loadfunction'),
('templatekeyword', templatekw, 'loadkeyword'),
]
@@ -216,3 +216,29 @@ class templatefilter(_templateregistrarb
Otherwise, explicit 'templatefilters.loadkeyword()' is needed.
"""
+
+class templatefunc(_templateregistrarbase):
+ """Decorator to register template function
+
+ Usage::
+
+ templatefunc = registrar.templatefunc()
+
+ @templatefunc('myfunc(arg1, arg2[, arg3])')
+ def myfuncfunc(context, mapping, args):
+ '''Explanation of this template function ....
+ '''
+ pass
+
+ The first string argument is used also in online help.
+
+ 'templatefunc' instance in example above can be used to
+ decorate multiple functions.
+
+ Decorated functions are registered automatically at loading
+ extension, if an instance named as 'templatefunc' is used for
+ decorating in extension.
+
+ Otherwise, explicit 'templater.loadfunction()' is needed.
+ """
+ _getname = _funcregistrarbase._parsefuncdecl
@@ -1069,5 +1069,11 @@ def stylemap(styles, paths=None):
raise RuntimeError("No hgweb templates found in %r" % paths)
+def loadfunction(ui, extname, registrarobj):
+ """Load template function from specified registrarobj
+ """
+ for name, func in registrarobj._table.iteritems():
+ funcs[name] = func
+
# tell hggettext to extract docstrings from these functions:
i18nfunctions = funcs.values()
@@ -3691,3 +3691,26 @@ utf8 filter:
[255]
$ cd ..
+
+Test that template function in extension is registered as expected
+
+ $ cd a
+
+ $ cat <<EOF > $TESTTMP/customfunc.py
+ > from mercurial import registrar
+ >
+ > templatefunc = registrar.templatefunc()
+ >
+ > @templatefunc('custom()')
+ > def custom(context, mapping, args):
+ > return 'custom'
+ > EOF
+ $ cat <<EOF > .hg/hgrc
+ > [extensions]
+ > customfunc = $TESTTMP/customfunc.py
+ > EOF
+
+ $ hg log -r . -T "{custom()}\n" --config customfunc.enabled=true
+ custom
+
+ $ cd ..