Package python-module-logilab-mtconverter-0 :: Package 8 :: Package 4 :: Package transforms
[frames] | no frames]

Source Code for Package python-module-logilab-mtconverter-0.8.4.transforms

  1  # This program is free software; you can redistribute it and/or modify it under 
  2  # the terms of the GNU General Public License as published by the Free Software 
  3  # Foundation; either version 2 of the License, or (at your option) any later 
  4  # version. 
  5  # 
  6  # This program is distributed in the hope that it will be useful, but WITHOUT 
  7  # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 
  8  # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. 
  9  # 
 10  # You should have received a copy of the GNU General Public License along with 
 11  # this program; if not, write to the Free Software Foundation, Inc., 
 12  # 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. 
 13  # copyright 2006-2011 LOGILAB S.A. (Paris, FRANCE), all rights reserved. 
 14  # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr 
 15  # 
 16  # This file is part of logilab-mtconverter. 
 17  # 
 18  # logilab-mtconverter is free software: you can redistribute it and/or modify it 
 19  # under the terms of the GNU Lesser General Public License as published by the 
 20  # Free Software Foundation, either version 2.1 of the License, or (at your 
 21  # option) any later version. 
 22  # 
 23  # logilab-mtconverter is distributed in the hope that it will be useful, but 
 24  # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
 25  # FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License 
 26  # for more details. 
 27  # 
 28  # You should have received a copy of the GNU Lesser General Public License along 
 29  # with logilab-mtconverter. If not, see <http://www.gnu.org/licenses/>. 
 30  """some basic transformations (pure python) 
 31   
 32  """ 
 33  __docformat__ = "restructuredtext en" 
 34   
 35  import re 
 36   
 37  from logilab.mtconverter import xml_escape 
 38  from logilab.mtconverter.transform import Transform 
 39   
40 -class IdentityTransform(Transform):
41 """identity transform: leave the content unchanged"""
42 - def _convert(self, trdata):
43 return trdata.data
44 45
46 -class text_to_text(IdentityTransform):
47 inputs = ('text/*',) 48 output = 'text/plain'
49 50
51 -class rest_to_text(Transform):
52 inputs = ('text/rest', 'text/x-rst') 53 output = 'text/plain' 54
55 - def _convert(self, trdata):
56 res = [] 57 for line in trdata.data.splitlines(): 58 sline = line.lstrip() 59 if sline.startswith('.. '): 60 continue 61 res.append(line) 62 return '\n'.join(res)
63 64 65 _TAG_PROG = re.compile(r'</?.*?>', re.U)
66 -class xml_to_text(Transform):
67 inputs = ('application/xml',) 68 output = 'text/plain' 69
70 - def _convert(self, trdata):
71 return _TAG_PROG.sub(' ', trdata.data)
72 73
74 -class text_to_html(Transform):
75 inputs = ('text/plain',) 76 output = 'text/html' 77
78 - def _convert(self, trdata):
79 res = ['<p>'] 80 for line in trdata.data.splitlines(): 81 line = line.strip() 82 if not line: 83 if not res[-1].endswith('<p>'): 84 res.append('</p><p>') 85 else: 86 res.append(xml_escape(line) + '<br/>') 87 res.append('</p>') 88 return '\n'.join(res)
89 90
91 -class text_to_html_pre(Transform):
92 """variant for text 2 html transformation : simply wrap text into pre tags 93 """ 94 inputs = ('text/plain',) 95 output = 'text/html' 96
97 - def _convert(self, trdata):
98 res = ['<pre>'] 99 res.append(xml_escape(trdata.data)) 100 res.append('</pre>') 101 return '\n'.join(res)
102 103
104 -class xlog_to_html(Transform):
105 inputs = ('text/x-log',) 106 output = 'text/html' 107
108 - def _convert(self, trdata):
109 return '\n'.join([xml_escape(x)+'<BR/>' for x in trdata.data.splitlines()])
110