TXTZ format: Store type of text formatting in the metadata and use it automatically when converting from TXTZ

This commit is contained in:
Kovid Goyal 2021-07-05 12:02:34 +05:30
parent dfa0e29927
commit 17be87e85a
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 23 additions and 1 deletions

View File

@ -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)

View File

@ -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)