From e6eb0e26f33f09603c1842926915acdf5fec56be Mon Sep 17 00:00:00 2001 From: "Marshall T. Vandegrift" Date: Thu, 22 Jan 2009 22:49:00 -0500 Subject: [PATCH] Improve Mobipocket metadata: - Only add ISBN s - Add the book title in a way which can be found without actually processing the MOBI header --- src/calibre/ebooks/mobi/writer.py | 9 ++++++++- src/calibre/ebooks/oeb/base.py | 11 ++++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/calibre/ebooks/mobi/writer.py b/src/calibre/ebooks/mobi/writer.py index fdafd2e08b..39c77eace5 100644 --- a/src/calibre/ebooks/mobi/writer.py +++ b/src/calibre/ebooks/mobi/writer.py @@ -452,6 +452,13 @@ class MobiWriter(object): code = EXTH_CODES[term] for item in oeb.metadata[term]: data = self.COLLAPSE_RE.sub(' ', unicode(item)) + if term == 'identifier': + if data.lower().startswith('urn:isbn:'): + data = data[9:] + elif item.get('scheme', '').lower() == 'isbn': + pass + else: + continue data = data.encode('utf-8') exth.write(pack('>II', code, len(data) + 8)) exth.write(data) @@ -468,7 +475,7 @@ class MobiWriter(object): nrecs += 3 exth = exth.getvalue() trail = len(exth) % 4 - pad = '' if not trail else '\0' * (4 - trail) + pad = '\0' * (4 - trail) # Always pad w/ at least 1 byte exth = ['EXTH', pack('>II', len(exth) + 12, nrecs), exth, pad] return ''.join(exth) diff --git a/src/calibre/ebooks/oeb/base.py b/src/calibre/ebooks/oeb/base.py index 8218525a37..3336391a38 100644 --- a/src/calibre/ebooks/oeb/base.py +++ b/src/calibre/ebooks/oeb/base.py @@ -222,7 +222,16 @@ class Metadata(object): raise AttributeError( '%r object has no attribute %r' \ % (self.__class__.__name__, name)) - + + def __getitem__(self, key): + return self.attrib[key] + + def __contains__(self, key): + return key in self.attrib + + def get(self, key, default=None): + return self.attrib.get(key, default) + def __repr__(self): return 'Item(term=%r, value=%r, attrib=%r)' \ % (barename(self.term), self.value, self.attrib)