(toc_items, page_list, start_index=1, model=None)
| 654 | |
| 655 | ##check if needed to process none page numbers |
| 656 | def process_none_page_numbers(toc_items, page_list, start_index=1, model=None): |
| 657 | for i, item in enumerate(toc_items): |
| 658 | if "physical_index" not in item: |
| 659 | # logger.info(f"fix item: {item}") |
| 660 | # Find previous physical_index |
| 661 | prev_physical_index = 0 # Default if no previous item exists |
| 662 | for j in range(i - 1, -1, -1): |
| 663 | if toc_items[j].get('physical_index') is not None: |
| 664 | prev_physical_index = toc_items[j]['physical_index'] |
| 665 | break |
| 666 | |
| 667 | # Find next physical_index |
| 668 | next_physical_index = -1 # Default if no next item exists |
| 669 | for j in range(i + 1, len(toc_items)): |
| 670 | if toc_items[j].get('physical_index') is not None: |
| 671 | next_physical_index = toc_items[j]['physical_index'] |
| 672 | break |
| 673 | |
| 674 | page_contents = [] |
| 675 | for page_index in range(prev_physical_index, next_physical_index+1): |
| 676 | # Add bounds checking to prevent IndexError |
| 677 | list_index = page_index - start_index |
| 678 | if list_index >= 0 and list_index < len(page_list): |
| 679 | page_text = f"<physical_index_{page_index}>\n{page_list[list_index][0]}\n<physical_index_{page_index}>\n\n" |
| 680 | page_contents.append(page_text) |
| 681 | else: |
| 682 | continue |
| 683 | |
| 684 | item_copy = copy.deepcopy(item) |
| 685 | del item_copy['page'] |
| 686 | result = add_page_number_to_toc(page_contents, item_copy, model) |
| 687 | if isinstance(result[0]['physical_index'], str) and result[0]['physical_index'].startswith('<physical_index'): |
| 688 | item['physical_index'] = int(result[0]['physical_index'].split('_')[-1].rstrip('>').strip()) |
| 689 | del item['page'] |
| 690 | |
| 691 | return toc_items |
| 692 | |
| 693 | |
| 694 |
no test coverage detected