comparison genshi/template/markup.py @ 843:004f81b59d97

Refactored the template flattening method to be less recursive.
author cmlenz
date Wed, 18 Mar 2009 16:44:02 +0000
parents 0319a8874510
children 4f9e5e6f1aab
comparison
equal deleted inserted replaced
841:67ec9a402bdc 843:004f81b59d97
309 to the stream. 309 to the stream.
310 """ 310 """
311 match_templates = ctxt._match_templates 311 match_templates = ctxt._match_templates
312 312
313 tail = [] 313 tail = []
314 def _strip(stream): 314 def _strip(stream, append=tail.append):
315 depth = 1 315 depth = 1
316 next = stream.next
316 while 1: 317 while 1:
317 event = stream.next() 318 event = next()
318 if event[0] is START: 319 if event[0] is START:
319 depth += 1 320 depth += 1
320 elif event[0] is END: 321 elif event[0] is END:
321 depth -= 1 322 depth -= 1
322 if depth > 0: 323 if depth > 0:
323 yield event 324 yield event
324 else: 325 else:
325 tail[:] = [event] 326 append(event)
326 break 327 break
327 328
328 for event in stream: 329 for event in stream:
329 330
330 # We (currently) only care about start and end events for matching 331 # We (currently) only care about start and end events for matching
358 if pre_end > 0: 359 if pre_end > 0:
359 inner = self._match(inner, ctxt, end=pre_end, **vars) 360 inner = self._match(inner, ctxt, end=pre_end, **vars)
360 content = self._include(chain([event], inner, tail), ctxt) 361 content = self._include(chain([event], inner, tail), ctxt)
361 if 'not_buffered' not in hints: 362 if 'not_buffered' not in hints:
362 content = list(content) 363 content = list(content)
364 content = Stream(content)
363 365
364 # Make the select() function available in the body of the 366 # Make the select() function available in the body of the
365 # match template 367 # match template
366 selected = [False] 368 selected = [False]
367 def select(path): 369 def select(path):
368 selected[0] = True 370 selected[0] = True
369 return Stream(content).select(path, namespaces, ctxt) 371 return content.select(path, namespaces, ctxt)
370 vars = dict(select=select) 372 vars = dict(select=select)
371 373
372 # Recursively process the output 374 # Recursively process the output
373 template = _apply_directives(template, directives, ctxt, 375 template = _apply_directives(template, directives, ctxt,
374 vars) 376 vars)
Copyright (C) 2012-2017 Edgewall Software