Mercurial > genshi > mirror
annotate genshi/template/interpolation.py @ 992:d81f8ba0e6ba stable-0.7.x
Merge r1213 from trunk (fix infinite loop in interpolation brace matching caused by a change in 2.7).
author | hodgestar |
---|---|
date | Sat, 26 Jan 2013 12:08:57 +0000 |
parents | f3d998cc941e |
children |
rev | line source |
---|---|
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
1 # -*- coding: utf-8 -*- |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
2 # |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2007-2009 Edgewall Software |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
4 # All rights reserved. |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
5 # |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
6 # This software is licensed as described in the file COPYING, which |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
7 # you should have received as part of this distribution. The terms |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
8 # are also available at http://genshi.edgewall.org/wiki/License. |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
9 # |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
10 # This software consists of voluntary contributions made by many |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
11 # individuals. For the exact contribution history, see the revision |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
12 # history and logs, available at http://genshi.edgewall.org/log/. |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
13 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
14 """String interpolation routines, i.e. the splitting up a given text into some |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
15 parts that are literal strings, and others that are Python expressions. |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
16 """ |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
17 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
18 from itertools import chain |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
19 import os |
732
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
20 import re |
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
21 from tokenize import PseudoToken |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
22 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
23 from genshi.core import TEXT |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
24 from genshi.template.base import TemplateSyntaxError, EXPR |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
25 from genshi.template.eval import Expression |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
26 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
27 __all__ = ['interpolate'] |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
28 __docformat__ = 'restructuredtext en' |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
29 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
30 NAMESTART = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_' |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
31 NAMECHARS = NAMESTART + '.0123456789' |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
32 PREFIX = '$' |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
33 |
732
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
34 token_re = re.compile('%s|%s(?s)' % ( |
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
35 r'[uU]?[rR]?("""|\'\'\')((?<!\\)\\\1|.)*?\1', |
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
36 PseudoToken |
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
37 )) |
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
38 |
859 | 39 |
714
fc6d9d2a3527
The `Template` class and its subclasses, as well as the interpolation API, now take an `filepath` parameter instead of `basedir`. Closes #207. Thanks to Waldemar Kornewald for the patch.
cmlenz
parents:
606
diff
changeset
|
40 def interpolate(text, filepath=None, lineno=-1, offset=0, lookup='strict'): |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
41 """Parse the given string and extract expressions. |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
42 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
43 This function is a generator that yields `TEXT` events for literal strings, |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
44 and `EXPR` events for expressions, depending on the results of parsing the |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
45 string. |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
46 |
442
97544725bb7f
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
425
diff
changeset
|
47 >>> for kind, data, pos in interpolate("hey ${foo}bar"): |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
793
diff
changeset
|
48 ... print('%s %r' % (kind, data)) |
859 | 49 TEXT 'hey ' |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
50 EXPR Expression('foo') |
859 | 51 TEXT 'bar' |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
52 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
53 :param text: the text to parse |
714
fc6d9d2a3527
The `Template` class and its subclasses, as well as the interpolation API, now take an `filepath` parameter instead of `basedir`. Closes #207. Thanks to Waldemar Kornewald for the patch.
cmlenz
parents:
606
diff
changeset
|
54 :param filepath: absolute path to the file in which the text was found |
fc6d9d2a3527
The `Template` class and its subclasses, as well as the interpolation API, now take an `filepath` parameter instead of `basedir`. Closes #207. Thanks to Waldemar Kornewald for the patch.
cmlenz
parents:
606
diff
changeset
|
55 (optional) |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
56 :param lineno: the line number at which the text was found (optional) |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
57 :param offset: the column number at which the text starts in the source |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
58 (optional) |
442
97544725bb7f
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
425
diff
changeset
|
59 :param lookup: the variable lookup mechanism; either "lenient" (the |
97544725bb7f
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
425
diff
changeset
|
60 default), "strict", or a custom lookup class |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
61 :return: a list of `TEXT` and `EXPR` events |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
62 :raise TemplateSyntaxError: when a syntax error in an expression is |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
422
diff
changeset
|
63 encountered |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
64 """ |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
65 pos = [filepath, lineno, offset] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
66 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
67 textbuf = [] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
68 textpos = None |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
407
diff
changeset
|
69 for is_expr, chunk in chain(lex(text, pos, filepath), [(True, '')]): |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
70 if is_expr: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
71 if textbuf: |
859 | 72 yield TEXT, ''.join(textbuf), textpos |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
73 del textbuf[:] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
74 textpos = None |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
75 if chunk: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
76 try: |
442
97544725bb7f
Back out [510] and instead implement configurable error handling modes. The default is the old 0.3.x behaviour, but more strict error handling is available as an option.
cmlenz
parents:
425
diff
changeset
|
77 expr = Expression(chunk.strip(), pos[0], pos[1], |
601
59fbd7586454
Simplify implementation of `py:with` directive by compiling to a `Suite`, instead of manually breaking up the statement and compiling each part to an `Expression`. Also, the first line of code in a `Suite` is now stored as the "function name" of the bytecode, so that it shows up in tracebacks.
cmlenz
parents:
544
diff
changeset
|
78 lookup=lookup) |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
79 yield EXPR, expr, tuple(pos) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
80 except SyntaxError, err: |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
407
diff
changeset
|
81 raise TemplateSyntaxError(err, filepath, pos[1], |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
82 pos[2] + (err.offset or 0)) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
83 else: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
84 textbuf.append(chunk) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
85 if textpos is None: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
86 textpos = tuple(pos) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
87 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
88 if '\n' in chunk: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
89 lines = chunk.splitlines() |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
90 pos[1] += len(lines) - 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
91 pos[2] += len(lines[-1]) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
92 else: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
93 pos[2] += len(chunk) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
94 |
859 | 95 |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
407
diff
changeset
|
96 def lex(text, textpos, filepath): |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
97 offset = pos = 0 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
98 end = len(text) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
99 escaped = False |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
100 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
101 while 1: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
102 if escaped: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
103 offset = text.find(PREFIX, offset + 2) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
104 escaped = False |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
105 else: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
106 offset = text.find(PREFIX, pos) |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
107 if offset < 0 or offset == end - 1: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
108 break |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
109 next = text[offset + 1] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
110 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
111 if next == '{': |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
112 if offset > pos: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
113 yield False, text[pos:offset] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
114 pos = offset + 2 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
115 level = 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
116 while level: |
732
8825ac5014b1
Fix expression interpolation for expression containing triple-quoted multiline strings. Thanks to Armin Ronacher for reporting the issue and providing a patch.
cmlenz
parents:
719
diff
changeset
|
117 match = token_re.match(text, pos) |
992
d81f8ba0e6ba
Merge r1213 from trunk (fix infinite loop in interpolation brace matching caused by a change in 2.7).
hodgestar
parents:
859
diff
changeset
|
118 if match is None or not match.group(): |
d81f8ba0e6ba
Merge r1213 from trunk (fix infinite loop in interpolation brace matching caused by a change in 2.7).
hodgestar
parents:
859
diff
changeset
|
119 # if there isn't a match or the match is the empty |
d81f8ba0e6ba
Merge r1213 from trunk (fix infinite loop in interpolation brace matching caused by a change in 2.7).
hodgestar
parents:
859
diff
changeset
|
120 # string, we're not going to match up braces ever |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
407
diff
changeset
|
121 raise TemplateSyntaxError('invalid syntax', filepath, |
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
407
diff
changeset
|
122 *textpos[1:]) |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
123 pos = match.end() |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
124 tstart, tend = match.regs[3] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
125 token = text[tstart:tend] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
126 if token == '{': |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
127 level += 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
128 elif token == '}': |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
129 level -= 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
130 yield True, text[offset + 2:pos - 1] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
131 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
132 elif next in NAMESTART: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
133 if offset > pos: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
134 yield False, text[pos:offset] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
135 pos = offset |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
136 pos += 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
137 while pos < end: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
138 char = text[pos] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
139 if char not in NAMECHARS: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
140 break |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
141 pos += 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
142 yield True, text[offset + 1:pos].strip() |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
143 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
144 elif not escaped and next == PREFIX: |
526
8779b2a775d9
Fix interpolation of short-form expressions that include literal text before the expression. Thanks to Alec for reporting the issue.
cmlenz
parents:
442
diff
changeset
|
145 if offset > pos: |
8779b2a775d9
Fix interpolation of short-form expressions that include literal text before the expression. Thanks to Alec for reporting the issue.
cmlenz
parents:
442
diff
changeset
|
146 yield False, text[pos:offset] |
407
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
147 escaped = True |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
148 pos = offset + 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
149 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
150 else: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
151 yield False, text[pos:offset + 1] |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
152 pos = offset + 1 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
153 |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
154 if pos < end: |
f37d8e6acdf2
Move string interpolation code into separate module (`genshi.template.interpolation`).
cmlenz
parents:
diff
changeset
|
155 yield False, text[pos:] |