Conversion: When converting TXT set <title> in HTML

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)
This commit is contained in:
Kovid Goyal 2013-10-24 14:31:48 +05:30
parent b9bfb53fa2
commit 39701cdaa8
2 changed files with 15 additions and 3 deletions

View File

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

View File

@ -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(',')