|
1 """mime type transformation engine for cubicweb, based on mtconverter |
|
2 |
|
3 :organization: Logilab |
|
4 :copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
|
5 :contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
|
6 :license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
|
7 """ |
|
8 __docformat__ = "restructuredtext en" |
|
9 |
|
10 from logilab import mtconverter |
|
11 |
|
12 from logilab.mtconverter.engine import TransformEngine |
|
13 from logilab.mtconverter.transform import Transform |
|
14 from logilab.mtconverter import (register_base_transforms, |
|
15 register_pil_transforms, |
|
16 register_pygments_transforms) |
|
17 |
|
18 from cubicweb.utils import UStringIO |
|
19 from cubicweb.uilib import rest_publish, html_publish |
|
20 |
|
21 HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml') |
|
22 |
|
23 # CubicWeb specific transformations |
|
24 |
|
25 class rest_to_html(Transform): |
|
26 inputs = ('text/rest', 'text/x-rst') |
|
27 output = 'text/html' |
|
28 def _convert(self, trdata): |
|
29 return rest_publish(trdata.appobject, trdata.decode()) |
|
30 |
|
31 class html_to_html(Transform): |
|
32 inputs = HTML_MIMETYPES |
|
33 output = 'text/html' |
|
34 def _convert(self, trdata): |
|
35 return html_publish(trdata.appobject, trdata.data) |
|
36 |
|
37 |
|
38 # Instantiate and configure the transformation engine |
|
39 |
|
40 mtconverter.UNICODE_POLICY = 'replace' |
|
41 |
|
42 ENGINE = TransformEngine() |
|
43 ENGINE.add_transform(rest_to_html()) |
|
44 ENGINE.add_transform(html_to_html()) |
|
45 |
|
46 try: |
|
47 from cubicweb.ext.tal import CubicWebContext, compile_template |
|
48 except ImportError: |
|
49 HAS_TAL = False |
|
50 from cubicweb import schema |
|
51 schema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template') |
|
52 |
|
53 else: |
|
54 HAS_TAL = True |
|
55 |
|
56 class ept_to_html(Transform): |
|
57 inputs = ('text/cubicweb-page-template',) |
|
58 output = 'text/html' |
|
59 output_encoding = 'utf-8' |
|
60 def _convert(self, trdata): |
|
61 context = CubicWebContext() |
|
62 appobject = trdata.appobject |
|
63 context.update({'self': appobject, 'rset': appobject.cw_rset, |
|
64 'req': appobject._cw, |
|
65 '_' : appobject._cw._, |
|
66 'user': appobject._cw.user}) |
|
67 output = UStringIO() |
|
68 template = compile_template(trdata.encode(self.output_encoding)) |
|
69 template.expand(context, output) |
|
70 return output.getvalue() |
|
71 |
|
72 ENGINE.add_transform(ept_to_html()) |
|
73 |
|
74 if register_pil_transforms(ENGINE, verb=False): |
|
75 HAS_PIL_TRANSFORMS = True |
|
76 else: |
|
77 HAS_PIL_TRANSFORMS = False |
|
78 |
|
79 try: |
|
80 from logilab.mtconverter.transforms import pygmentstransforms |
|
81 for mt in ('text/plain',) + HTML_MIMETYPES: |
|
82 try: |
|
83 pygmentstransforms.mimetypes.remove(mt) |
|
84 except ValueError: |
|
85 continue |
|
86 register_pygments_transforms(ENGINE, verb=False) |
|
87 |
|
88 def patch_convert(cls): |
|
89 def _convert(self, trdata, origconvert=cls._convert): |
|
90 try: |
|
91 trdata.appobject._cw.add_css('pygments.css') |
|
92 except AttributeError: # session has no add_css, only http request |
|
93 pass |
|
94 return origconvert(self, trdata) |
|
95 cls._convert = _convert |
|
96 patch_convert(pygmentstransforms.PygmentsHTMLTransform) |
|
97 |
|
98 HAS_PYGMENTS_TRANSFORMS = True |
|
99 except ImportError: |
|
100 HAS_PYGMENTS_TRANSFORMS = False |
|
101 |
|
102 register_base_transforms(ENGINE, verb=False) |