comparison genshi/template/text.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) 2006-2008 Edgewall Software 3 # Copyright (C) 2006-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.
59 ... We have the following items for you: 59 ... We have the following items for you:
60 ... {% for item in items %} 60 ... {% for item in items %}
61 ... * ${'Item %d' % item} 61 ... * ${'Item %d' % item}
62 ... {% end %} 62 ... {% end %}
63 ... ''') 63 ... ''')
64 >>> print tmpl.generate(name='Joe', items=[1, 2, 3]).render() 64 >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
65 Dear Joe, 65 Dear Joe,
66 <BLANKLINE> 66 <BLANKLINE>
67 <BLANKLINE> 67 <BLANKLINE>
68 We have the following items for you: 68 We have the following items for you:
69 <BLANKLINE> 69 <BLANKLINE>
84 ... We have the following items for you: 84 ... We have the following items for you:
85 ... {% for item in items %}\ 85 ... {% for item in items %}\
86 ... * $item 86 ... * $item
87 ... {% end %}\ 87 ... {% end %}\
88 ... ''') 88 ... ''')
89 >>> print tmpl.generate(name='Joe', items=[1, 2, 3]).render() 89 >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
90 Dear Joe, 90 Dear Joe,
91 <BLANKLINE> 91 <BLANKLINE>
92 We have the following items for you: 92 We have the following items for you:
93 * 1 93 * 1
94 * 2 94 * 2
104 ... We have the following items for you: 104 ... We have the following items for you:
105 ... {% for item in items %}\ 105 ... {% for item in items %}\
106 ... * $item 106 ... * $item
107 ... {% end %}\ 107 ... {% end %}\
108 ... ''') 108 ... ''')
109 >>> print tmpl.generate(name='Joe', items=[1, 2, 3]).render() 109 >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
110 Dear Joe, 110 Dear Joe,
111 <BLANKLINE> 111 <BLANKLINE>
112 {# This is a comment #} 112 {# This is a comment #}
113 We have the following items for you: 113 We have the following items for you:
114 * 1 114 * 1
142 def _set_delims(self, delims): 142 def _set_delims(self, delims):
143 if len(delims) != 4: 143 if len(delims) != 4:
144 raise ValueError('delimiers tuple must have exactly four elements') 144 raise ValueError('delimiers tuple must have exactly four elements')
145 self._delims = delims 145 self._delims = delims
146 self._directive_re = re.compile(self._DIRECTIVE_RE % tuple( 146 self._directive_re = re.compile(self._DIRECTIVE_RE % tuple(
147 map(re.escape, delims) 147 [re.escape(d) for d in delims]
148 ), re.DOTALL) 148 ), re.DOTALL)
149 self._escape_re = re.compile(self._ESCAPE_RE % tuple( 149 self._escape_re = re.compile(self._ESCAPE_RE % tuple(
150 map(re.escape, delims[::2]) 150 [re.escape(d) for d in delims[::2]]
151 )) 151 ))
152 delimiters = property(_get_delims, _set_delims, """\ 152 delimiters = property(_get_delims, _set_delims, """\
153 The delimiters for directives and comments. This should be a four item tuple 153 The delimiters for directives and comments. This should be a four item tuple
154 of the form ``(directive_start, directive_end, comment_start, 154 of the form ``(directive_start, directive_end, comment_start,
155 comment_end)``, where each item is a string. 155 comment_end)``, where each item is a string.
167 offset = 0 167 offset = 0
168 lineno = 1 168 lineno = 1
169 169
170 _escape_sub = self._escape_re.sub 170 _escape_sub = self._escape_re.sub
171 def _escape_repl(mo): 171 def _escape_repl(mo):
172 groups = filter(None, mo.groups()) 172 groups = [g for g in mo.groups() if g]
173 if not groups: 173 if not groups:
174 return '' 174 return ''
175 return groups[0] 175 return groups[0]
176 176
177 for idx, mo in enumerate(self._directive_re.finditer(source)): 177 for idx, mo in enumerate(self._directive_re.finditer(source)):
217 217
218 elif command: 218 elif command:
219 cls = self.get_directive(command) 219 cls = self.get_directive(command)
220 if cls is None: 220 if cls is None:
221 raise BadDirectiveError(command) 221 raise BadDirectiveError(command)
222 directive = cls, value, None, (self.filepath, lineno, 0) 222 directive = 0, cls, value, None, (self.filepath, lineno, 0)
223 dirmap[depth] = (directive, len(stream)) 223 dirmap[depth] = (directive, len(stream))
224 depth += 1 224 depth += 1
225 225
226 offset = end 226 offset = end
227 227
246 ... * $item 246 ... * $item
247 ... #end 247 ... #end
248 ... 248 ...
249 ... All the best, 249 ... All the best,
250 ... Foobar''') 250 ... Foobar''')
251 >>> print tmpl.generate(name='Joe', items=[1, 2, 3]).render() 251 >>> print(tmpl.generate(name='Joe', items=[1, 2, 3]).render(encoding=None))
252 Dear Joe, 252 Dear Joe,
253 <BLANKLINE> 253 <BLANKLINE>
254 We have the following items for you: 254 We have the following items for you:
255 * 1 255 * 1
256 * 2 256 * 2
313 stream.append((INCLUDE, (value.strip(), None, []), pos)) 313 stream.append((INCLUDE, (value.strip(), None, []), pos))
314 elif command != '#': 314 elif command != '#':
315 cls = self.get_directive(command) 315 cls = self.get_directive(command)
316 if cls is None: 316 if cls is None:
317 raise BadDirectiveError(command) 317 raise BadDirectiveError(command)
318 directive = cls, value, None, (self.filepath, lineno, 0) 318 directive = 0, cls, value, None, (self.filepath, lineno, 0)
319 dirmap[depth] = (directive, len(stream)) 319 dirmap[depth] = (directive, len(stream))
320 depth += 1 320 depth += 1
321 321
322 offset = end 322 offset = end
323 323
Copyright (C) 2012-2017 Edgewall Software