Use short lines when serializing bookmarks

This commit is contained in:
Kovid Goyal 2019-08-06 06:29:19 +05:30
parent 563b926e4b
commit cbd40617f3
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 4 deletions

View File

@ -59,11 +59,18 @@ def serialize_annotations(annots_map):
return json_dumps(ans)
def split_lines(chunk, length=80):
pos = 0
while pos < len(chunk):
yield chunk[pos:pos+length]
pos += length
def save_annots_to_epub(path, serialized_annots):
try:
zf = open(path, 'r+b')
except IOError:
return
with zf:
serialized_annots = EPUB_FILE_TYPE_MAGIC + as_base64_bytes(serialized_annots)
serialized_annots = EPUB_FILE_TYPE_MAGIC + b'\n'.join(split_lines(as_base64_bytes(serialized_annots)))
safe_replace(zf, 'META-INF/calibre_bookmarks.txt', BytesIO(serialized_annots), add_missing=True)

View File

@ -560,15 +560,16 @@ def parse_annotations(raw):
def get_stored_annotations(container):
from calibre.ebooks.oeb.iterator.bookmarks import parse_bookmarks
raw = container.bookmark_data or b''
if not raw:
return
if raw.startswith(EPUB_FILE_TYPE_MAGIC):
raw = raw[len(EPUB_FILE_TYPE_MAGIC):]
raw = raw[len(EPUB_FILE_TYPE_MAGIC):].replace(b'\n', b'')
for annot in parse_annotations(from_base64_bytes(raw)):
yield annot
return
from calibre.ebooks.oeb.iterator.bookmarks import parse_bookmarks
for bm in parse_bookmarks(raw):
if bm['type'] == 'cfi' and isinstance(bm['pos'], unicode_type):
spine_index = (1 + bm['spine']) * 2