Mercurial > public > mercurial-scm > hg
comparison mercurial/templater.py @ 25781:82c918509ef5
templater: extract function that parses template string
It will be called recursively to parse nested template strings.
author | Yuya Nishihara <yuya@tcha.org> |
---|---|
date | Mon, 15 Jun 2015 22:55:34 +0900 |
parents | 8b900b937e1c |
children | babd2c93bd99 |
comparison
equal
deleted
inserted
replaced
25780:8b900b937e1c | 25781:82c918509ef5 |
---|---|
125 else: | 125 else: |
126 raise error.ParseError(_("syntax error"), pos) | 126 raise error.ParseError(_("syntax error"), pos) |
127 pos += 1 | 127 pos += 1 |
128 yield ('end', None, pos) | 128 yield ('end', None, pos) |
129 | 129 |
130 def compiletemplate(tmpl, context): | 130 def _parsetemplate(tmpl, start, stop): |
131 parsed = [] | 131 parsed = [] |
132 pos, stop = 0, len(tmpl) | 132 pos = start |
133 p = parser.parser(elements) | 133 p = parser.parser(elements) |
134 while pos < stop: | 134 while pos < stop: |
135 n = tmpl.find('{', pos, stop) | 135 n = tmpl.find('{', pos, stop) |
136 if n < 0: | 136 if n < 0: |
137 parsed.append(('string', tmpl[pos:stop])) | 137 parsed.append(('string', tmpl[pos:stop])) |
146 if n > pos: | 146 if n > pos: |
147 parsed.append(('string', tmpl[pos:n])) | 147 parsed.append(('string', tmpl[pos:n])) |
148 | 148 |
149 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop)) | 149 parseres, pos = p.parse(tokenize(tmpl, n + 1, stop)) |
150 parsed.append(parseres) | 150 parsed.append(parseres) |
151 | 151 return parsed, pos |
152 | |
153 def compiletemplate(tmpl, context): | |
154 parsed, pos = _parsetemplate(tmpl, 0, len(tmpl)) | |
152 return [compileexp(e, context, methods) for e in parsed] | 155 return [compileexp(e, context, methods) for e in parsed] |
153 | 156 |
154 def compileexp(exp, context, curmethods): | 157 def compileexp(exp, context, curmethods): |
155 t = exp[0] | 158 t = exp[0] |
156 if t in curmethods: | 159 if t in curmethods: |