registrar: add templatefilter to mark a function as template filter (API)
authorFUJIWARA Katsunori <foozy@lares.dti.ne.jp>
Wed, 30 Mar 2016 02:10:44 +0900
changeset 28692 6b3b958daf03
parent 28691 a04baf9c063b
child 28693 11f623b5668f
registrar: add templatefilter to mark a function as template filter (API) This patch also adds loadfilter() to templatefilters, because this combination helps to figure out how they cooperate with each other. Listing up loadfilter() in dispatch.extraloaders causes implicit loading template filter functions at loading (3rd party) extension. This change requires that "templatefilter" attribute of (3rd party) extension is registrar.templatefilter or so.
mercurial/dispatch.py
mercurial/registrar.py
mercurial/templatefilters.py
--- a/mercurial/dispatch.py	Sun Feb 14 20:43:30 2016 +0900
+++ b/mercurial/dispatch.py	Wed Mar 30 02:10:44 2016 +0900
@@ -35,6 +35,7 @@
     hg,
     hook,
     revset,
+    templatefilters,
     templatekw,
     ui as uimod,
     util,
@@ -766,6 +767,7 @@
     ('cmdtable', commands, 'loadcmdtable'),
     ('filesetpredicate', fileset, 'loadpredicate'),
     ('revsetpredicate', revset, 'loadpredicate'),
+    ('templatefilter', templatefilters, 'loadfilter'),
     ('templatekeyword', templatekw, 'loadkeyword'),
 ]
 
--- a/mercurial/registrar.py	Sun Feb 14 20:43:30 2016 +0900
+++ b/mercurial/registrar.py	Wed Mar 30 02:10:44 2016 +0900
@@ -191,3 +191,28 @@
 
     Otherwise, explicit 'templatekw.loadkeyword()' is needed.
     """
+
+class templatefilter(_templateregistrarbase):
+    """Decorator to register template filer
+
+    Usage::
+
+        templatefilter = registrar.templatefilter()
+
+        @templatefilter('myfilter')
+        def myfilterfunc(text):
+            '''Explanation of this template filter ....
+            '''
+            pass
+
+    The first string argument is used also in online help.
+
+    'templatefilter' instance in example above can be used to
+    decorate multiple functions.
+
+    Decorated functions are registered automatically at loading
+    extension, if an instance named as 'templatefilter' is used for
+    decorating in extension.
+
+    Otherwise, explicit 'templatefilters.loadkeyword()' is needed.
+    """
--- a/mercurial/templatefilters.py	Sun Feb 14 20:43:30 2016 +0900
+++ b/mercurial/templatefilters.py	Wed Mar 30 02:10:44 2016 +0900
@@ -415,5 +415,11 @@
             text = regexp.sub(format, text)
     return text
 
+def loadfilter(ui, extname, registrarobj):
+    """Load template filter from specified registrarobj
+    """
+    for name, func in registrarobj._table.iteritems():
+        filters[name] = func
+
 # tell hggettext to extract docstrings from these functions:
 i18nfunctions = filters.values()