|
10 | 10 | urlparse = urllib.parse.urlparse |
11 | 11 | HTMLParser = html_parser.HTMLParser |
12 | 12 |
|
13 | | -from collections import OrderedDict |
| 13 | +try: |
| 14 | + from collections import OrderedDict |
| 15 | +except ImportError: |
| 16 | + from ordereddict import OrderedDict |
14 | 17 |
|
15 | 18 | from xml.etree import ElementTree |
16 | 19 |
|
@@ -47,22 +50,33 @@ def generate_tag(tag, content, attributes=None): |
47 | 50 | content are strings, the attributes argument is a dictionary. As |
48 | 51 | a convenience, if the content is ' /', a self-closing tag is generated.""" |
49 | 52 | content = six.text_type(content) |
50 | | - element = ElementTree.Element(tag, attrib=attributes) |
51 | 53 | enc = 'unicode' |
52 | 54 | if six.PY2: |
53 | 55 | enc = 'UTF-8' |
| 56 | + attributes = OrderedDict((k, v.decode('utf8')) for k, v in attributes.items()) |
54 | 57 | if not tag: |
55 | 58 | return content |
| 59 | + element = ElementTree.Element(tag, attrib=attributes) |
56 | 60 | # FIXME: Kind of an ugly hack. There *must* be a cleaner way. I tried |
57 | 61 | # adding text by assigning it to element_tag.text. That results in |
58 | 62 | # non-ascii text being html-entity encoded. Not bad, but not entirely |
59 | 63 | # matching php-textile either. |
60 | | - element_tag = ElementTree.tostringlist(element, encoding=enc, |
61 | | - method='html') |
62 | | - if six.PY2: |
| 64 | + try: |
| 65 | + element_tag = ElementTree.tostringlist(element, encoding=enc, |
| 66 | + method='html') |
63 | 67 | element_tag = [v.decode('utf8') for v in element_tag] |
64 | | - element_tag.insert(len(element_tag) - 1, content) |
65 | | - element_text = ''.join(element_tag) |
| 68 | + element_tag.insert(len(element_tag) - 1, content) |
| 69 | + element_text = ''.join(element_tag) |
| 70 | + except AttributeError: |
| 71 | + # Python 2.6 doesn't have the tostringlist method, so we have to treat |
| 72 | + # it differently. |
| 73 | + element_tag = ElementTree.tostring(element, encoding=enc) |
| 74 | + element_text = re.sub(r"<\?xml version='1.0' encoding='UTF-8'\?>\n", |
| 75 | + '', element_tag) |
| 76 | + if content != six.text_type(' /'): |
| 77 | + element_text = element_text.rstrip(' />') |
| 78 | + element_text = six.text_type('{0}>{1}</{2}>').format(six.text_type( |
| 79 | + element_text), content, tag) |
66 | 80 | return element_text |
67 | 81 |
|
68 | 82 | def has_raw_text(text): |
|
0 commit comments