From 0c25a25eb4617f501843391bd79d203bbf513950 Mon Sep 17 00:00:00 2001 From: Oliver Graf Date: Sat, 28 Jul 2012 20:34:26 +0200 Subject: [PATCH 1/2] Fix to MOBI 'Unknown' problem (replacing all empty strings with the string Unknown) --- src/calibre/ebooks/mobi/utils.py | 7 +++++-- src/calibre/ebooks/mobi/writer2/serializer.py | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/calibre/ebooks/mobi/utils.py b/src/calibre/ebooks/mobi/utils.py index ae8e583a1b..dfdb73fdff 100644 --- a/src/calibre/ebooks/mobi/utils.py +++ b/src/calibre/ebooks/mobi/utils.py @@ -302,7 +302,7 @@ def encode_tbs(val, extra, flag_size=4): ans += encint(extra[0b0001]) return ans -def utf8_text(text): +def utf8_text(text, empty=False): ''' Convert a possibly null string to utf-8 bytes, guaranteeing to return a non empty, normalized bytestring. @@ -313,7 +313,10 @@ def utf8_text(text): text = text.decode('utf-8', 'replace') text = normalize(text).encode('utf-8') else: - text = _('Unknown').encode('utf-8') + if not empty: + text = _('Unknown').encode('utf-8') + else: + text = u''.encode('utf-8') # yeah, stupid return text def align_block(raw, multiple=4, pad=b'\0'): diff --git a/src/calibre/ebooks/mobi/writer2/serializer.py b/src/calibre/ebooks/mobi/writer2/serializer.py index 5251bf934f..87b42cd601 100644 --- a/src/calibre/ebooks/mobi/writer2/serializer.py +++ b/src/calibre/ebooks/mobi/writer2/serializer.py @@ -355,7 +355,7 @@ class Serializer(object): text = text.replace(u'\u00AD', '') # Soft-hyphen if quot: text = text.replace('"', '"') - self.buf.write(utf8_text(text)) + self.buf.write(utf8_text(text, empty=True)) def fixup_links(self): ''' From 45deedd546aba851beadf46f8eb0abb046dac2e3 Mon Sep 17 00:00:00 2001 From: Oliver Graf Date: Sun, 29 Jul 2012 08:26:29 +0200 Subject: [PATCH 2/2] Removed uneeded utf8_text, after trunk fix. --- src/calibre/ebooks/mobi/utils.py | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/calibre/ebooks/mobi/utils.py b/src/calibre/ebooks/mobi/utils.py index dfdb73fdff..ae8e583a1b 100644 --- a/src/calibre/ebooks/mobi/utils.py +++ b/src/calibre/ebooks/mobi/utils.py @@ -302,7 +302,7 @@ def encode_tbs(val, extra, flag_size=4): ans += encint(extra[0b0001]) return ans -def utf8_text(text, empty=False): +def utf8_text(text): ''' Convert a possibly null string to utf-8 bytes, guaranteeing to return a non empty, normalized bytestring. @@ -313,10 +313,7 @@ def utf8_text(text, empty=False): text = text.decode('utf-8', 'replace') text = normalize(text).encode('utf-8') else: - if not empty: - text = _('Unknown').encode('utf-8') - else: - text = u''.encode('utf-8') # yeah, stupid + text = _('Unknown').encode('utf-8') return text def align_block(raw, multiple=4, pad=b'\0'):