Changeset 525 for trunk/genshi/input.py
- Timestamp:
- Mar 22, 2007, 10:12:03 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/genshi/input.py (modified) (6 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/genshi/input.py
r517 r525 35 35 36 36 def ET(element): 37 """Convert a given ElementTree element to a markup stream.""" 37 """Convert a given ElementTree element to a markup stream. 38 39 :param element: an ElementTree element 40 :return: a markup stream 41 """ 38 42 tag_name = QName(element.tag.lstrip('{')) 39 43 attrs = Attrs([(QName(attr), value) for attr, value in element.items()]) … … 52 56 class ParseError(Exception): 53 57 """Exception raised when fatal syntax errors are found in the input being 54 parsed.""" 58 parsed. 59 """ 55 60 56 61 def __init__(self, message, filename=None, lineno=-1, offset=-1): 62 """Exception initializer. 63 64 :param message: the error message from the parser 65 :param filename: the path to the file that was parsed 66 :param lineno: the number of the line on which the error was encountered 67 :param offset: the column number where the error was encountered 68 """ 57 69 self.msg = message 58 70 if filename: … … 129 141 130 142 def parse(self): 143 """Generator that parses the XML source, yielding markup events. 144 145 :return: a markup event stream 146 :raises ParseError: if the XML text is not well formed 147 """ 131 148 def _generate(): 132 149 try: … … 238 255 239 256 def XML(text): 257 """Parse the given XML source and return a markup stream. 258 259 Unlike with `XMLParser`, the returned stream is reusable, meaning it can be 260 iterated over multiple times: 261 262 >>> xml = XML('<doc><elem>Foo</elem><elem>Bar</elem></doc>') 263 >>> print xml 264 <doc><elem>Foo</elem><elem>Bar</elem></doc> 265 >>> print xml.select('elem') 266 <elem>Foo</elem><elem>Bar</elem> 267 >>> print xml.select('elem/text()') 268 FooBar 269 270 :param text: the XML source 271 :return: the parsed XML event stream 272 :raises ParseError: if the XML text is not well-formed 273 """ 240 274 return Stream(list(XMLParser(StringIO(text)))) 241 275 … … 278 312 279 313 def parse(self): 314 """Generator that parses the HTML source, yielding markup events. 315 316 :return: a markup event stream 317 :raises ParseError: if the HTML text is not well formed 318 """ 280 319 def _generate(): 281 320 try: … … 369 408 370 409 def HTML(text, encoding='utf-8'): 410 """Parse the given HTML source and return a markup stream. 411 412 Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be 413 iterated over multiple times: 414 415 >>> html = HTML('<body><h1>Foo</h1></body>') 416 >>> print html 417 <body><h1>Foo</h1></body> 418 >>> print html.select('h1') 419 <h1>Foo</h1> 420 >>> print html.select('h1/text()') 421 Foo 422 423 :param text: the HTML source 424 :return: the parsed XML event stream 425 :raises ParseError: if the HTML text is not well-formed, and error recovery 426 fails 427 """ 371 428 return Stream(list(HTMLParser(StringIO(text), encoding=encoding))) 372 429
Note: See TracChangeset
for help on using the changeset viewer.
