author | sylvain.thenault@logilab.fr |
Tue, 10 Mar 2009 19:35:38 +0100 | |
branch | tls-sprint |
changeset 1036 | 593df4919845 |
parent 715 | 83228d379cbe |
child 1132 | 96752791c2b6 |
permissions | -rw-r--r-- |
0 | 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 logilab.mtconverter import (register_base_transforms, |
|
14 |
register_pil_transforms, |
|
15 |
register_pygments_transforms) |
|
16 |
||
17 |
from cubicweb.common.uilib import rest_publish, html_publish, remove_html_tags |
|
18 |
||
19 |
HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml') |
|
20 |
||
21 |
# CubicWeb specific transformations |
|
22 |
||
23 |
class rest_to_html(Transform): |
|
24 |
inputs = ('text/rest', 'text/x-rst') |
|
25 |
output = 'text/html' |
|
26 |
def _convert(self, trdata): |
|
27 |
return rest_publish(trdata.appobject, trdata.decode()) |
|
28 |
||
29 |
class html_to_html(Transform): |
|
30 |
inputs = HTML_MIMETYPES |
|
31 |
output = 'text/html' |
|
32 |
def _convert(self, trdata): |
|
33 |
return html_publish(trdata.appobject, trdata.data) |
|
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
34 |
|
0 | 35 |
|
36 |
# Instantiate and configure the transformation engine |
|
37 |
||
38 |
mtconverter.UNICODE_POLICY = 'replace' |
|
39 |
||
40 |
ENGINE = TransformEngine() |
|
41 |
ENGINE.add_transform(rest_to_html()) |
|
42 |
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
|
43 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
44 |
try: |
715 | 45 |
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
|
46 |
except ImportError: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
47 |
HAS_TAL = False |
715 | 48 |
from cubicweb.schema import FormatConstraint |
49 |
FormatConstraint.need_perm_formats.remove('text/cubicweb-page-template') |
|
50 |
||
704
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
51 |
else: |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
52 |
HAS_TAL = True |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
53 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
54 |
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
|
55 |
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
|
56 |
output = 'text/html' |
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
57 |
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
|
58 |
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
|
59 |
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
|
60 |
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
|
61 |
|
0c2c8f0a6ded
new ext package for modules depending on an option third party package
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
62 |
ENGINE.add_transform(ept_to_html()) |
0 | 63 |
|
64 |
if register_pil_transforms(ENGINE, verb=False): |
|
65 |
HAS_PIL_TRANSFORMS = True |
|
66 |
else: |
|
67 |
HAS_PIL_TRANSFORMS = False |
|
68 |
||
69 |
try: |
|
70 |
from logilab.mtconverter.transforms import pygmentstransforms |
|
71 |
for mt in ('text/plain',) + HTML_MIMETYPES: |
|
72 |
try: |
|
73 |
pygmentstransforms.mimetypes.remove(mt) |
|
74 |
except ValueError: |
|
75 |
continue |
|
76 |
register_pygments_transforms(ENGINE, verb=False) |
|
77 |
||
78 |
def patch_convert(cls): |
|
79 |
def _convert(self, trdata, origconvert=cls._convert): |
|
80 |
try: |
|
81 |
trdata.appobject.req.add_css('pygments.css') |
|
82 |
except AttributeError: # session has no add_css, only http request |
|
83 |
pass |
|
84 |
return origconvert(self, trdata) |
|
85 |
cls._convert = _convert |
|
86 |
patch_convert(pygmentstransforms.PygmentsHTMLTransform) |
|
87 |
||
88 |
HAS_PYGMENTS_TRANSFORMS = True |
|
89 |
except ImportError: |
|
90 |
HAS_PYGMENTS_TRANSFORMS = False |
|
91 |
||
92 |
register_base_transforms(ENGINE, verb=False) |