goa/overrides/mttransforms.py
branchstable
changeset 6340 470d8e828fda
parent 6339 bdc3dc94d744
child 6341 ad5e08981153
equal deleted inserted replaced
6339:bdc3dc94d744 6340:470d8e828fda
     1 # copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved.
       
     2 # contact http://www.logilab.fr/ -- mailto:contact@logilab.fr
       
     3 #
       
     4 # This file is part of CubicWeb.
       
     5 #
       
     6 # CubicWeb is free software: you can redistribute it and/or modify it under the
       
     7 # terms of the GNU Lesser General Public License as published by the Free
       
     8 # Software Foundation, either version 2.1 of the License, or (at your option)
       
     9 # any later version.
       
    10 #
       
    11 # CubicWeb is distributed in the hope that it will be useful, but WITHOUT
       
    12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
       
    13 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
       
    14 # details.
       
    15 #
       
    16 # You should have received a copy of the GNU Lesser General Public License along
       
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
       
    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 cubicweb.uilib import rest_publish, html_publish, remove_html_tags
       
    28 
       
    29 HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml')
       
    30 # CubicWeb specific transformations
       
    31 
       
    32 class rest_to_html(Transform):
       
    33     inputs = ('text/rest', 'text/x-rst')
       
    34     output = 'text/html'
       
    35     def _convert(self, trdata):
       
    36         return rest_publish(trdata.appobject, trdata.decode())
       
    37 
       
    38 class html_to_html(Transform):
       
    39     inputs = HTML_MIMETYPES
       
    40     output = 'text/html'
       
    41     def _convert(self, trdata):
       
    42         return html_publish(trdata.appobject, trdata.data)
       
    43 
       
    44 
       
    45 # Instantiate and configure the transformation engine
       
    46 
       
    47 mtconverter.UNICODE_POLICY = 'replace'
       
    48 
       
    49 ENGINE = TransformEngine()
       
    50 ENGINE.add_transform(rest_to_html())
       
    51 ENGINE.add_transform(html_to_html())
       
    52 
       
    53 HAS_PIL_TRANSFORMS = False
       
    54 HAS_PYGMENTS_TRANSFORMS = False
       
    55 
       
    56 class html_to_text(Transform):
       
    57     inputs = HTML_MIMETYPES
       
    58     output = 'text/plain'
       
    59     def _convert(self, trdata):
       
    60         return remove_html_tags(trdata.data)
       
    61 ENGINE.add_transform(html_to_text())