From 39701cdaa870407d1a09ea8ca242271a6b2e54a1 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Thu, 24 Oct 2013 14:31:48 +0530 Subject: [PATCH] Conversion: When converting TXT set in HTML MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit TXT Input: Ensure that <title> in the generated HTML has a meaningful value. Fixes #1236923 [txt → epub When I don't give a title, the <title> tag is set to unknown. It should default to the chapter name and chapter ID for each fragment. Or use the name of the converted file](https://bugs.launchpad.net/calibre/+bug/1236923) --- src/calibre/ebooks/conversion/plugins/txt_input.py | 9 +++++++++ src/calibre/ebooks/metadata/txt.py | 9 ++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/conversion/plugins/txt_input.py b/src/calibre/ebooks/conversion/plugins/txt_input.py index 4f73a067a6..5cab2a1a96 100644 --- a/src/calibre/ebooks/conversion/plugins/txt_input.py +++ b/src/calibre/ebooks/conversion/plugins/txt_input.py @@ -236,5 +236,14 @@ class TXTInput(InputFormatPlugin): from calibre.ebooks.oeb.transforms.metadata import meta_info_to_oeb_metadata mi = get_file_type_metadata(stream, file_ext) meta_info_to_oeb_metadata(mi, oeb.metadata, log) + self.html_postprocess_title = mi.title return oeb + + def postprocess_book(self, oeb, opts, log): + for item in oeb.spine: + if hasattr(item.data, 'xpath'): + for title in item.data.xpath('//*[local-name()="title"]'): + if title.text == _('Unknown'): + title.text = self.html_postprocess_title + diff --git a/src/calibre/ebooks/metadata/txt.py b/src/calibre/ebooks/metadata/txt.py index 70d3c72ae0..9140fb6f61 100644 --- a/src/calibre/ebooks/metadata/txt.py +++ b/src/calibre/ebooks/metadata/txt.py @@ -7,7 +7,7 @@ __copyright__ = '2009, John Schember <john@nachtimwald.com>' Read meta information from TXT files ''' -import re +import re, os from calibre.ebooks.metadata import MetaInformation @@ -15,7 +15,10 @@ def get_metadata(stream, extract_cover=True): ''' Return metadata as a L{MetaInfo} object ''' - mi = MetaInformation(_('Unknown'), [_('Unknown')]) + name = getattr(stream, 'name', '').rpartition('.')[0] + if name: + name = os.path.basename(name) + mi = MetaInformation(name or _('Unknown'), [_('Unknown')]) stream.seek(0) mdata = u'' @@ -29,7 +32,7 @@ def get_metadata(stream, extract_cover=True): mdata = mdata[:100] mo = re.search('(?u)^[ ]*(?P<title>.+)[ ]*(\n{3}|(\r\n){3}|\r{3})[ ]*(?P<author>.+)[ ]*(\n|\r\n|\r)$', mdata) - if mo != None: + if mo is not None: mi.title = mo.group('title') mi.authors = mo.group('author').split(',')