diff mercurial/templater.py @ 28912:867d6ba2353d

templater: add parsing and expansion rules to process "templatealias" section The debugtemplate command is updated to show expanded tree, but still the template engine doesn't support alias expansion. That's why the test says "parse error" for now.
author Yuya Nishihara <yuya@tcha.org>
date Sun, 27 Mar 2016 20:31:56 +0900
parents 35da19348143
children f59e24002678
line wrap: on
line diff
--- a/mercurial/templater.py	Sun Mar 27 20:29:03 2016 +0900
+++ b/mercurial/templater.py	Sun Mar 27 20:31:56 2016 +0900
@@ -872,6 +872,25 @@
 methods = exprmethods.copy()
 methods["integer"] = exprmethods["symbol"]  # '{1}' as variable
 
+class _aliasrules(parser.basealiasrules):
+    """Parsing and expansion rule set of template aliases"""
+    _section = _('template alias')
+    _parse = staticmethod(_parseexpr)
+
+    @staticmethod
+    def _trygetfunc(tree):
+        """Return (name, args) if tree is func(...) or ...|filter; otherwise
+        None"""
+        if tree[0] == 'func' and tree[1][0] == 'symbol':
+            return tree[1][1], getlist(tree[2])
+        if tree[0] == '|' and tree[2][0] == 'symbol':
+            return tree[2][1], [tree[1]]
+
+def expandaliases(tree, aliases):
+    """Return new tree of aliases are expanded"""
+    aliasmap = _aliasrules.buildmap(aliases)
+    return _aliasrules.expand(aliasmap, tree)
+
 # template engine
 
 stringify = templatefilters.stringify