Mercurial > genshi > mirror
annotate genshi/input.py @ 999:f2d1f04b0959 stable-0.6.x
Merge r1219 from trunk (fix for PIs without data, fixes #368).
author | hodgestar |
---|---|
date | Sat, 26 Jan 2013 17:37:46 +0000 |
parents | f3d998cc941e |
children |
rev | line source |
---|---|
1 | 1 # -*- coding: utf-8 -*- |
2 # | |
854
4d9bef447df9
More work on reducing the size of the diff produced by 2to3.
cmlenz
parents:
853
diff
changeset
|
3 # Copyright (C) 2006-2009 Edgewall Software |
1 | 4 # All rights reserved. |
5 # | |
6 # This software is licensed as described in the file COPYING, which | |
7 # you should have received as part of this distribution. The terms | |
230 | 8 # are also available at http://genshi.edgewall.org/wiki/License. |
1 | 9 # |
10 # This software consists of voluntary contributions made by many | |
11 # individuals. For the exact contribution history, see the revision | |
230 | 12 # history and logs, available at http://genshi.edgewall.org/log/. |
1 | 13 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
14 """Support for constructing markup streams from files, strings, or other |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
15 sources. |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
16 """ |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
17 |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
18 from itertools import chain |
859 | 19 import htmlentitydefs as entities |
20 import HTMLParser as html | |
21 from StringIO import StringIO | |
1 | 22 from xml.parsers import expat |
23 | |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
24 from genshi.core import Attrs, QName, Stream, stripentities |
859 | 25 from genshi.core import START, END, XML_DECL, DOCTYPE, TEXT, START_NS, \ |
26 END_NS, START_CDATA, END_CDATA, PI, COMMENT | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
27 |
290
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
28 __all__ = ['ET', 'ParseError', 'XMLParser', 'XML', 'HTMLParser', 'HTML'] |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
29 __docformat__ = 'restructuredtext en' |
290
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
30 |
859 | 31 |
290
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
32 def ET(element): |
433 | 33 """Convert a given ElementTree element to a markup stream. |
34 | |
35 :param element: an ElementTree element | |
36 :return: a markup stream | |
37 """ | |
290
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
38 tag_name = QName(element.tag.lstrip('{')) |
458
5f5b227b04be
The `ET()` function now correctly handles attributes with a namespace.
cmlenz
parents:
434
diff
changeset
|
39 attrs = Attrs([(QName(attr.lstrip('{')), value) |
5f5b227b04be
The `ET()` function now correctly handles attributes with a namespace.
cmlenz
parents:
434
diff
changeset
|
40 for attr, value in element.items()]) |
290
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
41 |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
42 yield START, (tag_name, attrs), (None, -1, -1) |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
43 if element.text: |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
44 yield TEXT, element.text, (None, -1, -1) |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
45 for child in element.getchildren(): |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
46 for item in ET(child): |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
47 yield item |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
48 yield END, tag_name, (None, -1, -1) |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
49 if element.tail: |
94f9f2cc66c8
Move the ElementTree ''element-to-stream'' adaptation function `ET()` into the `genshi.input` module.
cmlenz
parents:
230
diff
changeset
|
50 yield TEXT, element.tail, (None, -1, -1) |
1 | 51 |
52 | |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
53 class ParseError(Exception): |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
54 """Exception raised when fatal syntax errors are found in the input being |
433 | 55 parsed. |
56 """ | |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
57 |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
419
diff
changeset
|
58 def __init__(self, message, filename=None, lineno=-1, offset=-1): |
433 | 59 """Exception initializer. |
60 | |
61 :param message: the error message from the parser | |
62 :param filename: the path to the file that was parsed | |
63 :param lineno: the number of the line on which the error was encountered | |
64 :param offset: the column number where the error was encountered | |
65 """ | |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
419
diff
changeset
|
66 self.msg = message |
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
419
diff
changeset
|
67 if filename: |
434
5692bc32ba5f
* Better method to propogate the full path to the template file on parse errors. Supersedes r513.
cmlenz
parents:
433
diff
changeset
|
68 message += ', in ' + filename |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
69 Exception.__init__(self, message) |
422
5d08a744636e
More work to include absolute file paths in exceptions.
cmlenz
parents:
419
diff
changeset
|
70 self.filename = filename or '<string>' |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
71 self.lineno = lineno |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
72 self.offset = offset |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
73 |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
74 |
1 | 75 class XMLParser(object): |
76 """Generator-based XML parser based on roughly equivalent code in | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
77 Kid/ElementTree. |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
78 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
79 The parsing is initiated by iterating over the parser object: |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
80 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
81 >>> parser = XMLParser(StringIO('<root id="2"><child>Foo</child></root>')) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
82 >>> for kind, data, pos in parser: |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
83 ... print('%s %s' % (kind, data)) |
857
129e54866a98
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
84 START (QName('root'), Attrs([(QName('id'), u'2')])) |
129e54866a98
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
85 START (QName('child'), Attrs()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
86 TEXT Foo |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
87 END child |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
88 END root |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
89 """ |
1 | 90 |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
91 _entitydefs = ['<!ENTITY %s "&#%d;">' % (name, value) for name, value in |
856 | 92 entities.name2codepoint.items()] |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
93 _external_dtd = '\n'.join(_entitydefs) |
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
94 |
316
a946edefac40
Configurable encoding of template files, closing #65.
cmlenz
parents:
312
diff
changeset
|
95 def __init__(self, source, filename=None, encoding=None): |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
96 """Initialize the parser for the given XML input. |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
97 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
98 :param source: the XML text as a file-like object |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
99 :param filename: the name of the file, if appropriate |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
100 :param encoding: the encoding of the file; if not specified, the |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
101 encoding is assumed to be ASCII, UTF-8, or UTF-16, or |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
102 whatever the encoding specified in the XML declaration |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
103 (if any) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
104 """ |
1 | 105 self.source = source |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
106 self.filename = filename |
1 | 107 |
108 # Setup the Expat parser | |
316
a946edefac40
Configurable encoding of template files, closing #65.
cmlenz
parents:
312
diff
changeset
|
109 parser = expat.ParserCreate(encoding, '}') |
1 | 110 parser.buffer_text = True |
111 parser.returns_unicode = True | |
160 | 112 parser.ordered_attributes = True |
113 | |
1 | 114 parser.StartElementHandler = self._handle_start |
115 parser.EndElementHandler = self._handle_end | |
116 parser.CharacterDataHandler = self._handle_data | |
117 parser.StartDoctypeDeclHandler = self._handle_doctype | |
118 parser.StartNamespaceDeclHandler = self._handle_start_ns | |
119 parser.EndNamespaceDeclHandler = self._handle_end_ns | |
143
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
120 parser.StartCdataSectionHandler = self._handle_start_cdata |
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
121 parser.EndCdataSectionHandler = self._handle_end_cdata |
1 | 122 parser.ProcessingInstructionHandler = self._handle_pi |
460
75425671b437
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
458
diff
changeset
|
123 parser.XmlDeclHandler = self._handle_xml_decl |
1 | 124 parser.CommentHandler = self._handle_comment |
209
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
125 |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
126 # Tell Expat that we'll handle non-XML entities ourselves |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
127 # (in _handle_other) |
1 | 128 parser.DefaultHandler = self._handle_other |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
129 parser.SetParamEntityParsing(expat.XML_PARAM_ENTITY_PARSING_ALWAYS) |
209
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
130 parser.UseForeignDTD() |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
131 parser.ExternalEntityRefHandler = self._build_foreign |
1 | 132 |
133 self.expat = parser | |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
134 self._queue = [] |
1 | 135 |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
136 def parse(self): |
433 | 137 """Generator that parses the XML source, yielding markup events. |
138 | |
139 :return: a markup event stream | |
140 :raises ParseError: if the XML text is not well formed | |
141 """ | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
142 def _generate(): |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
143 try: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
144 bufsize = 4 * 1024 # 4K |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
145 done = False |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
146 while 1: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
147 while not done and len(self._queue) == 0: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
148 data = self.source.read(bufsize) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
149 if data == '': # end of data |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
150 if hasattr(self, 'expat'): |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
151 self.expat.Parse('', True) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
152 del self.expat # get rid of circular references |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
153 done = True |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
154 else: |
207
28bfc6aafab7
The `XMLParser` now correctly handles unicode input. Closes #43.
cmlenz
parents:
182
diff
changeset
|
155 if isinstance(data, unicode): |
28bfc6aafab7
The `XMLParser` now correctly handles unicode input. Closes #43.
cmlenz
parents:
182
diff
changeset
|
156 data = data.encode('utf-8') |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
157 self.expat.Parse(data, False) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
158 for event in self._queue: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
159 yield event |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
160 self._queue = [] |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
161 if done: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
162 break |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
163 except expat.ExpatError, e: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
164 msg = str(e) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
165 raise ParseError(msg, self.filename, e.lineno, e.offset) |
146 | 166 return Stream(_generate()).filter(_coalesce) |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
167 |
1 | 168 def __iter__(self): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
169 return iter(self.parse()) |
1 | 170 |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
171 def _build_foreign(self, context, base, sysid, pubid): |
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
172 parser = self.expat.ExternalEntityParserCreate(context) |
859 | 173 parser.ParseFile(StringIO(self._external_dtd)) |
293
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
174 return 1 |
e17b7459b515
Fix a bug in the XML parser, where attributes containing HTML entity references would get pulled out of the attribute value, and instead added as a text node just before the associated start tag. Thanks to Hamish Lawson for [http://groups.google.com/group/genshi/browse_thread/thread/c64eb48676b0ff96/0e6ce786e8820f3d pointing out the problem].
cmlenz
parents:
290
diff
changeset
|
175 |
143
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
176 def _enqueue(self, kind, data=None, pos=None): |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
177 if pos is None: |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
178 pos = self._getpos() |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
179 if kind is TEXT: |
134
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
180 # Expat reports the *end* of the text event as current position. We |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
181 # try to fix that up here as much as possible. Unfortunately, the |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
182 # offset is only valid for single-line text. For multi-line text, |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
183 # it is apparently not possible to determine at what offset it |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
184 # started |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
185 if '\n' in data: |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
186 lines = data.splitlines() |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
187 lineno = pos[1] - len(lines) + 1 |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
188 offset = -1 |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
189 else: |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
190 lineno = pos[1] |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
191 offset = pos[2] - len(data) |
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
192 pos = (pos[0], lineno, offset) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
193 self._queue.append((kind, data, pos)) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
194 |
1 | 195 def _getpos_unknown(self): |
134
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
196 return (self.filename, -1, -1) |
1 | 197 |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
198 def _getpos(self): |
134
d681d2c3cd8d
* Improve the accuracy of line numbers for text nodes, so that reported errors about syntax or evaluation errors in expressions point to the right line (not quite perfect yet, though).
cmlenz
parents:
69
diff
changeset
|
199 return (self.filename, self.expat.CurrentLineNumber, |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
200 self.expat.CurrentColumnNumber) |
1 | 201 |
202 def _handle_start(self, tag, attrib): | |
403
228907abb726
Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
cmlenz
parents:
378
diff
changeset
|
203 attrs = Attrs([(QName(name), value) for name, value in |
228907abb726
Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
cmlenz
parents:
378
diff
changeset
|
204 zip(*[iter(attrib)] * 2)]) |
228907abb726
Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
cmlenz
parents:
378
diff
changeset
|
205 self._enqueue(START, (QName(tag), attrs)) |
1 | 206 |
207 def _handle_end(self, tag): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
208 self._enqueue(END, QName(tag)) |
1 | 209 |
210 def _handle_data(self, text): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
211 self._enqueue(TEXT, text) |
1 | 212 |
460
75425671b437
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
458
diff
changeset
|
213 def _handle_xml_decl(self, version, encoding, standalone): |
75425671b437
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
458
diff
changeset
|
214 self._enqueue(XML_DECL, (version, encoding, standalone)) |
75425671b437
Apply patch by Alec Thomas for processing XML declarations (#111). Thanks!
cmlenz
parents:
458
diff
changeset
|
215 |
1 | 216 def _handle_doctype(self, name, sysid, pubid, has_internal_subset): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
217 self._enqueue(DOCTYPE, (name, pubid, sysid)) |
1 | 218 |
219 def _handle_start_ns(self, prefix, uri): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
220 self._enqueue(START_NS, (prefix or '', uri)) |
1 | 221 |
222 def _handle_end_ns(self, prefix): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
223 self._enqueue(END_NS, prefix or '') |
1 | 224 |
143
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
225 def _handle_start_cdata(self): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
226 self._enqueue(START_CDATA) |
143
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
227 |
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
228 def _handle_end_cdata(self): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
229 self._enqueue(END_CDATA) |
143
3d4c214c979a
CDATA sections in XML input now appear as CDATA sections in the output. This should address the problem with escaping the contents of `<style>` and `<script>` elements, which would only get interpreted correctly if the output was served as `application/xhtml+xml`. Closes #24.
cmlenz
parents:
140
diff
changeset
|
230 |
1 | 231 def _handle_pi(self, target, data): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
232 self._enqueue(PI, (target, data)) |
1 | 233 |
234 def _handle_comment(self, text): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
235 self._enqueue(COMMENT, text) |
1 | 236 |
237 def _handle_other(self, text): | |
238 if text.startswith('&'): | |
239 # deal with undefined entities | |
240 try: | |
856 | 241 text = unichr(entities.name2codepoint[text[1:-1]]) |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
242 self._enqueue(TEXT, text) |
1 | 243 except KeyError: |
209
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
244 filename, lineno, offset = self._getpos() |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
245 error = expat.error('undefined entity "%s": line %d, column %d' |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
246 % (text, lineno, offset)) |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
247 error.code = expat.errors.XML_ERROR_UNDEFINED_ENTITY |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
248 error.lineno = lineno |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
249 error.offset = offset |
fc6b2fb66518
* Fix bug in handling of undefined entities. Thanks to Arnar for reporting the issue on IRC.
cmlenz
parents:
207
diff
changeset
|
250 raise error |
1 | 251 |
252 | |
253 def XML(text): | |
433 | 254 """Parse the given XML source and return a markup stream. |
255 | |
256 Unlike with `XMLParser`, the returned stream is reusable, meaning it can be | |
257 iterated over multiple times: | |
258 | |
259 >>> xml = XML('<doc><elem>Foo</elem><elem>Bar</elem></doc>') | |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
260 >>> print(xml) |
433 | 261 <doc><elem>Foo</elem><elem>Bar</elem></doc> |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
262 >>> print(xml.select('elem')) |
433 | 263 <elem>Foo</elem><elem>Bar</elem> |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
264 >>> print(xml.select('elem/text()')) |
433 | 265 FooBar |
266 | |
267 :param text: the XML source | |
268 :return: the parsed XML event stream | |
269 :raises ParseError: if the XML text is not well-formed | |
270 """ | |
859 | 271 return Stream(list(XMLParser(StringIO(text)))) |
1 | 272 |
273 | |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
274 class HTMLParser(html.HTMLParser, object): |
1 | 275 """Parser for HTML input based on the Python `HTMLParser` module. |
276 | |
277 This class provides the same interface for generating stream events as | |
278 `XMLParser`, and attempts to automatically balance tags. | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
279 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
280 The parsing is initiated by iterating over the parser object: |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
281 |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
282 >>> parser = HTMLParser(StringIO('<UL compact><LI>Foo</UL>')) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
283 >>> for kind, data, pos in parser: |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
284 ... print('%s %s' % (kind, data)) |
857
129e54866a98
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
285 START (QName('ul'), Attrs([(QName('compact'), u'compact')])) |
129e54866a98
Avoid unicode literals in `repr`s of `QName` and `Namespace` when not necessary.
cmlenz
parents:
856
diff
changeset
|
286 START (QName('li'), Attrs()) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
287 TEXT Foo |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
288 END li |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
289 END ul |
1 | 290 """ |
291 | |
292 _EMPTY_ELEMS = frozenset(['area', 'base', 'basefont', 'br', 'col', 'frame', | |
293 'hr', 'img', 'input', 'isindex', 'link', 'meta', | |
294 'param']) | |
295 | |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
296 def __init__(self, source, filename=None, encoding='utf-8'): |
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
297 """Initialize the parser for the given HTML input. |
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
298 |
425
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
299 :param source: the HTML text as a file-like object |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
300 :param filename: the name of the file, if known |
073640758a42
Try to use proper reStructuredText for docstrings throughout.
cmlenz
parents:
423
diff
changeset
|
301 :param filename: encoding of the file; ignored if the input is unicode |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
302 """ |
1 | 303 html.HTMLParser.__init__(self) |
304 self.source = source | |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
305 self.filename = filename |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
306 self.encoding = encoding |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
307 self._queue = [] |
1 | 308 self._open_tags = [] |
309 | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
310 def parse(self): |
433 | 311 """Generator that parses the HTML source, yielding markup events. |
312 | |
313 :return: a markup event stream | |
314 :raises ParseError: if the HTML text is not well formed | |
315 """ | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
316 def _generate(): |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
317 try: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
318 bufsize = 4 * 1024 # 4K |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
319 done = False |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
320 while 1: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
321 while not done and len(self._queue) == 0: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
322 data = self.source.read(bufsize) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
323 if data == '': # end of data |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
324 self.close() |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
325 done = True |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
326 else: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
327 self.feed(data) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
328 for kind, data, pos in self._queue: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
329 yield kind, data, pos |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
330 self._queue = [] |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
331 if done: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
332 open_tags = self._open_tags |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
333 open_tags.reverse() |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
334 for tag in open_tags: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
335 yield END, QName(tag), pos |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
336 break |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
337 except html.HTMLParseError, e: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
338 msg = '%s: line %d, column %d' % (e.msg, e.lineno, e.offset) |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
339 raise ParseError(msg, self.filename, e.lineno, e.offset) |
146 | 340 return Stream(_generate()).filter(_coalesce) |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
341 |
1 | 342 def __iter__(self): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
343 return iter(self.parse()) |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
344 |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
345 def _enqueue(self, kind, data, pos=None): |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
346 if pos is None: |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
347 pos = self._getpos() |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
348 self._queue.append((kind, data, pos)) |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
349 |
21
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
350 def _getpos(self): |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
351 lineno, column = self.getpos() |
b4d17897d053
* Include paths are now interpreted relative to the path of the including template. Closes #3.
cmlenz
parents:
1
diff
changeset
|
352 return (self.filename, lineno, column) |
1 | 353 |
354 def handle_starttag(self, tag, attrib): | |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
355 fixed_attrib = [] |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
356 for name, value in attrib: # Fixup minimized attributes |
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
357 if value is None: |
312
cb7326367f91
Follow-up to [385]: also decode attribute values in the `HTMLParser`.
cmlenz
parents:
311
diff
changeset
|
358 value = unicode(name) |
cb7326367f91
Follow-up to [385]: also decode attribute values in the `HTMLParser`.
cmlenz
parents:
311
diff
changeset
|
359 elif not isinstance(value, unicode): |
cb7326367f91
Follow-up to [385]: also decode attribute values in the `HTMLParser`.
cmlenz
parents:
311
diff
changeset
|
360 value = value.decode(self.encoding, 'replace') |
403
228907abb726
Remove some magic/overhead from `Attrs` creation and manipulation by not automatically wrapping attribute names in `QName`.
cmlenz
parents:
378
diff
changeset
|
361 fixed_attrib.append((QName(name), stripentities(value))) |
26
3c1a022be04c
* Split out the XPath tests into a separate `unittest`-based file.
cmlenz
parents:
21
diff
changeset
|
362 |
182
2f30ce3fb85e
Renamed `Attributes` to `Attrs` to reduce the verbosity.
cmlenz
parents:
160
diff
changeset
|
363 self._enqueue(START, (QName(tag), Attrs(fixed_attrib))) |
1 | 364 if tag in self._EMPTY_ELEMS: |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
365 self._enqueue(END, QName(tag)) |
1 | 366 else: |
367 self._open_tags.append(tag) | |
368 | |
369 def handle_endtag(self, tag): | |
370 if tag not in self._EMPTY_ELEMS: | |
371 while self._open_tags: | |
372 open_tag = self._open_tags.pop() | |
378
873ca2a7ec05
Improve handling of incorrectly nested tags in the HTML parser.
cmlenz
parents:
376
diff
changeset
|
373 self._enqueue(END, QName(open_tag)) |
1 | 374 if open_tag.lower() == tag.lower(): |
375 break | |
376 | |
377 def handle_data(self, text): | |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
378 if not isinstance(text, unicode): |
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
379 text = text.decode(self.encoding, 'replace') |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
380 self._enqueue(TEXT, text) |
1 | 381 |
382 def handle_charref(self, name): | |
423
56bbe1d94da0
Applied patch for #106 (handling of hex charrefs in HTML parser).
cmlenz
parents:
422
diff
changeset
|
383 if name.lower().startswith('x'): |
56bbe1d94da0
Applied patch for #106 (handling of hex charrefs in HTML parser).
cmlenz
parents:
422
diff
changeset
|
384 text = unichr(int(name[1:], 16)) |
56bbe1d94da0
Applied patch for #106 (handling of hex charrefs in HTML parser).
cmlenz
parents:
422
diff
changeset
|
385 else: |
56bbe1d94da0
Applied patch for #106 (handling of hex charrefs in HTML parser).
cmlenz
parents:
422
diff
changeset
|
386 text = unichr(int(name)) |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
387 self._enqueue(TEXT, text) |
1 | 388 |
389 def handle_entityref(self, name): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
390 try: |
856 | 391 text = unichr(entities.name2codepoint[name]) |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
392 except KeyError: |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
393 text = '&%s;' % name |
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
394 self._enqueue(TEXT, text) |
1 | 395 |
396 def handle_pi(self, data): | |
376
0e0952d85d97
Fix parsing of processing instructions in HTML input.
cmlenz
parents:
326
diff
changeset
|
397 if data.endswith('?'): |
0e0952d85d97
Fix parsing of processing instructions in HTML input.
cmlenz
parents:
326
diff
changeset
|
398 data = data[:-1] |
999
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
399 try: |
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
400 target, data = data.split(None, 1) |
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
401 except ValueError: |
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
402 # PI with no data |
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
403 target = data |
f2d1f04b0959
Merge r1219 from trunk (fix for PIs without data, fixes #368).
hodgestar
parents:
859
diff
changeset
|
404 data = '' |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
405 self._enqueue(PI, (target.strip(), data.strip())) |
1 | 406 |
407 def handle_comment(self, text): | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
408 self._enqueue(COMMENT, text) |
1 | 409 |
410 | |
311
8de1ff534d22
* The `HTMLParser` class and the `HTML` function now accept an `encoding` parameter to properly deal with bytestring input (defaults to UTF-8).
cmlenz
parents:
293
diff
changeset
|
411 def HTML(text, encoding='utf-8'): |
433 | 412 """Parse the given HTML source and return a markup stream. |
413 | |
414 Unlike with `HTMLParser`, the returned stream is reusable, meaning it can be | |
415 iterated over multiple times: | |
416 | |
417 >>> html = HTML('<body><h1>Foo</h1></body>') | |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
418 >>> print(html) |
433 | 419 <body><h1>Foo</h1></body> |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
420 >>> print(html.select('h1')) |
433 | 421 <h1>Foo</h1> |
853
f33ecf3c319e
Convert a bunch of print statements to py3k compatible syntax.
cmlenz
parents:
852
diff
changeset
|
422 >>> print(html.select('h1/text()')) |
433 | 423 Foo |
424 | |
425 :param text: the HTML source | |
426 :return: the parsed XML event stream | |
427 :raises ParseError: if the HTML text is not well-formed, and error recovery | |
428 fails | |
429 """ | |
859 | 430 return Stream(list(HTMLParser(StringIO(text), encoding=encoding))) |
431 | |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
432 |
146 | 433 def _coalesce(stream): |
144
d1ce85a7f296
* Coalesce adjacent text events that the parsers would produce when text crossed the buffer boundaries. Fixes #26.
cmlenz
parents:
143
diff
changeset
|
434 """Coalesces adjacent TEXT events into a single event.""" |
146 | 435 textbuf = [] |
436 textpos = None | |
437 for kind, data, pos in chain(stream, [(None, None, None)]): | |
438 if kind is TEXT: | |
439 textbuf.append(data) | |
440 if textpos is None: | |
441 textpos = pos | |
442 else: | |
443 if textbuf: | |
852
07f4339fecb0
Remove usage of unicode literals in a couple of places where they were not strictly necessary.
cmlenz
parents:
750
diff
changeset
|
444 yield TEXT, ''.join(textbuf), textpos |
146 | 445 del textbuf[:] |
446 textpos = None | |
447 if kind: | |
448 yield kind, data, pos |