comparison genshi/template/interpolation.py @ 902:09cc3627654c experimental-inline

Sync `experimental/inline` branch with [source:trunk@1126].
author cmlenz
date Fri, 23 Apr 2010 21:08:26 +0000
parents 1837f39efd6f
children
comparison
equal deleted inserted replaced
830:de82830f8816 902:09cc3627654c
1 # -*- coding: utf-8 -*- 1 # -*- coding: utf-8 -*-
2 # 2 #
3 # Copyright (C) 2007-2008 Edgewall Software 3 # Copyright (C) 2007-2009 Edgewall Software
4 # All rights reserved. 4 # All rights reserved.
5 # 5 #
6 # This software is licensed as described in the file COPYING, which 6 # This software is licensed as described in the file COPYING, which
7 # you should have received as part of this distribution. The terms 7 # you should have received as part of this distribution. The terms
8 # are also available at http://genshi.edgewall.org/wiki/License. 8 # are also available at http://genshi.edgewall.org/wiki/License.
34 token_re = re.compile('%s|%s(?s)' % ( 34 token_re = re.compile('%s|%s(?s)' % (
35 r'[uU]?[rR]?("""|\'\'\')((?<!\\)\\\1|.)*?\1', 35 r'[uU]?[rR]?("""|\'\'\')((?<!\\)\\\1|.)*?\1',
36 PseudoToken 36 PseudoToken
37 )) 37 ))
38 38
39
39 def interpolate(text, filepath=None, lineno=-1, offset=0, lookup='strict'): 40 def interpolate(text, filepath=None, lineno=-1, offset=0, lookup='strict'):
40 """Parse the given string and extract expressions. 41 """Parse the given string and extract expressions.
41 42
42 This function is a generator that yields `TEXT` events for literal strings, 43 This function is a generator that yields `TEXT` events for literal strings,
43 and `EXPR` events for expressions, depending on the results of parsing the 44 and `EXPR` events for expressions, depending on the results of parsing the
44 string. 45 string.
45 46
46 >>> for kind, data, pos in interpolate("hey ${foo}bar"): 47 >>> for kind, data, pos in interpolate("hey ${foo}bar"):
47 ... print kind, repr(data) 48 ... print('%s %r' % (kind, data))
48 TEXT u'hey ' 49 TEXT 'hey '
49 EXPR Expression('foo') 50 EXPR Expression('foo')
50 TEXT u'bar' 51 TEXT 'bar'
51 52
52 :param text: the text to parse 53 :param text: the text to parse
53 :param filepath: absolute path to the file in which the text was found 54 :param filepath: absolute path to the file in which the text was found
54 (optional) 55 (optional)
55 :param lineno: the line number at which the text was found (optional) 56 :param lineno: the line number at which the text was found (optional)
66 textbuf = [] 67 textbuf = []
67 textpos = None 68 textpos = None
68 for is_expr, chunk in chain(lex(text, pos, filepath), [(True, '')]): 69 for is_expr, chunk in chain(lex(text, pos, filepath), [(True, '')]):
69 if is_expr: 70 if is_expr:
70 if textbuf: 71 if textbuf:
71 yield TEXT, u''.join(textbuf), textpos 72 yield TEXT, ''.join(textbuf), textpos
72 del textbuf[:] 73 del textbuf[:]
73 textpos = None 74 textpos = None
74 if chunk: 75 if chunk:
75 try: 76 try:
76 expr = Expression(chunk.strip(), pos[0], pos[1], 77 expr = Expression(chunk.strip(), pos[0], pos[1],
88 lines = chunk.splitlines() 89 lines = chunk.splitlines()
89 pos[1] += len(lines) - 1 90 pos[1] += len(lines) - 1
90 pos[2] += len(lines[-1]) 91 pos[2] += len(lines[-1])
91 else: 92 else:
92 pos[2] += len(chunk) 93 pos[2] += len(chunk)
94
93 95
94 def lex(text, textpos, filepath): 96 def lex(text, textpos, filepath):
95 offset = pos = 0 97 offset = pos = 0
96 end = len(text) 98 end = len(text)
97 escaped = False 99 escaped = False
Copyright (C) 2012-2017 Edgewall Software