1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 """
18 ========================
19 Output Encoder Classes
20 ========================
21
22 This module provides output encoding logic.
23 """
24 __author__ = u"Andr\xe9 Malo"
25 __docformat__ = "restructuredtext en"
26
27 from tdi import interfaces as _interfaces
28
29
31 """ Encoder for HTML/XML output """
32 __implements__ = [_interfaces.EncoderInterface]
33
35 """
36 Initialization
37
38 :Parameters:
39 `encoding` : ``str``
40 The target encoding
41 """
42 self.encoding = encoding
43
45 """ :See: `EncoderInterface` """
46 return "<%s%s>" % (' '.join([name] + [
47 value is not None and "%s=%s" % (key, value) or key
48 for key, value in attr
49 ]), closed and ' /' or '')
50
52 """ :See: `EncoderInterface` """
53 return "</%s>" % name
54
55 - def name(self, name):
56 """ :See: `EncoderInterface` """
57 if isinstance(name, unicode):
58 return name.encode(self.encoding, 'strict')
59 return name
60
62 """ :See: `EncoderInterface` """
63 if isinstance(value, unicode):
64 value = (value
65 .replace(u'&', u'&')
66 .replace(u'<', u'<')
67 .replace(u'>', u'>')
68 .replace(u'"', u'"')
69 .encode(self.encoding, 'xmlcharrefreplace')
70 )
71 else:
72 value = (value
73 .replace('&', '&')
74 .replace('<', '<')
75 .replace('>', '>')
76 .replace('"', '"')
77 )
78
79 return '"%s"' % value
80
81 - def content(self, value):
82 """ :See: `EncoderInterface` """
83 if isinstance(value, unicode):
84 return (value
85 .replace(u'&', u'&')
86 .replace(u'<', u'<')
87 .replace(u'>', u'>')
88 .encode(self.encoding, 'xmlcharrefreplace')
89 )
90 return (value
91 .replace('&', '&')
92 .replace('<', '<')
93 .replace('>', '>')
94 )
95
97 """ :See: `EncoderInterface` """
98 return value.encode(self.encoding, 'xmlcharrefreplace')
99
101 """ :See: `EncoderInterface` """
102 return value
103
104
105 from tdi import c
106 c = c.load('impl')
107 if c is not None:
108 SoupEncoder = c.SoupEncoder
109 del c
110