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

Source Code for Module tdi.markup.text.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 TextEncoder(object):
38 """ Encoder for text 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 (closed and "[[%s]]" or "[%s]") % (' '.join([name] + [ 54 value is not None and "%s=%s" % (key, value) or key 55 for key, value in attr 56 ]))
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'\\"') 74 .encode(self.encoding, 'strict') 75 ) 76 else: 77 value = value.replace('"', '\\"') 78 return '"%s"' % value
79
80 - def content(self, value):
81 """ :See: `EncoderInterface` """ 82 if isinstance(value, unicode): 83 return ( 84 value 85 .encode(self.encoding, 'strict') 86 ) 87 return value
88
89 - def encode(self, value):
90 """ :See: `EncoderInterface` """ 91 return value.encode(self.encoding, 'strict')
92
93 - def escape(self, value):
94 """ :See: `EncoderInterface` """ 95 return value.replace('[', '[]')
96 97 98 from ... import c 99 c = c.load('impl') 100 if c is not None: 101 TextEncoder = c.TextEncoder # noqa 102 del c 103