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 # os.chmod(output_dir, 0o777)
36 print('Temporary output directory:')
39 # Start the XSLT transformation.
40 xslt = script_folder + 'docbook/epub3/chunk.xsl'
41 saxon_jar = script_folder + 'scripts/saxon6.5.5.jar'
42 saxon_params = 'base.dir=%s' % output_dir
43 command = '"' + java_path + '" -jar "' + saxon_jar + '" ' + input + ' ' + xslt + ' ' + saxon_params
45 print('XSLT style sheet to use:')
47 print('Command to execute:')
50 if os.system('"' + command + '"') != 0:
51 print('docbook2epub fails')
52 shutil.rmtree(output_dir, ignore_errors=True)
55 print('Generated ePub contents.')
57 # TODO: Copy the assets to the OEBPS/images/.
59 # Create the actual ePub file.
60 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
61 for file in glob.glob(output_dir + '/**/*', recursive=True):
62 zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
64 shutil.rmtree(output_dir)
65 print('Generated ePub.')