From 17be87e85a7995803f88df23331b87f9f53eb3b3 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Mon, 5 Jul 2021 12:02:34 +0530 Subject: [PATCH] TXTZ format: Store type of text formatting in the metadata and use it automatically when converting from TXTZ --- .../ebooks/conversion/plugins/txt_input.py | 18 ++++++++++++++++++ .../ebooks/conversion/plugins/txt_output.py | 6 +++++- 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/calibre/ebooks/conversion/plugins/txt_input.py b/src/calibre/ebooks/conversion/plugins/txt_input.py index 42cc70cb17..dd2b82cfde 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_input.py +++ b/src/calibre/ebooks/conversion/plugins/txt_input.py @@ -147,6 +147,7 @@ class TXTInput(InputFormatPlugin): # Extract content from zip archive. if file_ext == 'txtz': + options.input_encoding = 'utf-8' zf = ZipFile(stream) zf.extractall('.') @@ -154,6 +155,23 @@ class TXTInput(InputFormatPlugin): if os.path.splitext(x)[1].lower() in ('.txt', '.text'): with open(x, 'rb') as tf: txt += tf.read() + b'\n\n' + if os.path.exists('metadata.opf'): + from lxml import etree + with open('metadata.opf', 'rb') as mf: + raw = mf.read() + try: + root = etree.fromstring(raw) + except Exception: + pass + else: + txt_formatting = root.find('text-formatting') + if txt_formatting is not None and txt_formatting.text: + txt_formatting = txt_formatting.text.strip() + if txt_formatting in ('plain', 'textile', 'markdown') and options.formatting_type == 'auto': + log.info(f'Using metadata from TXTZ archive to set text formmating type to: {txt_formatting}') + options.formatting_type = txt_formatting + if txt_formatting != 'plain': + options.paragraph_type = 'off' else: if getattr(stream, 'name', None): base_dir = os.path.dirname(stream.name) diff --git a/src/calibre/ebooks/conversion/plugins/txt_output.py b/src/calibre/ebooks/conversion/plugins/txt_output.py index f250f08f01..cd62a0d80a 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_output.py +++ b/src/calibre/ebooks/conversion/plugins/txt_output.py @@ -158,7 +158,11 @@ class TXTZOutput(TXTOutput): # Metadata with open(os.path.join(tdir, 'metadata.opf'), 'wb') as mdataf: - mdataf.write(xml2str(oeb_book.metadata.to_opf1(), pretty_print=True)) + root = oeb_book.metadata.to_opf1() + elem = root.makeelement('text-formatting') + elem.text = opts.txt_output_formatting + root.append(elem) + mdataf.write(xml2str(root, pretty_print=True)) txtz = ZipFile(output_path, 'w') txtz.add_dir(tdir)