#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Chapter Paths Module Provides utilities for finding and managing chapter files. """ from pathlib import Path from typing import Optional def default_chapter_draft_path(project_root: Path, chapter_num: int) -> Path: """ Get the default path for a chapter draft. Args: project_root: Project root directory chapter_num: Chapter number Returns: Path to the chapter draft file """ return project_root / "chapters" / f"chapter_{chapter_num:04d}.txt" def find_chapter_file(project_root: Path, chapter_num: int) -> Optional[Path]: """ Find a chapter file by number. Args: project_root: Project root directory chapter_num: Chapter number Returns: Path to the chapter file if found, None otherwise """ # Try default path default_path = default_chapter_draft_path(project_root, chapter_num) if default_path.exists(): return default_path # Try alternative naming patterns patterns = [ f"chapter_{chapter_num:04d}.txt", f"chapter_{chapter_num}.txt", f"第{chapter_num}章.txt", f"ch{chapter_num:04d}.txt", ] chapters_dir = project_root / "chapters" if not chapters_dir.exists(): return None for pattern in patterns: for file in chapters_dir.glob(pattern): return file return None def list_chapters(project_root: Path) -> list[int]: """ List all chapter numbers in the project. Args: project_root: Project root directory Returns: List of chapter numbers """ chapters_dir = project_root / "chapters" if not chapters_dir.exists(): return [] chapters = [] for file in chapters_dir.glob("chapter_*.txt"): try: num = int(file.stem.split("_")[1]) chapters.append(num) except (ValueError, IndexError): continue return sorted(chapters)