Commit 62ec2cee authored by Sylvain Thenault's avatar Sylvain Thenault
Browse files

xml_to_text and rest_to_text transforms, though rest_to_text is not registered...

xml_to_text and rest_to_text transforms, though rest_to_text is not registered by register_base_transforms
parent 0fd4c0ba894c
......@@ -196,10 +196,12 @@ def register_pygments_transforms(engine, verb=True):
def register_base_transforms(engine, verb=True):
from logilab.mtconverter.transforms import cmdtransforms, text_to_text, text_to_html, xlog_to_html
from logilab.mtconverter.transforms import cmdtransforms, text_to_text, \
xml_to_text, text_to_html, xlog_to_html
from logilab.mtconverter.transforms.python import python_to_html
from logilab.mtconverter.transforms.html2text import html_to_formatted_text
engine.add_transform(text_to_text())
engine.add_transform(xml_to_text())
engine.add_transform(text_to_html())
engine.add_transform(xlog_to_html())
engine.add_transform(python_to_html())
......
......@@ -11,11 +11,16 @@ register_pil_transforms(ENGINE)
class Html2TextTC(TestCase):
def test(self):
def test_html_to_text(self):
data = TransformData(u'<b>yo (zou ;)</b>', 'text/html', 'utf8')
converted = ENGINE.convert(data, 'text/plain').decode().strip()
self.assertEquals(converted, u'**yo (zou ;)**')
def test_xml_to_text(self):
data = TransformData(u'<root><b>yo (zou ;)</b>a<tag/>b<root>', 'application/xml', 'utf8')
converted = ENGINE.convert(data, 'text/plain').decode().strip()
self.assertEquals(converted, u'yo (zou ;) a b')
if __name__ == '__main__':
unittest_main()
......
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc.,
# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
"""some basic transformations (pure python)
:organization: Logilab
:copyright: 2006-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
"""
__docformat__ = "restructuredtext en"
import re
from logilab.mtconverter import html_escape
from logilab.mtconverter.transform import Transform
......@@ -12,6 +34,29 @@ class text_to_text(IdentityTransform):
output = 'text/plain'
class rest_to_text(Transform):
inputs = ('text/rest', 'text/x-rst')
output = 'text/plain'
def _convert(self, trdata):
res = []
for line in trdata.data.splitlines():
sline = line.lstrip()
if sline.startswith('.. '):
continue
res.append(line)
return '\n'.join(res)
_TAG_PROG = re.compile(r'</?.*?>', re.U)
class xml_to_text(Transform):
inputs = ('application/xml',)
output = 'text/plain'
def _convert(self, trdata):
return _TAG_PROG.sub(' ', trdata.data)
class text_to_html(Transform):
inputs = ('text/plain',)
output = 'text/html'
......@@ -27,14 +72,6 @@ class text_to_html(Transform):
res.append(html_escape(line))
res.append('</p>')
return '\n'.join(res)
class xlog_to_html(Transform):
inputs = ('text/x-log',)
output = 'text/html'
def _convert(self, trdata):
return '\n'.join([x+'<BR/>' for x in trdata.data.splitlines()])
class text_to_html_pre(Transform):
......@@ -49,3 +86,10 @@ class text_to_html_pre(Transform):
res.append('</pre>')
return '\n'.join(res)
class xlog_to_html(Transform):
inputs = ('text/x-log',)
output = 'text/html'
def _convert(self, trdata):
return '\n'.join([x+'<BR/>' for x in trdata.data.splitlines()])
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment