Patchwork [2,of,7] templater: extract function that parses template string

login
register
mail settings
Submitter Yuya Nishihara
Date July 12, 2015, 2:54 p.m.
Message ID <6c48facea2ad500110e0.1436712897@mimosa>
Download mbox | patch
Permalink /patch/9954/
State Accepted
Headers show

Comments

Yuya Nishihara - July 12, 2015, 2:54 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1434376534 -32400
#      Mon Jun 15 22:55:34 2015 +0900
# Node ID 6c48facea2ad500110e07700991dbad126156aca
# Parent  0bdccc00e5aaba80b379ad6eda4917958f9f30e3
templater: extract function that parses template string

It will be called recursively to parse nested template strings.

Patch

diff --git a/mercurial/templater.py b/mercurial/templater.py
--- a/mercurial/templater.py
+++ b/mercurial/templater.py
@@ -127,9 +127,9 @@  def tokenize(program, start, end):
         pos += 1
     yield ('end', None, pos)
 
-def compiletemplate(tmpl, context):
+def _parsetemplate(tmpl, start, stop):
     parsed = []
-    pos, stop = 0, len(tmpl)
+    pos = start
     p = parser.parser(elements)
     while pos < stop:
         n = tmpl.find('{', pos, stop)
@@ -148,7 +148,10 @@  def compiletemplate(tmpl, context):
 
         parseres, pos = p.parse(tokenize(tmpl, n + 1, stop))
         parsed.append(parseres)
+    return parsed, pos
 
+def compiletemplate(tmpl, context):
+    parsed, pos = _parsetemplate(tmpl, 0, len(tmpl))
     return [compileexp(e, context, methods) for e in parsed]
 
 def compileexp(exp, context, curmethods):