Package tdi :: Package markup :: Package soup :: Module encoder
[frames] | no frames]

Source Code for Module tdi.markup.soup.encoder

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2006 - 2013 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 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   
30 -class SoupEncoder(object):
31 """ Encoder for HTML/XML output """ 32 __implements__ = [_interfaces.EncoderInterface] 33
34 - def __init__(self, encoding):
35 """ 36 Initialization 37 38 :Parameters: 39 `encoding` : ``str`` 40 The target encoding 41 """ 42 self.encoding = encoding
43
44 - def starttag(self, name, attr, closed):
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
51 - def endtag(self, name):
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
61 - def attribute(self, value):
62 """ :See: `EncoderInterface` """ 63 if isinstance(value, unicode): 64 value = (value 65 .replace(u'&', u'&amp;') 66 .replace(u'<', u'&lt;') 67 .replace(u'>', u'&gt;') 68 .replace(u'"', u'&quot;') 69 .encode(self.encoding, 'xmlcharrefreplace') 70 ) 71 else: 72 value = (value 73 .replace('&', '&amp;') 74 .replace('<', '&lt;') 75 .replace('>', '&gt;') 76 .replace('"', '&quot;') 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'&amp;') 86 .replace(u'<', u'&lt;') 87 .replace(u'>', u'&gt;') 88 .encode(self.encoding, 'xmlcharrefreplace') 89 ) 90 return (value 91 .replace('&', '&amp;') 92 .replace('<', '&lt;') 93 .replace('>', '&gt;') 94 )
95
96 - def encode(self, value):
97 """ :See: `EncoderInterface` """ 98 return value.encode(self.encoding, 'xmlcharrefreplace')
99
100 - def escape(self, value):
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