author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 16 Sep 2009 17:52:54 +0200 | |
branch | 3.5 |
changeset 3268 | e9f4ea71e696 |
parent 2459 | d088d0ff48a1 |
child 2817 | 0313eecc707e |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""mime type transformation engine for cubicweb, based on mtconverter |
2 |
||
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1584
diff
changeset
|
4 |
:copyright: 2001-2009 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 |
||
1132 | 18 |
from cubicweb.common.uilib import rest_publish, html_publish |
0 | 19 |
|
20 |
HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml') |
|
21 |
||
22 |
# CubicWeb specific transformations |
|
23 |
||
24 |
class rest_to_html(Transform): |
|
25 |
inputs = ('text/rest', 'text/x-rst') |
|
26 |
output = 'text/html' |
|
27 |
def _convert(self, trdata): |
|
28 |
return rest_publish(trdata.appobject, trdata.decode()) |
|
29 |
||
30 |
class html_to_html(Transform): |
|
31 |
inputs = HTML_MIMETYPES |
|
32 |
output = 'text/html' |
|
33 |
def _convert(self, trdata): |
|
34 |
return html_publish(trdata.appobject, trdata.data) |
|
1584 | 35 |
|
0 | 36 |
|
37 |
# Instantiate and configure the transformation engine |
|
38 |
||
39 |
mtconverter.UNICODE_POLICY = 'replace' |
|
40 |
||
41 |
ENGINE = TransformEngine() |
|
42 |
ENGINE.add_transform(rest_to_html()) |
|
43 |
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
|
44 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
45 |
try: |
715 | 46 |
from cubicweb.ext.tal import compile_template |
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
47 |
except ImportError: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
48 |
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
|
49 |
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
|
50 |
schema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template') |
1584 | 51 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
52 |
else: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
53 |
HAS_TAL = True |
1584 | 54 |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
55 |
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
|
56 |
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
|
57 |
output = 'text/html' |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
58 |
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
|
59 |
def _convert(self, trdata): |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
60 |
value = trdata.encode(self.output_encoding) |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
61 |
return trdata.appobject.tal_render(compile_template(value), {}) |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |
ENGINE.add_transform(ept_to_html()) |
0 | 64 |
|
65 |
if register_pil_transforms(ENGINE, verb=False): |
|
66 |
HAS_PIL_TRANSFORMS = True |
|
67 |
else: |
|
68 |
HAS_PIL_TRANSFORMS = False |
|
1584 | 69 |
|
0 | 70 |
try: |
71 |
from logilab.mtconverter.transforms import pygmentstransforms |
|
72 |
for mt in ('text/plain',) + HTML_MIMETYPES: |
|
73 |
try: |
|
74 |
pygmentstransforms.mimetypes.remove(mt) |
|
75 |
except ValueError: |
|
76 |
continue |
|
77 |
register_pygments_transforms(ENGINE, verb=False) |
|
78 |
||
79 |
def patch_convert(cls): |
|
80 |
def _convert(self, trdata, origconvert=cls._convert): |
|
81 |
try: |
|
82 |
trdata.appobject.req.add_css('pygments.css') |
|
83 |
except AttributeError: # session has no add_css, only http request |
|
84 |
pass |
|
85 |
return origconvert(self, trdata) |
|
86 |
cls._convert = _convert |
|
87 |
patch_convert(pygmentstransforms.PygmentsHTMLTransform) |
|
1584 | 88 |
|
0 | 89 |
HAS_PYGMENTS_TRANSFORMS = True |
90 |
except ImportError: |
|
91 |
HAS_PYGMENTS_TRANSFORMS = False |
|
1584 | 92 |
|
0 | 93 |
register_base_transforms(ENGINE, verb=False) |