Edgewall Software

Ticket #110: eof_newline.patch

File eof_newline.patch, 3.9 KB (added by Carsten Klein <carsten.klein@…>, 13 years ago)

Initial patch for this feature

  • output.py

     
    181189    _PRESERVE_SPACE = frozenset()
    182190
    183191    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):
    185194        """Initialize the XML serializer.
    186195       
    187196        :param doctype: a ``(name, pubid, sysid)`` tuple that represents the
     
    203212        if doctype:
    204213            self.filters.append(DocTypeInserter(doctype))
    205214        self.cache = cache
     215        self.append_new_line_at_eof = append_new_line_at_eof
    206216
    207217    def __call__(self, stream):
    208218        have_decl = have_doctype = False
     
    281291            elif kind is PI:
    282292                yield _emit(kind, data, Markup('<?%s %s?>' % data))
    283293
     294        if self.append_new_line_at_eof:
     295            yield unicode('\n')
    284296
     297
    285298class XHTMLSerializer(XMLSerializer):
    286299    """Produces XHTML text from an event stream.
    287300   
     
    303316    ])
    304317
    305318    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):
    307321        super(XHTMLSerializer, self).__init__(doctype, False)
    308322        self.filters = [EmptyTagFilter()]
    309323        if strip_whitespace:
     
    316330            self.filters.append(DocTypeInserter(doctype))
    317331        self.drop_xml_decl = drop_xml_decl
    318332        self.cache = cache
     333        self.append_new_line_at_eof = append_new_line_at_eof
    319334
    320335    def __call__(self, stream):
    321336        boolean_attrs = self._BOOLEAN_ATTRS
     
    409424            elif kind is PI:
    410425                yield _emit(kind, data, Markup('<?%s %s?>' % data))
    411426
     427        if self.append_new_line_at_eof:
     428            yield unicode('\n')
    412429
     430
    413431class HTMLSerializer(XHTMLSerializer):
    414432    """Produces HTML text from an event stream.
    415433   
     
    424442        QName('style'), QName('http://www.w3.org/1999/xhtml}style')
    425443    ])
    426444
    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):
    428446        """Initialize the HTML serializer.
    429447       
    430448        :param doctype: a ``(name, pubid, sysid)`` tuple that represents the
     
    447465        if doctype:
    448466            self.filters.append(DocTypeInserter(doctype))
    449467        self.cache = True
     468        self.append_new_line_at_eof = append_new_line_at_eof
    450469
    451470    def __call__(self, stream):
    452471        boolean_attrs = self._BOOLEAN_ATTRS
     
    526545            elif kind is PI:
    527546                yield _emit(kind, data, Markup('<?%s %s?>' % data))
    528547
     548        if self.append_new_line_at_eof:
     549            yield unicode('\n')
    529550
     551
    530552class TextSerializer(object):
    531553    """Produces plain text from an event stream.
    532554   
     
    556578    Hello & Bye!
    557579    """
    558580
    559     def __init__(self, strip_markup=False):
     581    def __init__(self, strip_markup=False, append_new_line_at_eof=False):
    560582        """Create the serializer.
    561583       
    562584        :param strip_markup: whether markup (tags and encoded characters) found
    563585                             in the text should be removed
    564586        """
    565587        self.strip_markup = strip_markup
     588        self.append_new_line_at_eof = append_new_line_at_eof
    566589
    567590    def __call__(self, stream):
    568591        strip_markup = self.strip_markup
     
    573596                    data = data.striptags().stripentities()
    574597                yield unicode(data)
    575598
     599        if self.append_new_line_at_eof:
     600            yield unicode('\n')
    576601
     602
    577603class EmptyTagFilter(object):
    578604    """Combines `START` and `STOP` events into `EMPTY` events for elements that
    579605    have no contents.