|
1 """mime type transformation engine for cubicweb, based on mtconverter |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2008 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 """ |
|
7 __docformat__ = "restructuredtext en" |
|
8 |
|
9 from logilab import mtconverter |
|
10 |
|
11 from logilab.mtconverter.engine import TransformEngine |
|
12 from logilab.mtconverter.transform import Transform |
|
13 from cubicweb.common.uilib import rest_publish, html_publish, remove_html_tags |
|
14 |
|
15 HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml') |
|
16 # CubicWeb specific transformations |
|
17 |
|
18 class rest_to_html(Transform): |
|
19 inputs = ('text/rest', 'text/x-rst') |
|
20 output = 'text/html' |
|
21 def _convert(self, trdata): |
|
22 return rest_publish(trdata.appobject, trdata.decode()) |
|
23 |
|
24 class html_to_html(Transform): |
|
25 inputs = HTML_MIMETYPES |
|
26 output = 'text/html' |
|
27 def _convert(self, trdata): |
|
28 return html_publish(trdata.appobject, trdata.data) |
|
29 |
|
30 |
|
31 # Instantiate and configure the transformation engine |
|
32 |
|
33 mtconverter.UNICODE_POLICY = 'replace' |
|
34 |
|
35 ENGINE = TransformEngine() |
|
36 ENGINE.add_transform(rest_to_html()) |
|
37 ENGINE.add_transform(html_to_html()) |
|
38 |
|
39 HAS_PIL_TRANSFORMS = False |
|
40 HAS_PYGMENTS_TRANSFORMS = False |
|
41 |
|
42 class html_to_text(Transform): |
|
43 inputs = HTML_MIMETYPES |
|
44 output = 'text/plain' |
|
45 def _convert(self, trdata): |
|
46 return remove_html_tags(trdata.data) |
|
47 ENGINE.add_transform(html_to_text()) |