Back to index

plone3  3.1.7
rtf_to_html.py
Go to the documentation of this file.
00001 """
00002 Uses the http://freshmeat.net/projects/rtfconverter/ bin to do its handy work
00003 """
00004 
00005 from Products.PortalTransforms.interfaces import itransform
00006 from Products.PortalTransforms.libtransforms.utils import bin_search, sansext
00007 from Products.PortalTransforms.libtransforms.commandtransform import commandtransform
00008 from Products.CMFDefault.utils import bodyfinder
00009 import os
00010 
00011 class rtf_to_html(commandtransform):
00012     __implements__ = itransform
00013 
00014     __name__ = "rtf_to_html"
00015     inputs   = ('application/rtf',)
00016     output  = 'text/html'
00017 
00018     binaryName = "rtf-converter"
00019 
00020     def __init__(self):
00021         commandtransform.__init__(self, binary=self.binaryName)
00022 
00023     def convert(self, data, cache, **kwargs):
00024         kwargs['filename'] = 'unknow.rtf'
00025 
00026         tmpdir, fullname = self.initialize_tmpdir(data, **kwargs)
00027         html = self.invokeCommand(tmpdir, fullname)
00028         path, images = self.subObjects(tmpdir)
00029         objects = {}
00030         if images:
00031             self.fixImages(path, images, objects)
00032         self.cleanDir(tmpdir)
00033         cache.setData(bodyfinder(html))
00034         cache.setSubObjects(objects)
00035         return cache
00036 
00037     def invokeCommand(self, tmpdir, fullname):
00038         # FIXME: windows users...
00039         htmlfile = "%s/%s.html" % (tmpdir, sansext(fullname))
00040         cmd = 'cd "%s" && %s -o %s "%s" 2>error_log 1>/dev/null' % (
00041             tmpdir, self.binary, htmlfile, fullname)
00042         os.system(cmd)
00043         try:
00044             html = open(htmlfile).read()
00045         except:
00046             try:
00047                 return open("%s/error_log" % tmpdir, 'r').read()
00048             except:
00049                 return ''
00050         return html
00051 
00052 def register():
00053     return rtf_to_html()