- # First and last are simply the first *n* pages and the last *n* pages,
- # where *n* is the current window size.
- first = set(page_range[:window])
- last = set(page_range[-window:])
- # Now we look around our current page, making sure that we don't wrap
- # around.
- current_start = page_obj.number-1-window
- if current_start < 0:
- current_start = 0
- current_end = page_obj.number-1+window
- if current_end < 0:
- current_end = 0
- current = set(page_range[current_start:current_end])
- pages = []
- # If there's no overlap between the first set of pages and the current
- # set of pages, then there's a possible need for elusion.
- if len(first.intersection(current)) == 0:
- first_list = list(first)
- first_list.sort()
- second_list = list(current)
- second_list.sort()
- pages.extend(first_list)
- diff = second_list[0] - first_list[-1]
- # If there is a gap of two, between the last page of the first
- # set and the first page of the current set, then we're missing a
- # page.
- if diff == 2:
- pages.append(second_list[0] - 1)
- # If the difference is just one, then there's nothing to be done,
- # as the pages need no elusion and are correct.
- elif diff == 1:
- pass
- # Otherwise, there's a bigger gap which needs to be signaled for
- # elusion, by pushing a None value to the page list.
- else:
- pages.append(None)
- pages.extend(second_list)
+
+ # figure window
+ window_start = page_obj.number - window - 1
+ window_end = page_obj.number + window
+
+ # solve if window exceeded page range
+ if window_start < 0:
+ window_end = window_end - window_start
+ window_start = 0
+ if window_end > paginator.num_pages:
+ window_start = window_start - (window_end - paginator.num_pages)
+ window_end = paginator.num_pages
+ pages = page_range[window_start:window_end]
+
+ # figure margin and add elipses
+ if margin > 0:
+ # figure margin
+ tmp_pages = set(pages)
+ tmp_pages = tmp_pages.union(page_range[:margin])
+ tmp_pages = tmp_pages.union(page_range[-margin:])
+ tmp_pages = list(tmp_pages)
+ tmp_pages.sort()
+ pages = []
+ pages.append(tmp_pages[0])
+ for i in range(1, len(tmp_pages)):
+ # figure gap size => add elipses or fill in gap
+ gap = tmp_pages[i] - tmp_pages[i - 1]
+ if gap >= 3:
+ pages.append(None)
+ elif gap == 2:
+ pages.append(tmp_pages[i] - 1)
+ pages.append(tmp_pages[i])