author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 06 May 2010 14:26:47 +0200 | |
branch | stable |
changeset 5494 | f3bb53f1737c |
parent 5424 | 8ecbcbff9777 |
child 5611 | 55366f5b7a9f |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""mime type transformation engine for cubicweb, based on mtconverter |
19 |
||
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
from logilab import mtconverter |
|
24 |
||
25 |
from logilab.mtconverter.engine import TransformEngine |
|
26 |
from logilab.mtconverter.transform import Transform |
|
27 |
from logilab.mtconverter import (register_base_transforms, |
|
1584 | 28 |
register_pil_transforms, |
0 | 29 |
register_pygments_transforms) |
30 |
||
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
31 |
from cubicweb.utils import UStringIO |
4023
eae23c40627a
drop common subpackage
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3418
diff
changeset
|
32 |
from cubicweb.uilib import rest_publish, html_publish |
0 | 33 |
|
34 |
HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml') |
|
35 |
||
36 |
# CubicWeb specific transformations |
|
37 |
||
38 |
class rest_to_html(Transform): |
|
39 |
inputs = ('text/rest', 'text/x-rst') |
|
40 |
output = 'text/html' |
|
41 |
def _convert(self, trdata): |
|
42 |
return rest_publish(trdata.appobject, trdata.decode()) |
|
43 |
||
44 |
class html_to_html(Transform): |
|
45 |
inputs = HTML_MIMETYPES |
|
46 |
output = 'text/html' |
|
47 |
def _convert(self, trdata): |
|
48 |
return html_publish(trdata.appobject, trdata.data) |
|
1584 | 49 |
|
0 | 50 |
|
51 |
# Instantiate and configure the transformation engine |
|
52 |
||
53 |
mtconverter.UNICODE_POLICY = 'replace' |
|
54 |
||
55 |
ENGINE = TransformEngine() |
|
56 |
ENGINE.add_transform(rest_to_html()) |
|
57 |
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
|
58 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
59 |
try: |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
60 |
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
|
61 |
except ImportError: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
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
|
63 |
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
|
64 |
schema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template') |
1584 | 65 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
66 |
else: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
67 |
HAS_TAL = True |
1584 | 68 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
69 |
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
|
70 |
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
|
71 |
output = 'text/html' |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
72 |
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
|
73 |
def _convert(self, trdata): |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
74 |
context = CubicWebContext() |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
75 |
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
|
76 |
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
|
77 |
'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
|
78 |
'_' : appobject._cw._, |
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
79 |
'user': appobject._cw.user}) |
2817
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
80 |
output = UStringIO() |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
81 |
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
|
82 |
template.expand(context, output) |
0313eecc707e
entity has no more tal_render method
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2459
diff
changeset
|
83 |
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
|
84 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
85 |
ENGINE.add_transform(ept_to_html()) |
0 | 86 |
|
87 |
if register_pil_transforms(ENGINE, verb=False): |
|
88 |
HAS_PIL_TRANSFORMS = True |
|
89 |
else: |
|
90 |
HAS_PIL_TRANSFORMS = False |
|
1584 | 91 |
|
0 | 92 |
try: |
93 |
from logilab.mtconverter.transforms import pygmentstransforms |
|
94 |
for mt in ('text/plain',) + HTML_MIMETYPES: |
|
95 |
try: |
|
96 |
pygmentstransforms.mimetypes.remove(mt) |
|
97 |
except ValueError: |
|
98 |
continue |
|
99 |
register_pygments_transforms(ENGINE, verb=False) |
|
100 |
||
101 |
def patch_convert(cls): |
|
102 |
def _convert(self, trdata, origconvert=cls._convert): |
|
103 |
try: |
|
3418
7b49fa7e942d
[api] use _cw, cw_row, cw_col, cw_rset etc.
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2817
diff
changeset
|
104 |
trdata.appobject._cw.add_css('pygments.css') |
0 | 105 |
except AttributeError: # session has no add_css, only http request |
106 |
pass |
|
107 |
return origconvert(self, trdata) |
|
108 |
cls._convert = _convert |
|
109 |
patch_convert(pygmentstransforms.PygmentsHTMLTransform) |
|
1584 | 110 |
|
0 | 111 |
HAS_PYGMENTS_TRANSFORMS = True |
112 |
except ImportError: |
|
113 |
HAS_PYGMENTS_TRANSFORMS = False |
|
1584 | 114 |
|
0 | 115 |
register_base_transforms(ENGINE, verb=False) |