| 1 |
""" |
|---|
| 2 |
Transform OOo file to HTML through XSL |
|---|
| 3 |
""" |
|---|
| 4 |
# $Id$ |
|---|
| 5 |
import os |
|---|
| 6 |
import sys |
|---|
| 7 |
|
|---|
| 8 |
from Products.PortalTransforms.interfaces import itransform |
|---|
| 9 |
from Products.PortalTransforms.libtransforms.utils \ |
|---|
| 10 |
import basename, sansext |
|---|
| 11 |
from Products.PortalTransforms.libtransforms.commandtransform \ |
|---|
| 12 |
import commandtransform |
|---|
| 13 |
from zLOG import LOG, DEBUG, WARNING |
|---|
| 14 |
|
|---|
| 15 |
XSL_STYLESHEET = os.path.join( |
|---|
| 16 |
os.getcwd(), os.path.dirname(__file__), 'sx2ml', 'main_html.xsl') |
|---|
| 17 |
|
|---|
| 18 |
class ooo_to_html(commandtransform): |
|---|
| 19 |
__implements__ = itransform |
|---|
| 20 |
|
|---|
| 21 |
__name__ = 'ooo_to_html' |
|---|
| 22 |
inputs = ('application/vnd.sun.xml.writer', |
|---|
| 23 |
'application/vnd.sun.xml.impress', |
|---|
| 24 |
'application/vnd.sun.xml.calc', |
|---|
| 25 |
'application/vnd.sun.xml.writer.template', |
|---|
| 26 |
'application/vnd.sun.xml.impress.template', |
|---|
| 27 |
'application/vnd.sun.xml.calc.template', |
|---|
| 28 |
) |
|---|
| 29 |
output = 'text/html' |
|---|
| 30 |
|
|---|
| 31 |
def convert(self, data, cache, **kwargs): |
|---|
| 32 |
kwargs['filename'] = basename((kwargs.get('filename') or 'unknown.pdf')) |
|---|
| 33 |
|
|---|
| 34 |
tmpdir, fullname = self.initialize_tmpdir(data, **kwargs) |
|---|
| 35 |
html = self.invokeCommand(tmpdir, fullname) |
|---|
| 36 |
|
|---|
| 37 |
subObjectsPaths = [tmpdir, os.path.join(tmpdir, 'Pictures')] |
|---|
| 38 |
for subObjectsPath in subObjectsPaths: |
|---|
| 39 |
if os.path.exists(subObjectsPath): |
|---|
| 40 |
path, images = self.subObjects(subObjectsPath) |
|---|
| 41 |
objects = {} |
|---|
| 42 |
if images: |
|---|
| 43 |
self.fixImages(path, images, objects) |
|---|
| 44 |
|
|---|
| 45 |
self.cleanDir(tmpdir) |
|---|
| 46 |
cache.setData(html) |
|---|
| 47 |
cache.setSubObjects(objects) |
|---|
| 48 |
return cache |
|---|
| 49 |
|
|---|
| 50 |
def invokeCommand(self, tmpdir, fullname): |
|---|
| 51 |
if sys.platform == 'win32': |
|---|
| 52 |
cmd = 'unzip %s -d %s' % (fullname, tmpdir) |
|---|
| 53 |
else: |
|---|
| 54 |
cmd = 'cd "%s" && unzip %s 2>error_log 1>/dev/null' % ( |
|---|
| 55 |
tmpdir, fullname) |
|---|
| 56 |
os.system(cmd) |
|---|
| 57 |
if sys.platform == 'win32': |
|---|
| 58 |
cmd = 'xsltproc --novalid "%s" "%s" > "%s"' % ( |
|---|
| 59 |
XSL_STYLESHEET, |
|---|
| 60 |
os.path.join(tmpdir, 'content.xml'), |
|---|
| 61 |
os.path.join(tmpdir, sansext(fullname)+'.html')) |
|---|
| 62 |
else: |
|---|
| 63 |
cmd = ('cd "%s" && xsltproc --novalid %s content.xml >"%s.html" ' |
|---|
| 64 |
'2>"%s.log-xsltproc"') % ( |
|---|
| 65 |
tmpdir, XSL_STYLESHEET, sansext(fullname), sansext(fullname)) |
|---|
| 66 |
LOG(self.__name__, DEBUG, "cmd = %s" % cmd) |
|---|
| 67 |
os.system(cmd) |
|---|
| 68 |
try: |
|---|
| 69 |
htmlfile = open(os.path.join(tmpdir, "%s.html" % sansext(fullname)), |
|---|
| 70 |
'r') |
|---|
| 71 |
html = htmlfile.read() |
|---|
| 72 |
htmlfile.close() |
|---|
| 73 |
except: |
|---|
| 74 |
try: |
|---|
| 75 |
return open(os.path.join(tmpdir, 'error_log'), 'r').read() |
|---|
| 76 |
except: |
|---|
| 77 |
return '' |
|---|
| 78 |
return html |
|---|
| 79 |
|
|---|
| 80 |
def register(): |
|---|
| 81 |
return ooo_to_html() |
|---|