1
2 u"""
3 :Copyright:
4
5 Copyright 2006 - 2013
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 __author__ = u"Andr\xe9 Malo"
29 __docformat__ = "restructuredtext en"
30
31 from tdi import interfaces as _interfaces
32
33
35 """ Encoder for HTML/XML output """
36 __implements__ = [_interfaces.EncoderInterface]
37
39 """
40 Initialization
41
42 :Parameters:
43 `encoding` : ``str``
44 The target encoding
45 """
46 self.encoding = encoding
47
49 """ :See: `EncoderInterface` """
50 return "<%s%s>" % (' '.join([name] + [
51 value is not None and "%s=%s" % (key, value) or key
52 for key, value in attr
53 ]), closed and ' /' or '')
54
56 """ :See: `EncoderInterface` """
57 return "</%s>" % name
58
59 - def name(self, name):
60 """ :See: `EncoderInterface` """
61 if isinstance(name, unicode):
62 return name.encode(self.encoding, 'strict')
63 return name
64
66 """ :See: `EncoderInterface` """
67 if isinstance(value, unicode):
68 value = (value
69 .replace(u'&', u'&')
70 .replace(u'<', u'<')
71 .replace(u'>', u'>')
72 .replace(u'"', u'"')
73 .encode(self.encoding, 'xmlcharrefreplace')
74 )
75 else:
76 value = (value
77 .replace('&', '&')
78 .replace('<', '<')
79 .replace('>', '>')
80 .replace('"', '"')
81 )
82
83 return '"%s"' % value
84
85 - def content(self, value):
86 """ :See: `EncoderInterface` """
87 if isinstance(value, unicode):
88 return (value
89 .replace(u'&', u'&')
90 .replace(u'<', u'<')
91 .replace(u'>', u'>')
92 .encode(self.encoding, 'xmlcharrefreplace')
93 )
94 return (value
95 .replace('&', '&')
96 .replace('<', '<')
97 .replace('>', '>')
98 )
99
101 """ :See: `EncoderInterface` """
102 return value.encode(self.encoding, 'xmlcharrefreplace')
103
105 """ :See: `EncoderInterface` """
106 return value
107
108
109 from tdi import c
110 c = c.load('impl')
111 if c is not None:
112 SoupEncoder = c.SoupEncoder
113 del c
114