Spaces:
Runtime error
Runtime error
File size: 1,778 Bytes
8126b08 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | # 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
|