Returns a Unicode representation of this tag and its contents. :param eventual_encoding: The tag is destined to be encoded into this encoding. This method is _not_ responsible for performing that encoding. This information is passed in so that it can be subs
(self, indent_level=None,
eventual_encoding=DEFAULT_OUTPUT_ENCODING,
formatter="minimal")
| 1063 | or self._is_xml)) |
| 1064 | |
| 1065 | def decode(self, indent_level=None, |
| 1066 | eventual_encoding=DEFAULT_OUTPUT_ENCODING, |
| 1067 | formatter="minimal"): |
| 1068 | """Returns a Unicode representation of this tag and its contents. |
| 1069 | |
| 1070 | :param eventual_encoding: The tag is destined to be |
| 1071 | encoded into this encoding. This method is _not_ |
| 1072 | responsible for performing that encoding. This information |
| 1073 | is passed in so that it can be substituted in if the |
| 1074 | document contains a <META> tag that mentions the document's |
| 1075 | encoding. |
| 1076 | """ |
| 1077 | |
| 1078 | # First off, turn a string formatter into a function. This |
| 1079 | # will stop the lookup from happening over and over again. |
| 1080 | if not isinstance(formatter, collections.Callable): |
| 1081 | formatter = self._formatter_for_name(formatter) |
| 1082 | |
| 1083 | attrs = [] |
| 1084 | if self.attrs: |
| 1085 | for key, val in sorted(self.attrs.items()): |
| 1086 | if val is None: |
| 1087 | decoded = key |
| 1088 | else: |
| 1089 | if isinstance(val, list) or isinstance(val, tuple): |
| 1090 | val = ' '.join(val) |
| 1091 | elif not isinstance(val, str): |
| 1092 | val = str(val) |
| 1093 | elif ( |
| 1094 | isinstance(val, AttributeValueWithCharsetSubstitution) |
| 1095 | and eventual_encoding is not None): |
| 1096 | val = val.encode(eventual_encoding) |
| 1097 | |
| 1098 | text = self.format_string(val, formatter) |
| 1099 | decoded = ( |
| 1100 | str(key) + '=' |
| 1101 | + EntitySubstitution.quoted_attribute_value(text)) |
| 1102 | attrs.append(decoded) |
| 1103 | close = '' |
| 1104 | closeTag = '' |
| 1105 | |
| 1106 | prefix = '' |
| 1107 | if self.prefix: |
| 1108 | prefix = self.prefix + ":" |
| 1109 | |
| 1110 | if self.is_empty_element: |
| 1111 | close = '/' |
| 1112 | else: |
| 1113 | closeTag = '</%s%s>' % (prefix, self.name) |
| 1114 | |
| 1115 | pretty_print = self._should_pretty_print(indent_level) |
| 1116 | space = '' |
| 1117 | indent_space = '' |
| 1118 | if indent_level is not None: |
| 1119 | indent_space = (' ' * (indent_level - 1)) |
| 1120 | if pretty_print: |
| 1121 | space = indent_space |
| 1122 | indent_contents = indent_level + 1 |