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 java_binary in.docbook out.epub
14 from __future__ import print_function
16 # import glob # Not powerful enough before Python 3.5.
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 quoted_command = command
51 # On Windows, it is typical to have spaces in folder names, and that requires to wrap the whole command
52 # in quotes. On Linux, this might create errors when starting the command.
53 quoted_command = '"' + command + '"'
54 # This could be simplified by using subprocess.run, but this requires Python 3.5.
56 if os.system(quoted_command) != 0:
57 print('docbook2epub fails')
58 shutil.rmtree(output_dir, ignore_errors=True)
61 print('Generated ePub contents.')
63 # TODO: Copy the assets to the OEBPS/images/.
65 # Create the actual ePub file.
66 with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
67 # Python 3.5 brings the `recursive` argument. For older versions, this trick is required...
68 # for file in glob.glob(output_dir + '/**/*', recursive=True):
69 for file in [os.path.join(dp, f) for dp, dn, filenames in os.walk(output_dir) for f in filenames]:
70 zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
72 shutil.rmtree(output_dir)
73 print('Generated ePub.')