[LyX/master] docbook2epub.
Thibaut Cuvelier
tcuvelier at lyx.org
Fri Feb 5 15:05:03 UTC 2021
commit 9ad760db92a57b76fc2e131e11772c3694ce93f4
Author: Thibaut Cuvelier <tcuvelier at lyx.org>
Date: Thu Jan 28 04:57:57 2021 +0100
docbook2epub.
---
lib/scripts/docbook2epub.py | 65 +++++++++++++++++++++++++++++++++++++++++++
1 files changed, 65 insertions(+), 0 deletions(-)
diff --git a/lib/scripts/docbook2epub.py b/lib/scripts/docbook2epub.py
new file mode 100644
index 0000000..a75c458
--- /dev/null
+++ b/lib/scripts/docbook2epub.py
@@ -0,0 +1,65 @@
+# -*- 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:
+# python docbook2epub.py in.docbook out.epub
+
+from __future__ import print_function
+
+import os
+import shutil
+import sys
+import tempfile
+import zipfile
+import glob
+
+if __name__ == '__main__':
+ if len(sys.argv) != 3:
+ 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('\\', '/')
+ # os.chmod(output_dir, 0o777)
+ 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
+ command = '"' + java_path + '" -jar "' + saxon_jar + '" ' + input + ' ' + xslt + ' ' + saxon_params
+
+ print('XSLT style sheet to use:')
+ print(xslt)
+ print('Command to execute:')
+ print(command)
+
+ if os.system('"' + command + '"') != 0:
+ 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:
+ for file in glob.glob(output_dir + '/**/*', recursive=True):
+ zip.write(file, os.path.relpath(file, output_dir), compress_type=zipfile.ZIP_STORED)
+
+ shutil.rmtree(output_dir)
+ print('Generated ePub.')
More information about the lyx-cvs
mailing list