comparison genshi/template/directives.py @ 351:0cc031745884 trunk

The `py:content`, `py:replace`, and `py:strip=""` directives are now expanded when the template is loaded (as opposed to when it's rendered).
author cmlenz
date Fri, 10 Nov 2006 23:32:24 +0000
parents 2aa7ca37ae6a
children fe40d34fb71d
comparison
equal deleted inserted replaced
346:96882a191686 351:0cc031745884
112 <li>Bye</li> 112 <li>Bye</li>
113 </ul> 113 </ul>
114 """ 114 """
115 __slots__ = [] 115 __slots__ = []
116 116
117 def __call__(self, stream, ctxt, directives): 117 def prepare(self, directives, stream):
118 def _generate(): 118 directives.remove(self)
119 yield stream.next() 119 return [stream[0], (EXPR, self.expr, (None, -1, --1)), stream[-1]]
120 yield EXPR, self.expr, (None, -1, -1)
121 event = stream.next()
122 for next in stream:
123 event = next
124 yield event
125
126 return _apply_directives(_generate(), ctxt, directives)
127 120
128 121
129 class DefDirective(Directive): 122 class DefDirective(Directive):
130 """Implementation of the `py:def` template directive. 123 """Implementation of the `py:def` template directive.
131 124
365 Bye 358 Bye
366 </div> 359 </div>
367 """ 360 """
368 __slots__ = [] 361 __slots__ = []
369 362
370 def __call__(self, stream, ctxt, directives): 363 def prepare(self, directives, stream):
371 yield EXPR, self.expr, (None, -1, -1) 364 directives.remove(self)
365 return [(EXPR, self.expr, (None, -1, -1))]
372 366
373 367
374 class StripDirective(Directive): 368 class StripDirective(Directive):
375 """Implementation of the `py:strip` template directive. 369 """Implementation of the `py:strip` template directive.
376 370
404 """ 398 """
405 __slots__ = [] 399 __slots__ = []
406 400
407 def __call__(self, stream, ctxt, directives): 401 def __call__(self, stream, ctxt, directives):
408 def _generate(): 402 def _generate():
409 if self.expr: 403 if self.expr.evaluate(ctxt):
410 strip = self.expr.evaluate(ctxt)
411 else:
412 strip = True
413 if strip:
414 stream.next() # skip start tag 404 stream.next() # skip start tag
415 previous = stream.next() 405 previous = stream.next()
416 for event in stream: 406 for event in stream:
417 yield previous 407 yield previous
418 previous = event 408 previous = event
419 else: 409 else:
420 for event in stream: 410 for event in stream:
421 yield event 411 yield event
422
423 return _apply_directives(_generate(), ctxt, directives) 412 return _apply_directives(_generate(), ctxt, directives)
413
414 def prepare(self, directives, stream):
415 if not self.expr:
416 directives.remove(self)
417 return stream[1:-1]
418 return stream
424 419
425 420
426 class ChooseDirective(Directive): 421 class ChooseDirective(Directive):
427 """Implementation of the `py:choose` directive for conditionally selecting 422 """Implementation of the `py:choose` directive for conditionally selecting
428 one of several body elements to display. 423 one of several body elements to display.
Copyright (C) 2012-2017 Edgewall Software