Package python-module-logilab-mtconverter-0 ::
Package 8 ::
Package 4 ::
Package transforms
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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
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
105 inputs = ('text/x-log',)
106 output = 'text/html'
107
109 return '\n'.join([xml_escape(x)+'<BR/>' for x in trdata.data.splitlines()])
110