Addresses an issue where duplicate filenames could be generated for individuals with the same name, especially in large datasets. This change introduces a mechanism to ensure unique filenames by appending a counter to duplicate names. It updates the index generator and markdown generator to use this mechanism, ensuring that links between individuals are correctly resolved.
115 lines
4.5 KiB
Python
115 lines
4.5 KiB
Python
"""
|
|
Index file generator for family tree notes.
|
|
|
|
This module generates an alphabetical index of all individuals in the
|
|
family tree.
|
|
"""
|
|
|
|
from pathlib import Path
|
|
from typing import List
|
|
import logging
|
|
import re
|
|
|
|
from individual import Individual
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
class IndexGenerator:
|
|
"""
|
|
Generates an index file linking to all individual notes.
|
|
|
|
The index is organized alphabetically by last name, then first name.
|
|
"""
|
|
|
|
def __init__(self, output_dir: Path, people_subdir: str = "", filename_map: dict = None):
|
|
"""
|
|
Create an IndexGenerator configured with the target output directory and an optional subdirectory for individual files.
|
|
|
|
Parameters:
|
|
output_dir (Path): Directory where the generated index file will be written.
|
|
people_subdir (str): Optional subdirectory name to prepend to people file links (empty string means no subdirectory).
|
|
filename_map (dict): Optional mapping from individual IDs to actual filenames (for handling duplicates).
|
|
"""
|
|
self.output_dir = output_dir
|
|
self.people_subdir = people_subdir
|
|
self.filename_map = filename_map or {}
|
|
|
|
def generate_index(
|
|
self, individuals: List[Individual], index_filename: str = "Index.md"
|
|
) -> Path:
|
|
"""
|
|
Generate an alphabetical Markdown index of individuals grouped by last-name initial.
|
|
|
|
Writes a Markdown file at self.output_dir / index_filename containing a header with the total count, section headers "## {LETTER}" for each last-name initial (uses "#" for individuals without a last name), and one wiki-style link per individual (prefixed by the instance's people_subdir when set). Each entry includes an optional life-span "(birth-death)" when birth or death information is available.
|
|
|
|
Parameters:
|
|
individuals (List[Individual]): Individuals to include in the index.
|
|
index_filename (str): Name of the index file to create (default: 'Index.md').
|
|
|
|
Returns:
|
|
Path: Path to the created index file.
|
|
"""
|
|
index_path = self.output_dir / index_filename
|
|
|
|
logger.info(f"Generating index file: {index_filename}")
|
|
|
|
# Sort individuals by last name, then first name
|
|
sorted_individuals = sorted(
|
|
individuals,
|
|
key=lambda i: (i.get_names()[1].lower(), i.get_names()[0].lower()),
|
|
)
|
|
|
|
with open(index_path, "w", encoding="utf-8") as f:
|
|
f.write("# Family Tree Index\n\n")
|
|
f.write(f"Total individuals: {len(individuals)}\n\n")
|
|
|
|
# Group by last name initial
|
|
current_letter = ""
|
|
|
|
for individual in sorted_individuals:
|
|
_, last = individual.get_names()
|
|
|
|
# Write letter header if changed
|
|
if last:
|
|
letter = last[0].upper()
|
|
else:
|
|
letter = "#" # For individuals without last name
|
|
|
|
if letter != current_letter:
|
|
current_letter = letter
|
|
f.write(f"\n## {current_letter}\n\n")
|
|
|
|
# Write individual link with life span
|
|
# Use mapped filename if available, otherwise use base filename
|
|
individual_id = individual.get_id()
|
|
if individual_id in self.filename_map:
|
|
filename = self.filename_map[individual_id]
|
|
else:
|
|
filename = individual.get_file_name()
|
|
|
|
birth_info = individual.get_birth_info()
|
|
death_info = individual.get_death_info()
|
|
|
|
# Format life span
|
|
if birth_info["year"] or death_info["date"]:
|
|
death_year = ""
|
|
if death_info["date"]:
|
|
match = re.search(r"(\d{4})\b", death_info["date"])
|
|
if match:
|
|
death_year = match.group(1)
|
|
life_span = f" ({birth_info['year']}-{death_year})"
|
|
else:
|
|
life_span = ""
|
|
|
|
# Create WikiLink with path prefix if using subdirectories
|
|
if self.people_subdir:
|
|
wiki_link = f"[[{self.people_subdir}/{filename}|{individual.get_full_name()}]]"
|
|
else:
|
|
wiki_link = f"[[{filename}]]"
|
|
|
|
f.write(f"- {wiki_link}{life_span}\n")
|
|
|
|
logger.info(f"Index generated with {len(individuals)} individuals")
|
|
return index_path |