mttransforms.py
changeset 11057 0b59724cb3f2
parent 11052 058bb3dc685f
child 11058 23eb30449fe5
equal deleted inserted replaced
11052:058bb3dc685f 11057:0b59724cb3f2
     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 __docformat__ = "restructuredtext en"
       
    21 
       
    22 from logilab import mtconverter
       
    23 
       
    24 from logilab.mtconverter.engine import TransformEngine
       
    25 from logilab.mtconverter.transform import Transform
       
    26 from logilab.mtconverter import (register_base_transforms,
       
    27                                  register_pil_transforms,
       
    28                                  register_pygments_transforms)
       
    29 
       
    30 from cubicweb.utils import UStringIO
       
    31 from cubicweb.uilib import rest_publish, markdown_publish, html_publish
       
    32 
       
    33 HTML_MIMETYPES = ('text/html', 'text/xhtml', 'application/xhtml+xml')
       
    34 
       
    35 # CubicWeb specific transformations
       
    36 
       
    37 class rest_to_html(Transform):
       
    38     inputs = ('text/rest', 'text/x-rst')
       
    39     output = 'text/html'
       
    40     def _convert(self, trdata):
       
    41         return rest_publish(trdata.appobject, trdata.decode())
       
    42 
       
    43 class markdown_to_html(Transform):
       
    44     inputs = ('text/markdown', 'text/x-markdown')
       
    45     output = 'text/html'
       
    46     def _convert(self, trdata):
       
    47         return markdown_publish(trdata.appobject, trdata.decode())
       
    48 
       
    49 class html_to_html(Transform):
       
    50     inputs = HTML_MIMETYPES
       
    51     output = 'text/html'
       
    52     def _convert(self, trdata):
       
    53         return html_publish(trdata.appobject, trdata.data)
       
    54 
       
    55 
       
    56 # Instantiate and configure the transformation engine
       
    57 
       
    58 mtconverter.UNICODE_POLICY = 'replace'
       
    59 
       
    60 ENGINE = TransformEngine()
       
    61 ENGINE.add_transform(rest_to_html())
       
    62 ENGINE.add_transform(markdown_to_html())
       
    63 ENGINE.add_transform(html_to_html())
       
    64 
       
    65 try:
       
    66     from cubicweb.ext.tal import CubicWebContext, compile_template
       
    67 except ImportError:
       
    68     HAS_TAL = False
       
    69     from cubicweb import schema
       
    70     schema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template')
       
    71 
       
    72 else:
       
    73     HAS_TAL = True
       
    74 
       
    75     class ept_to_html(Transform):
       
    76         inputs = ('text/cubicweb-page-template',)
       
    77         output = 'text/html'
       
    78         output_encoding = 'utf-8'
       
    79         def _convert(self, trdata):
       
    80             context = CubicWebContext()
       
    81             appobject = trdata.appobject
       
    82             context.update({'self': appobject, 'rset': appobject.cw_rset,
       
    83                             'req': appobject._cw,
       
    84                             '_' : appobject._cw._,
       
    85                             'user': appobject._cw.user})
       
    86             output = UStringIO()
       
    87             template = compile_template(trdata.encode(self.output_encoding))
       
    88             template.expand(context, output)
       
    89             return output.getvalue()
       
    90 
       
    91     ENGINE.add_transform(ept_to_html())
       
    92 
       
    93 if register_pil_transforms(ENGINE, verb=False):
       
    94     HAS_PIL_TRANSFORMS = True
       
    95 else:
       
    96     HAS_PIL_TRANSFORMS = False
       
    97 
       
    98 try:
       
    99     from logilab.mtconverter.transforms import pygmentstransforms
       
   100     for mt in ('text/plain',) + HTML_MIMETYPES:
       
   101         try:
       
   102             pygmentstransforms.mimetypes.remove(mt)
       
   103         except ValueError:
       
   104             continue
       
   105     register_pygments_transforms(ENGINE, verb=False)
       
   106 
       
   107     def patch_convert(cls):
       
   108         def _convert(self, trdata, origconvert=cls._convert):
       
   109             add_css = getattr(trdata.appobject._cw, 'add_css', None)
       
   110             if add_css is not None:
       
   111                 # session has no add_css, only http request
       
   112                 add_css('pygments.css')
       
   113             return origconvert(self, trdata)
       
   114         cls._convert = _convert
       
   115     patch_convert(pygmentstransforms.PygmentsHTMLTransform)
       
   116 
       
   117     HAS_PYGMENTS_TRANSFORMS = True
       
   118 except ImportError:
       
   119     HAS_PYGMENTS_TRANSFORMS = False
       
   120 
       
   121 register_base_transforms(ENGINE, verb=False)