2021-01-28 03:57:57 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# file docbook2epub.py
|
|
|
|
# This file is part of LyX, the document processor.
|
|
|
|
# Licence details can be found in the file COPYING.
|
|
|
|
#
|
|
|
|
# \author Thibaut Cuvelier
|
|
|
|
#
|
|
|
|
# Full author contact details are available in file CREDITS
|
|
|
|
|
|
|
|
# Usage:
|
2021-02-07 05:32:46 +00:00
|
|
|
# python docbook2epub.py java_binary in.docbook out.epub
|
2021-01-28 03:57:57 +00:00
|
|
|
|
|
|
|
from __future__ import print_function
|
|
|
|
|
2021-02-07 05:32:15 +00:00
|
|
|
# import glob # Not powerful enough before Python 3.5.
|
2021-01-28 03:57:57 +00:00
|
|
|
import os
|
|
|
|
import shutil
|
|
|
|
import sys
|
|
|
|
import tempfile
|
|
|
|
import zipfile
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2021-02-07 00:55:29 +00:00
|
|
|
if len(sys.argv) != 4:
|
2021-01-28 03:57:57 +00:00
|
|
|
sys.exit(1)
|
|
|
|
own_path, java_path, input, output = sys.argv
|
|
|
|
script_folder = os.path.dirname(own_path) + '/../'
|
|
|
|
|
|
|
|
print('Generating ePub:')
|
|
|
|
print(own_path)
|
|
|
|
print(input)
|
|
|
|
print(output)
|
|
|
|
|
|
|
|
output_dir = tempfile.mkdtemp().replace('\\', '/')
|
|
|
|
print('Temporary output directory:')
|
|
|
|
print(output_dir)
|
|
|
|
|
|
|
|
# Start the XSLT transformation.
|
|
|
|
xslt = script_folder + 'docbook/epub3/chunk.xsl'
|
|
|
|
saxon_jar = script_folder + 'scripts/saxon6.5.5.jar'
|
|
|
|
saxon_params = 'base.dir=%s' % output_dir
|
2021-02-07 11:42:17 +00:00
|
|
|
command = '"' + java_path + '" -jar "' + saxon_jar + '" "' + input + '" "' + xslt + '" "' + saxon_params + '"'
|
2021-01-28 03:57:57 +00:00
|
|
|
|
|
|
|
print('XSLT style sheet to use:')
|
|
|
|
print(xslt)
|
|
|
|
print('Command to execute:')
|
|
|
|
print(command)
|
|
|
|
|
2021-02-07 05:27:07 +00:00
|
|
|
quoted_command = command
|
|
|
|
if os.name == 'nt':
|
|
|
|
# On Windows, it is typical to have spaces in folder names, and that requires to wrap the whole command
|
|
|
|
# in quotes. On Linux, this might create errors when starting the command.
|
|
|
|
quoted_command = '"' + command + '"'
|
2021-02-07 05:35:42 +00:00
|
|
|
# This could be simplified by using subprocess.run, but this requires Python 3.5.
|
2021-02-07 05:27:07 +00:00
|
|
|
|
|
|
|
if os.system(quoted_command) != 0:
|
2021-01-28 03:57:57 +00:00
|
|
|
print('docbook2epub fails')
|
|
|
|
shutil.rmtree(output_dir, ignore_errors=True)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
print('Generated ePub contents.')
|
|
|
|
|
|
|
|
# TODO: Copy the assets to the OEBPS/images/.
|
|
|
|
|
|
|
|
# Create the actual ePub file.
|
|
|
|
with zipfile.ZipFile(output, 'w', zipfile.ZIP_DEFLATED) as zip:
|
2021-02-07 05:32:15 +00:00
|
|
|
# Python 3.5 brings the `recursive` argument. For older versions, this trick is required...
|
|
|
|
# for file in glob.glob(output_dir + '/**/*', recursive=True):
|
|
|
|
for file in [os.path.join(dp, f) for dp, dn, filenames in os.walk(output_dir) for f in filenames]:
|
2021-01-28 03:57:57 +00:00
|
|
|
zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
|
|
|
|
|
|
|
|
shutil.rmtree(output_dir)
|
|
|
|
print('Generated ePub.')
|