mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
TXTZ format: Store type of text formatting in the metadata and use it automatically when converting from TXTZ
This commit is contained in:
parent
dfa0e29927
commit
17be87e85a
@ -147,6 +147,7 @@ class TXTInput(InputFormatPlugin):
|
|||||||
|
|
||||||
# Extract content from zip archive.
|
# Extract content from zip archive.
|
||||||
if file_ext == 'txtz':
|
if file_ext == 'txtz':
|
||||||
|
options.input_encoding = 'utf-8'
|
||||||
zf = ZipFile(stream)
|
zf = ZipFile(stream)
|
||||||
zf.extractall('.')
|
zf.extractall('.')
|
||||||
|
|
||||||
@ -154,6 +155,23 @@ class TXTInput(InputFormatPlugin):
|
|||||||
if os.path.splitext(x)[1].lower() in ('.txt', '.text'):
|
if os.path.splitext(x)[1].lower() in ('.txt', '.text'):
|
||||||
with open(x, 'rb') as tf:
|
with open(x, 'rb') as tf:
|
||||||
txt += tf.read() + b'\n\n'
|
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:
|
else:
|
||||||
if getattr(stream, 'name', None):
|
if getattr(stream, 'name', None):
|
||||||
base_dir = os.path.dirname(stream.name)
|
base_dir = os.path.dirname(stream.name)
|
||||||
|
@ -158,7 +158,11 @@ class TXTZOutput(TXTOutput):
|
|||||||
|
|
||||||
# Metadata
|
# Metadata
|
||||||
with open(os.path.join(tdir, 'metadata.opf'), 'wb') as mdataf:
|
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 = ZipFile(output_path, 'w')
|
||||||
txtz.add_dir(tdir)
|
txtz.add_dir(tdir)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user