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

Source Code for Module tdi.markup.soup.encoder

  1  # -*- coding: ascii -*- 
  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      # pylint: disable = redefined-builtin 
 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   
37 -class SoupEncoder(object):
38 """ Encoder for HTML/XML output """ 39 __implements__ = [_interfaces.EncoderInterface] 40
41 - def __init__(self, encoding):
42 """ 43 Initialization 44 45 :Parameters: 46 `encoding` : ``str`` 47 The target encoding 48 """ 49 self.encoding = encoding
50
51 - def starttag(self, name, attr, closed):
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
58 - def endtag(self, name):
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
68 - def attribute(self, value):
69 """ :See: `EncoderInterface` """ 70 if isinstance(value, unicode): 71 value = ( 72 value 73 .replace(u'&', u'&amp;') 74 .replace(u'<', u'&lt;') 75 .replace(u'>', u'&gt;') 76 .replace(u'"', u'&quot;') 77 .encode(self.encoding, 'xmlcharrefreplace') 78 ) 79 else: 80 value = ( 81 value 82 .replace('&', '&amp;') 83 .replace('<', '&lt;') 84 .replace('>', '&gt;') 85 .replace('"', '&quot;') 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'&amp;') 96 .replace(u'<', u'&lt;') 97 .replace(u'>', u'&gt;') 98 .encode(self.encoding, 'xmlcharrefreplace') 99 ) 100 return ( 101 value 102 .replace('&', '&amp;') 103 .replace('<', '&lt;') 104 .replace('>', '&gt;') 105 )
106
107 - def encode(self, value):
108 """ :See: `EncoderInterface` """ 109 return value.encode(self.encoding, 'xmlcharrefreplace')
110
111 - def escape(self, value):
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 # noqa 120 del c 121