Files

32 lines
979 B
Python

from .models import Page
def reading_frames(chapter, pages):
"""Group ordered uploads into stable reader turns without altering images."""
frames = []
pending = []
def flush():
if pending:
frames.append({'kind': 'pair' if len(pending) == 2 else 'single', 'pages': pending[:]})
pending.clear()
if chapter.cover:
frames.append({'kind': 'cover', 'pages': []})
for index, page in enumerate(pages):
if index == 0 and not chapter.cover and chapter.first_upload_is_cover:
frames.append({'kind': 'cover', 'pages': [page]})
continue
if page.layout in (Page.Layout.SINGLE, Page.Layout.SPREAD):
flush()
frames.append({'kind': page.layout, 'pages': [page]})
continue
if page.layout == Page.Layout.PAIR_START:
flush()
pending.append(page)
if len(pending) == 2:
flush()
flush()
return frames