| 1 |
""" |
|---|
| 2 |
Transform DocBook XML 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 import bin_search, basename, sansext |
|---|
| 10 |
from Products.PortalTransforms.libtransforms.commandtransform import commandtransform |
|---|
| 11 |
from zLOG import LOG, DEBUG, WARNING |
|---|
| 12 |
|
|---|
| 13 |
XSL_STYLESHEET = os.path.join( |
|---|
| 14 |
os.getcwd(), os.path.dirname(__file__), 'docbook/custom-xhtml.xsl') |
|---|
| 15 |
|
|---|
| 16 |
class docbook_to_html(commandtransform): |
|---|
| 17 |
__implements__ = itransform |
|---|
| 18 |
|
|---|
| 19 |
__name__ = "docbook_to_html" |
|---|
| 20 |
inputs = ('application/docbook+xml',) |
|---|
| 21 |
output = 'text/html' |
|---|
| 22 |
|
|---|
| 23 |
binaryName = "xsltproc" |
|---|
| 24 |
|
|---|
| 25 |
def __init__(self): |
|---|
| 26 |
commandtransform.__init__(self, binary=self.binaryName) |
|---|
| 27 |
|
|---|
| 28 |
def convert(self, data, cache, **kwargs): |
|---|
| 29 |
kwargs['filename'] = basename((kwargs.get('filename') or 'unknown.docb.xml')) |
|---|
| 30 |
|
|---|
| 31 |
tmpdir, fullname = self.initialize_tmpdir(data, **kwargs) |
|---|
| 32 |
|
|---|
| 33 |
subfiles_dict = kwargs.get('subobjects', {}) |
|---|
| 34 |
for k, v in subfiles_dict.items(): |
|---|
| 35 |
subfile_name = k |
|---|
| 36 |
tmp_image_dir_path = os.path.join(tmpdir, 'images') |
|---|
| 37 |
if not os.path.exists(tmp_image_dir_path): |
|---|
| 38 |
os.mkdir(tmp_image_dir_path) |
|---|
| 39 |
subfile_path = os.path.join(tmp_image_dir_path, subfile_name) |
|---|
| 40 |
subfile = open(subfile_path, 'w+c') |
|---|
| 41 |
subfile.write(str(v)) |
|---|
| 42 |
subfile.close() |
|---|
| 43 |
|
|---|
| 44 |
html = self.invokeCommand(tmpdir, fullname) |
|---|
| 45 |
if sys.platform == 'win32': |
|---|
| 46 |
html = html.replace('img src="images\\', 'img src="') |
|---|
| 47 |
else: |
|---|
| 48 |
html = html.replace('img src="images/', 'img src="') |
|---|
| 49 |
try: |
|---|
| 50 |
path, images = self.subObjects(os.path.join(tmpdir, |
|---|
| 51 |
'images')) |
|---|
| 52 |
except OSError, e: |
|---|
| 53 |
LOG('docbook_to_html.convert', WARNING, 'OSError: %s' % e) |
|---|
| 54 |
path, images = '', [] |
|---|
| 55 |
objects = {} |
|---|
| 56 |
if images: |
|---|
| 57 |
self.fixImages(path, images, objects) |
|---|
| 58 |
self.cleanDir(tmpdir) |
|---|
| 59 |
cache.setData(html) |
|---|
| 60 |
cache.setSubObjects(objects) |
|---|
| 61 |
return cache |
|---|
| 62 |
|
|---|
| 63 |
def invokeCommand(self, tmpdir, fullname): |
|---|
| 64 |
if sys.platform == 'win32': |
|---|
| 65 |
cmd = '%s --novalid "%s" "%s" > "%s"' % ( |
|---|
| 66 |
self.binaryName, XSL_STYLESHEET, fullname, |
|---|
| 67 |
os.path.join(tmpdir, sansext(fullname)+'.html')) |
|---|
| 68 |
else: |
|---|
| 69 |
cmd = ('cd "%s" && %s --novalid %s %s >"%s.html" 2>"%s.log-xsltproc"' |
|---|
| 70 |
% (tmpdir, self.binary, XSL_STYLESHEET, fullname, |
|---|
| 71 |
sansext(fullname), sansext(fullname))) |
|---|
| 72 |
LOG(self.__name__, DEBUG, "cmd = %s" % cmd) |
|---|
| 73 |
os.system(cmd) |
|---|
| 74 |
try: |
|---|
| 75 |
htmlfile = open(os.path.join(tmpdir, sansext(fullname)+'.html')) |
|---|
| 76 |
html = htmlfile.read() |
|---|
| 77 |
htmlfile.close() |
|---|
| 78 |
except: |
|---|
| 79 |
try: |
|---|
| 80 |
return open(os.path.join(tmpdir, 'error_log')).read() |
|---|
| 81 |
except: |
|---|
| 82 |
return '' |
|---|
| 83 |
return html |
|---|
| 84 |
|
|---|
| 85 |
def register(): |
|---|
| 86 |
return docbook_to_html() |
|---|