author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Tue, 26 Jan 2010 20:29:52 +0100 | |
changeset 4381 | 62a89969c8b3 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
permissions | -rw-r--r-- |
0 | 1 |
"""mime type transformation engine for cubicweb, based on mtconverter |
2 |
||
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2459
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1584
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 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, |
|
1584 | 15 |
register_pil_transforms, |
0 | 16 |
register_pygments_transforms) |
17 |
||
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
18 |
from cubicweb.utils import UStringIO |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3418
diff
changeset
|
19 |
from cubicweb.uilib import rest_publish, html_publish |
0 | 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) |
|
1584 | 36 |
|
0 | 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()) |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
45 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
46 |
try: |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
47 |
from cubicweb.ext.tal import CubicWebContext, compile_template |
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
48 |
except ImportError: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
49 |
HAS_TAL = False |
2459
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
50 |
from cubicweb import schema |
d088d0ff48a1
move RichString and co to yams, keeping only a small monkeypatch for cw-page-template here
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
51 |
schema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template') |
1584 | 52 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
53 |
else: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
54 |
HAS_TAL = True |
1584 | 55 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
56 |
class ept_to_html(Transform): |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
57 |
inputs = ('text/cubicweb-page-template',) |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
58 |
output = 'text/html' |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
output_encoding = 'utf-8' |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
60 |
def _convert(self, trdata): |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
61 |
context = CubicWebContext() |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
62 |
appobject = trdata.appobject |
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
63 |
context.update({'self': appobject, 'rset': appobject.cw_rset, |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
64 |
'req': appobject._cw, |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
65 |
'_' : appobject._cw._, |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
66 |
'user': appobject._cw.user}) |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
67 |
output = UStringIO() |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
68 |
template = compile_template(trdata.encode(self.output_encoding)) |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
69 |
template.expand(context, output) |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
70 |
return output.getvalue() |
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
71 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
72 |
ENGINE.add_transform(ept_to_html()) |
0 | 73 |
|
74 |
if register_pil_transforms(ENGINE, verb=False): |
|
75 |
HAS_PIL_TRANSFORMS = True |
|
76 |
else: |
|
77 |
HAS_PIL_TRANSFORMS = False |
|
1584 | 78 |
|
0 | 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: |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
91 |
trdata.appobject._cw.add_css('pygments.css') |
0 | 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) |
|
1584 | 97 |
|
0 | 98 |
HAS_PYGMENTS_TRANSFORMS = True |
99 |
except ImportError: |
|
100 |
HAS_PYGMENTS_TRANSFORMS = False |
|
1584 | 101 |
|
0 | 102 |
register_base_transforms(ENGINE, verb=False) |