comparison genshi/output.py @ 368:94ff33bfe515 stable-0.3.x

Ported [425] to 0.3.x.
author cmlenz
date Wed, 22 Nov 2006 20:55:08 +0000
parents 65a46e008098
children
comparison
equal deleted inserted replaced
367:eb2ef86a2cec 368:94ff33bfe515
181 'hr', 'img', 'input', 'isindex', 'link', 'meta', 181 'hr', 'img', 'input', 'isindex', 'link', 'meta',
182 'param']) 182 'param'])
183 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare', 183 _BOOLEAN_ATTRS = frozenset(['selected', 'checked', 'compact', 'declare',
184 'defer', 'disabled', 'ismap', 'multiple', 184 'defer', 'disabled', 'ismap', 'multiple',
185 'nohref', 'noresize', 'noshade', 'nowrap']) 185 'nohref', 'noresize', 'noshade', 'nowrap'])
186 _PRESERVE_SPACE = frozenset([QName('pre'), QName('textarea')]) 186 _PRESERVE_SPACE = frozenset([
187 QName('pre'), QName('http://www.w3.org/1999/xhtml}pre'),
188 QName('textarea'), QName('http://www.w3.org/1999/xhtml}textarea')
189 ])
187 190
188 def __call__(self, stream): 191 def __call__(self, stream):
189 namespace = self.NAMESPACE 192 namespace = self.NAMESPACE
190 ns_attrib = [] 193 ns_attrib = []
191 ns_mapping = {XML_NAMESPACE.uri: 'xml'} 194 ns_mapping = {XML_NAMESPACE.uri: 'xml'}
459 @param preserve: a set or sequence of tag names for which white-space 462 @param preserve: a set or sequence of tag names for which white-space
460 should be ignored. 463 should be ignored.
461 @param noescape: a set or sequence of tag names for which text content 464 @param noescape: a set or sequence of tag names for which text content
462 should not be escaped 465 should not be escaped
463 466
464 Both the `preserve` and `noescape` sets are expected to refer to 467 The `noescape` set is expected to refer to elements that cannot contain
465 elements that cannot contain further child elements. 468 further child elements (such as <style> or <script> in HTML documents).
466 """ 469 """
467 if preserve is None: 470 if preserve is None:
468 preserve = [] 471 preserve = []
469 self.preserve = frozenset(preserve) 472 self.preserve = frozenset(preserve)
470 if noescape is None: 473 if noescape is None:
475 def __call__(self, stream, ctxt=None, space=XML_NAMESPACE['space'], 478 def __call__(self, stream, ctxt=None, space=XML_NAMESPACE['space'],
476 trim_trailing_space=re.compile('[ \t]+(?=\n)').sub, 479 trim_trailing_space=re.compile('[ \t]+(?=\n)').sub,
477 collapse_lines=re.compile('\n{2,}').sub): 480 collapse_lines=re.compile('\n{2,}').sub):
478 mjoin = Markup('').join 481 mjoin = Markup('').join
479 preserve_elems = self.preserve 482 preserve_elems = self.preserve
480 preserve = False 483 preserve = 0
481 noescape_elems = self.noescape 484 noescape_elems = self.noescape
482 noescape = False 485 noescape = False
483 escape_cdata = self.escape_cdata 486 escape_cdata = self.escape_cdata
484 487
485 textbuf = [] 488 textbuf = []
500 if not preserve: 503 if not preserve:
501 text = collapse_lines('\n', trim_trailing_space('', text)) 504 text = collapse_lines('\n', trim_trailing_space('', text))
502 yield TEXT, Markup(text), pos 505 yield TEXT, Markup(text), pos
503 506
504 if kind is START: 507 if kind is START:
505 tag, attrib = data 508 tag, attrs = data
506 if not preserve and (tag in preserve_elems or 509 if preserve or (tag in preserve_elems or
507 attrib.get(space) == 'preserve'): 510 attrs.get(space) == 'preserve'):
508 preserve = True 511 preserve += 1
509 if not noescape and tag in noescape_elems: 512 if not noescape and tag in noescape_elems:
510 noescape = True 513 noescape = True
511 514
512 elif kind is END: 515 elif kind is END:
513 preserve = noescape = False 516 noescape = False
517 if preserve:
518 preserve -= 1
514 519
515 elif kind is START_CDATA and not escape_cdata: 520 elif kind is START_CDATA and not escape_cdata:
516 noescape = True 521 noescape = True
517 522
518 elif kind is END_CDATA and not escape_cdata: 523 elif kind is END_CDATA and not escape_cdata:
Copyright (C) 2012-2017 Edgewall Software