]> git.lyx.org Git - features.git/blob - lib/scripts/docbook2epub.py
ePub: fix argument handling in Python script.
[features.git] / lib / scripts / docbook2epub.py
1 # -*- coding: utf-8 -*-
2
3 # file docbook2epub.py
4 # This file is part of LyX, the document processor.
5 # Licence details can be found in the file COPYING.
6 #
7 # \author Thibaut Cuvelier
8 #
9 # Full author contact details are available in file CREDITS
10
11 # Usage:
12 #   python docbook2epub.py in.docbook out.epub
13
14 from __future__ import print_function
15
16 import os
17 import shutil
18 import sys
19 import tempfile
20 import zipfile
21 import glob
22
23 if __name__ == '__main__':
24     if len(sys.argv) != 4:
25         sys.exit(1)
26     own_path, java_path, input, output = sys.argv
27     script_folder = os.path.dirname(own_path) + '/../'
28
29     print('Generating ePub:')
30     print(own_path)
31     print(input)
32     print(output)
33
34     output_dir = tempfile.mkdtemp().replace('\\', '/')
35     # os.chmod(output_dir, 0o777)
36     print('Temporary output directory:')
37     print(output_dir)
38
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
44
45     print('XSLT style sheet to use:')
46     print(xslt)
47     print('Command to execute:')
48     print(command)
49
50     if os.system('"' + command + '"') != 0:
51         print('docbook2epub fails')
52         shutil.rmtree(output_dir, ignore_errors=True)
53         sys.exit(1)
54
55     print('Generated ePub contents.')
56
57     # TODO: Copy the assets to the OEBPS/images/.
58
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)
63
64     shutil.rmtree(output_dir)
65     print('Generated ePub.')