mirror of
https://github.com/kovidgoyal/calibre.git
synced 2025-07-09 03:04:10 -04:00
Use short lines when serializing bookmarks
This commit is contained in:
parent
563b926e4b
commit
cbd40617f3
@ -59,11 +59,18 @@ def serialize_annotations(annots_map):
|
|||||||
return json_dumps(ans)
|
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):
|
def save_annots_to_epub(path, serialized_annots):
|
||||||
try:
|
try:
|
||||||
zf = open(path, 'r+b')
|
zf = open(path, 'r+b')
|
||||||
except IOError:
|
except IOError:
|
||||||
return
|
return
|
||||||
with zf:
|
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)
|
safe_replace(zf, 'META-INF/calibre_bookmarks.txt', BytesIO(serialized_annots), add_missing=True)
|
||||||
|
@ -560,15 +560,16 @@ def parse_annotations(raw):
|
|||||||
|
|
||||||
|
|
||||||
def get_stored_annotations(container):
|
def get_stored_annotations(container):
|
||||||
from calibre.ebooks.oeb.iterator.bookmarks import parse_bookmarks
|
|
||||||
|
|
||||||
raw = container.bookmark_data or b''
|
raw = container.bookmark_data or b''
|
||||||
|
if not raw:
|
||||||
|
return
|
||||||
if raw.startswith(EPUB_FILE_TYPE_MAGIC):
|
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)):
|
for annot in parse_annotations(from_base64_bytes(raw)):
|
||||||
yield annot
|
yield annot
|
||||||
return
|
return
|
||||||
|
|
||||||
|
from calibre.ebooks.oeb.iterator.bookmarks import parse_bookmarks
|
||||||
for bm in parse_bookmarks(raw):
|
for bm in parse_bookmarks(raw):
|
||||||
if bm['type'] == 'cfi' and isinstance(bm['pos'], unicode_type):
|
if bm['type'] == 'cfi' and isinstance(bm['pos'], unicode_type):
|
||||||
spine_index = (1 + bm['spine']) * 2
|
spine_index = (1 + bm['spine']) * 2
|
||||||
|
Loading…
x
Reference in New Issue
Block a user