1 # -*- coding: utf-8 -*-
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
7 # \author Thibaut Cuvelier
9 # Full author contact details are available in file CREDITS
12 # python docbook2epub.py in.docbook out.epub
14 from __future__ import print_function
23 if __name__ == '__main__':
24 if len(sys.argv) != 4:
26 own_path, java_path, input, output = sys.argv
27 script_folder = os.path.dirname(own_path) + '/../'
29 print('Generating ePub:')
34 output_dir = tempfile.mkdtemp().replace('\\', '/')
35 print('Temporary output directory:')
38 # Start the XSLT transformation.
39 xslt = script_folder + 'docbook/epub3/chunk.xsl'
40 saxon_jar = script_folder + 'scripts/saxon6.5.5.jar'
41 saxon_params = 'base.dir=%s' % output_dir
42 command = '"' + java_path + '" -jar "' + saxon_jar + '" ' + input + ' ' + xslt + ' ' + saxon_params
44 print('XSLT style sheet to use:')
46 print('Command to execute:')
49 if os.system('"' + command + '"') != 0:
50 print('docbook2epub fails')
51 shutil.rmtree(output_dir, ignore_errors=True)
54 print('Generated ePub contents.')
56 # TODO: Copy the assets to the OEBPS/images/.
58 # Create the actual ePub file.
59 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
60 for file in glob.glob(output_dir + '/**/*', recursive=True):
61 zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
63 shutil.rmtree(output_dir)
64 print('Generated ePub.')