"""mime type transformation engine for cubicweb, based on mtconverter:organization: Logilab:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2.:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses"""__docformat__="restructuredtext en"fromlogilabimportmtconverterfromlogilab.mtconverter.engineimportTransformEnginefromlogilab.mtconverter.transformimportTransformfromlogilab.mtconverterimport(register_base_transforms,register_pil_transforms,register_pygments_transforms)fromcubicweb.common.uilibimportrest_publish,html_publishHTML_MIMETYPES=('text/html','text/xhtml','application/xhtml+xml')# CubicWeb specific transformationsclassrest_to_html(Transform):inputs=('text/rest','text/x-rst')output='text/html'def_convert(self,trdata):returnrest_publish(trdata.appobject,trdata.decode())classhtml_to_html(Transform):inputs=HTML_MIMETYPESoutput='text/html'def_convert(self,trdata):returnhtml_publish(trdata.appobject,trdata.data)# Instantiate and configure the transformation enginemtconverter.UNICODE_POLICY='replace'ENGINE=TransformEngine()ENGINE.add_transform(rest_to_html())ENGINE.add_transform(html_to_html())try:fromcubicweb.ext.talimportcompile_templateexceptImportError:HAS_TAL=Falsefromcubicwebimportschemaschema.NEED_PERM_FORMATS.remove('text/cubicweb-page-template')else:HAS_TAL=Trueclassept_to_html(Transform):inputs=('text/cubicweb-page-template',)output='text/html'output_encoding='utf-8'def_convert(self,trdata):value=trdata.encode(self.output_encoding)returntrdata.appobject.tal_render(compile_template(value),{})ENGINE.add_transform(ept_to_html())ifregister_pil_transforms(ENGINE,verb=False):HAS_PIL_TRANSFORMS=Trueelse:HAS_PIL_TRANSFORMS=Falsetry:fromlogilab.mtconverter.transformsimportpygmentstransformsformtin('text/plain',)+HTML_MIMETYPES:try:pygmentstransforms.mimetypes.remove(mt)exceptValueError:continueregister_pygments_transforms(ENGINE,verb=False)defpatch_convert(cls):def_convert(self,trdata,origconvert=cls._convert):try:trdata.appobject.req.add_css('pygments.css')exceptAttributeError:# session has no add_css, only http requestpassreturnorigconvert(self,trdata)cls._convert=_convertpatch_convert(pygmentstransforms.PygmentsHTMLTransform)HAS_PYGMENTS_TRANSFORMS=TrueexceptImportError:HAS_PYGMENTS_TRANSFORMS=Falseregister_base_transforms(ENGINE,verb=False)