Ticket #110: eof_newline.patch
| File eof_newline.patch, 3.9 KB (added by Carsten Klein <carsten.klein@…>, 13 years ago) |
|---|
-
output.py
181 189 _PRESERVE_SPACE = frozenset() 182 190 183 191 def __init__(self, doctype=None, strip_whitespace=True, 184 namespace_prefixes=None, cache=True): 192 namespace_prefixes=None, cache=True, 193 append_new_line_at_eof=False): 185 194 """Initialize the XML serializer. 186 195 187 196 :param doctype: a ``(name, pubid, sysid)`` tuple that represents the … … 203 212 if doctype: 204 213 self.filters.append(DocTypeInserter(doctype)) 205 214 self.cache = cache 215 self.append_new_line_at_eof = append_new_line_at_eof 206 216 207 217 def __call__(self, stream): 208 218 have_decl = have_doctype = False … … 281 291 elif kind is PI: 282 292 yield _emit(kind, data, Markup('<?%s %s?>' % data)) 283 293 294 if self.append_new_line_at_eof: 295 yield unicode('\n') 284 296 297 285 298 class XHTMLSerializer(XMLSerializer): 286 299 """Produces XHTML text from an event stream. 287 300 … … 303 316 ]) 304 317 305 318 def __init__(self, doctype=None, strip_whitespace=True, 306 namespace_prefixes=None, drop_xml_decl=True, cache=True): 319 namespace_prefixes=None, drop_xml_decl=True, cache=True, 320 append_new_line_at_eof=False): 307 321 super(XHTMLSerializer, self).__init__(doctype, False) 308 322 self.filters = [EmptyTagFilter()] 309 323 if strip_whitespace: … … 316 330 self.filters.append(DocTypeInserter(doctype)) 317 331 self.drop_xml_decl = drop_xml_decl 318 332 self.cache = cache 333 self.append_new_line_at_eof = append_new_line_at_eof 319 334 320 335 def __call__(self, stream): 321 336 boolean_attrs = self._BOOLEAN_ATTRS … … 409 424 elif kind is PI: 410 425 yield _emit(kind, data, Markup('<?%s %s?>' % data)) 411 426 427 if self.append_new_line_at_eof: 428 yield unicode('\n') 412 429 430 413 431 class HTMLSerializer(XHTMLSerializer): 414 432 """Produces HTML text from an event stream. 415 433 … … 424 442 QName('style'), QName('http://www.w3.org/1999/xhtml}style') 425 443 ]) 426 444 427 def __init__(self, doctype=None, strip_whitespace=True, cache=True ):445 def __init__(self, doctype=None, strip_whitespace=True, cache=True, append_new_line_at_eof=False): 428 446 """Initialize the HTML serializer. 429 447 430 448 :param doctype: a ``(name, pubid, sysid)`` tuple that represents the … … 447 465 if doctype: 448 466 self.filters.append(DocTypeInserter(doctype)) 449 467 self.cache = True 468 self.append_new_line_at_eof = append_new_line_at_eof 450 469 451 470 def __call__(self, stream): 452 471 boolean_attrs = self._BOOLEAN_ATTRS … … 526 545 elif kind is PI: 527 546 yield _emit(kind, data, Markup('<?%s %s?>' % data)) 528 547 548 if self.append_new_line_at_eof: 549 yield unicode('\n') 529 550 551 530 552 class TextSerializer(object): 531 553 """Produces plain text from an event stream. 532 554 … … 556 578 Hello & Bye! 557 579 """ 558 580 559 def __init__(self, strip_markup=False ):581 def __init__(self, strip_markup=False, append_new_line_at_eof=False): 560 582 """Create the serializer. 561 583 562 584 :param strip_markup: whether markup (tags and encoded characters) found 563 585 in the text should be removed 564 586 """ 565 587 self.strip_markup = strip_markup 588 self.append_new_line_at_eof = append_new_line_at_eof 566 589 567 590 def __call__(self, stream): 568 591 strip_markup = self.strip_markup … … 573 596 data = data.striptags().stripentities() 574 597 yield unicode(data) 575 598 599 if self.append_new_line_at_eof: 600 yield unicode('\n') 576 601 602 577 603 class EmptyTagFilter(object): 578 604 """Combines `START` and `STOP` events into `EMPTY` events for elements that 579 605 have no contents.
