1
2 r"""
3 :Copyright:
4
5 Copyright 2006 - 2015
6 Andr\xe9 Malo or his licensors, as applicable
7
8 :License:
9
10 Licensed under the Apache License, Version 2.0 (the "License");
11 you may not use this file except in compliance with the License.
12 You may obtain a copy of the License at
13
14 http://www.apache.org/licenses/LICENSE-2.0
15
16 Unless required by applicable law or agreed to in writing, software
17 distributed under the License is distributed on an "AS IS" BASIS,
18 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 See the License for the specific language governing permissions and
20 limitations under the License.
21
22 ========================
23 Output Encoder Classes
24 ========================
25
26 This module provides output encoding logic.
27 """
28 if __doc__:
29
30 __doc__ = __doc__.encode('ascii').decode('unicode_escape')
31 __author__ = r"Andr\xe9 Malo".encode('ascii').decode('unicode_escape')
32 __docformat__ = "restructuredtext en"
33
34 from ... import interfaces as _interfaces
35
36
38 """ Encoder for HTML/XML output """
39 __implements__ = [_interfaces.EncoderInterface]
40
42 """
43 Initialization
44
45 :Parameters:
46 `encoding` : ``str``
47 The target encoding
48 """
49 self.encoding = encoding
50
52 """ :See: `EncoderInterface` """
53 return "<%s%s>" % (' '.join([name] + [
54 value is not None and "%s=%s" % (key, value) or key
55 for key, value in attr
56 ]), closed and ' /' or '')
57
59 """ :See: `EncoderInterface` """
60 return "</%s>" % name
61
62 - def name(self, name):
63 """ :See: `EncoderInterface` """
64 if isinstance(name, unicode):
65 return name.encode(self.encoding, 'strict')
66 return name
67
69 """ :See: `EncoderInterface` """
70 if isinstance(value, unicode):
71 value = (
72 value
73 .replace(u'&', u'&')
74 .replace(u'<', u'<')
75 .replace(u'>', u'>')
76 .replace(u'"', u'"')
77 .encode(self.encoding, 'xmlcharrefreplace')
78 )
79 else:
80 value = (
81 value
82 .replace('&', '&')
83 .replace('<', '<')
84 .replace('>', '>')
85 .replace('"', '"')
86 )
87
88 return '"%s"' % value
89
90 - def content(self, value):
91 """ :See: `EncoderInterface` """
92 if isinstance(value, unicode):
93 return (
94 value
95 .replace(u'&', u'&')
96 .replace(u'<', u'<')
97 .replace(u'>', u'>')
98 .encode(self.encoding, 'xmlcharrefreplace')
99 )
100 return (
101 value
102 .replace('&', '&')
103 .replace('<', '<')
104 .replace('>', '>')
105 )
106
108 """ :See: `EncoderInterface` """
109 return value.encode(self.encoding, 'xmlcharrefreplace')
110
112 """ :See: `EncoderInterface` """
113 return value
114
115
116 from ... import c
117 c = c.load('impl')
118 if c is not None:
119 SoupEncoder = c.SoupEncoder
120 del c
121