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

Source Code for Module tdi.markup.soup.encoder

  1  # -*- coding: ascii -*- 
  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   
34 -class SoupEncoder(object):
35 """ Encoder for HTML/XML output """ 36 __implements__ = [_interfaces.EncoderInterface] 37
38 - def __init__(self, encoding):
39 """ 40 Initialization 41 42 :Parameters: 43 `encoding` : ``str`` 44 The target encoding 45 """ 46 self.encoding = encoding
47
48 - def starttag(self, name, attr, closed):
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
55 - def endtag(self, name):
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
65 - def attribute(self, value):
66 """ :See: `EncoderInterface` """ 67 if isinstance(value, unicode): 68 value = (value 69 .replace(u'&', u'&amp;') 70 .replace(u'<', u'&lt;') 71 .replace(u'>', u'&gt;') 72 .replace(u'"', u'&quot;') 73 .encode(self.encoding, 'xmlcharrefreplace') 74 ) 75 else: 76 value = (value 77 .replace('&', '&amp;') 78 .replace('<', '&lt;') 79 .replace('>', '&gt;') 80 .replace('"', '&quot;') 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'&amp;') 90 .replace(u'<', u'&lt;') 91 .replace(u'>', u'&gt;') 92 .encode(self.encoding, 'xmlcharrefreplace') 93 ) 94 return (value 95 .replace('&', '&amp;') 96 .replace('<', '&lt;') 97 .replace('>', '&gt;') 98 )
99
100 - def encode(self, value):
101 """ :See: `EncoderInterface` """ 102 return value.encode(self.encoding, 'xmlcharrefreplace')
103
104 - def escape(self, value):
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