Spaces:
Runtime error
Runtime error
| # Utility functions shared across modules | |
| # This file helps break circular imports between main.py and pdf_generator.py | |
| from typing import List | |
| from models import ExportSection | |
| from constants import SECTION_ORDER | |
| def sort_sections_by_order(sections: List[ExportSection]) -> List[ExportSection]: | |
| """Sort sections according to the predefined SECTION_ORDER to ensure correct numerical ordering""" | |
| # Create a mapping from section title to order index | |
| order_map = {title: index for index, title in enumerate(SECTION_ORDER)} | |
| def get_section_order(section: ExportSection) -> int: | |
| # Try to match by exact title first | |
| if section.title in order_map: | |
| order_index = order_map[section.title] | |
| return order_index | |
| # Try case-insensitive matching | |
| section_title_lower = section.title.lower() | |
| for order_title, order_index in order_map.items(): | |
| if order_title.lower() == section_title_lower: | |
| return order_index | |
| # Try to match by key (remove 'prompt_' prefix if present) | |
| key_clean = section.key.replace('prompt_', '') if section.key.startswith('prompt_') else section.key | |
| if key_clean in order_map: | |
| order_index = order_map[key_clean] | |
| return order_index | |
| # Try partial matching for keys | |
| for order_title, order_index in order_map.items(): | |
| if key_clean.lower() in order_title.lower() or order_title.lower() in key_clean.lower(): | |
| return order_index | |
| # If no match found, put at the end | |
| return len(SECTION_ORDER) | |
| # Sort sections by their order | |
| sorted_sections = sorted(sections, key=get_section_order) | |
| return sorted_sections | |