common/mttransforms.py
author sylvain.thenault@logilab.fr
Wed, 06 May 2009 09:46:36 +0200
branchtls-sprint
changeset 1702 312310ed8a2f
parent 1584 323eed40a2b7
child 1977 606923dff11b
permissions -rw-r--r--
cleanup
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     1
"""mime type transformation engine for cubicweb, based on mtconverter
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     2
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     3
:organization: Logilab
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 715
diff changeset
     4
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     5
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     6
"""
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     7
__docformat__ = "restructuredtext en"
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     8
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
     9
from logilab import mtconverter
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    10
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    11
from logilab.mtconverter.engine import TransformEngine
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    12
from logilab.mtconverter.transform import Transform
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    13
from logilab.mtconverter import (register_base_transforms,
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    14
                                 register_pil_transforms,
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    15
                                 register_pygments_transforms)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    16
1132
96752791c2b6 pylint cleanup
sylvain.thenault@logilab.fr
parents: 715
diff changeset
    17
from cubicweb.common.uilib import rest_publish, html_publish
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    18
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    19
HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    20
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    21
# CubicWeb specific transformations
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    22
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    23
class rest_to_html(Transform):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    24
    inputs = ('text/rest', 'text/x-rst')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    25
    output = 'text/html'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    26
    def _convert(self, trdata):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    27
        return rest_publish(trdata.appobject, trdata.decode())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    28
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    29
class html_to_html(Transform):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    30
    inputs = HTML_MIMETYPES
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    31
    output = 'text/html'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    32
    def _convert(self, trdata):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    33
        return html_publish(trdata.appobject, trdata.data)
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    34
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    35
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    36
# Instantiate and configure the transformation engine
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    37
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    38
mtconverter.UNICODE_POLICY = 'replace'
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    39
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    40
ENGINE = TransformEngine()
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    41
ENGINE.add_transform(rest_to_html())
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    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
83228d379cbe do it the other way
sylvain.thenault@logilab.fr
parents: 704
diff changeset
    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
83228d379cbe do it the other way
sylvain.thenault@logilab.fr
parents: 704
diff changeset
    48
    from cubicweb.schema import FormatConstraint
83228d379cbe do it the other way
sylvain.thenault@logilab.fr
parents: 704
diff changeset
    49
    FormatConstraint.need_perm_formats.remove('text/cubicweb-page-template')
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    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
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    53
704
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
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    63
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    64
if register_pil_transforms(ENGINE, verb=False):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    65
    HAS_PIL_TRANSFORMS = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    66
else:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    67
    HAS_PIL_TRANSFORMS = False
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    68
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    69
try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    70
    from logilab.mtconverter.transforms import pygmentstransforms
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    71
    for mt in ('text/plain',) + HTML_MIMETYPES:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    72
        try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    73
            pygmentstransforms.mimetypes.remove(mt)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    74
        except ValueError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    75
            continue
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    76
    register_pygments_transforms(ENGINE, verb=False)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    77
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    78
    def patch_convert(cls):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    79
        def _convert(self, trdata, origconvert=cls._convert):
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    80
            try:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    81
                trdata.appobject.req.add_css('pygments.css')
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    82
            except AttributeError: # session has no add_css, only http request
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    83
                pass
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    84
            return origconvert(self, trdata)
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    85
        cls._convert = _convert
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    86
    patch_convert(pygmentstransforms.PygmentsHTMLTransform)
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    87
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    88
    HAS_PYGMENTS_TRANSFORMS = True
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    89
except ImportError:
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    90
    HAS_PYGMENTS_TRANSFORMS = False
1584
323eed40a2b7 delete-trailing-whitespaces
sylvain.thenault@logilab.fr
parents: 1132
diff changeset
    91
0
b97547f5f1fa Showtime !
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
diff changeset
    92
register_base_transforms(ENGINE, verb=False)