Edgewall Software

Changeset 385


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.
Location:
trunk/genshi
Files:
4 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):
  • trunk/genshi/template.py

    r384 r385  
    12881288       
    12891289        @param search_path: a list of absolute path names that should be
    1290             searched for template files
     1290            searched for template files, or a string containing a single
     1291            absolute path
    12911292        @param auto_reload: whether to check the last modification time of
    12921293            template files, and reload them if they have changed
     
    12971298        if self.search_path is None:
    12981299            self.search_path = []
     1300        elif isinstance(self.search_path, basestring):
     1301            self.search_path = [self.search_path]
    12991302        self.auto_reload = auto_reload
    13001303        self._cache = LRUCache(max_cache_size)
  • trunk/genshi/tests/input.py

    r361 r385  
    121121            self.assertEqual((None, 1, 6), pos)
    122122
     123    def test_input_encoding(self):
     124        text = u'<div>\xf6</div>'.encode('iso-8859-1')
     125        events = list(HTMLParser(StringIO(text), encoding='iso-8859-1'))
     126        kind, data, pos = events[1]
     127        self.assertEqual(Stream.TEXT, kind)
     128        self.assertEqual(u'\xf6', data)
     129
    123130    def test_unicode_input(self):
    124131        text = u'<div>\u2013</div>'
  • trunk/genshi/tests/template.py

    r370 r385  
    11411141
    11421142
     1143    # FIXME
     1144    #def test_empty_lines(self):
     1145    #    tmpl = TextTemplate("""Your items:
     1146    #
     1147    #    #for item in items
     1148    #      * ${item}
     1149    #
     1150    #    #end""")
     1151    #    self.assertEqual("""Your items:
     1152    #      * 0
     1153    #      * 1
     1154    #      * 2
     1155    #    """, tmpl.generate(items=range(3)).render('text'))
     1156
     1157
    11431158class TemplateLoaderTestCase(unittest.TestCase):
    11441159    """Tests for the template loader."""
     
    11491164    def tearDown(self):
    11501165        shutil.rmtree(self.dirname)
     1166
     1167    def test_search_path_empty(self):
     1168        loader = TemplateLoader()
     1169        self.assertEqual([], loader.search_path)
     1170
     1171    def test_search_path_as_string(self):
     1172        loader = TemplateLoader(self.dirname)
     1173        self.assertEqual([self.dirname], loader.search_path)
    11511174
    11521175    def test_relative_include_samedir(self):
Note: See TracChangeset for help on using the changeset viewer.