Edgewall Software

Changeset 385 for trunk/genshi/input.py


Ignore:
Timestamp:
Oct 22, 2006, 4:57:40 PM (17 years ago)
Author:
cmlenz
Message:
  • The HTMLParser class and the HTML function now accept an encoding parameter to properly deal with bytestring input (defaults to UTF-8).
  • The TemplateLoader class can now also be initialized from a string for the search path, for cases where the search path contains only a single directory.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/genshi/input.py

    r361 r385  
    7777
    7878    def __init__(self, source, filename=None):
    79         """Initialize the parser for the given XML text.
     79        """Initialize the parser for the given XML input.
    8080       
    8181        @param source: the XML text as a file-like object
     
    251251                              'param'])
    252252
    253     def __init__(self, source, filename=None):
     253    def __init__(self, source, filename=None, encoding='utf-8'):
     254        """Initialize the parser for the given HTML input.
     255       
     256        @param source: the HTML text as a file-like object
     257        @param filename: the name of the file, if known
     258        @param filename: encoding of the file; ignored if the input is unicode
     259        """
    254260        html.HTMLParser.__init__(self)
    255261        self.source = source
    256262        self.filename = filename
     263        self.encoding = encoding
    257264        self._queue = []
    258265        self._open_tags = []
     
    322329
    323330    def handle_data(self, text):
     331        if not isinstance(text, unicode):
     332            text = text.decode(self.encoding, 'replace')
    324333        self._enqueue(TEXT, text)
    325334
     
    344353
    345354
    346 def HTML(text):
    347     return Stream(list(HTMLParser(StringIO(text))))
     355def HTML(text, encoding='utf-8'):
     356    return Stream(list(HTMLParser(StringIO(text), encoding=encoding)))
    348357
    349358def _coalesce(stream):
Note: See TracChangeset for help on using the changeset viewer.