Fixes duplicate filename generation
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.
This commit is contained in:
4 files changed
+68
-19
No files matched your search
+11
-3
@@ -23,16 +23,18 @@ class IndexGenerator:
|
|||||||
The index is organized alphabetically by last name, then first name.
|
The index is organized alphabetically by last name, then first name.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, output_dir: Path, people_subdir: str = ""):
|
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.
|
Create an IndexGenerator configured with the target output directory and an optional subdirectory for individual files.
|
||||||
|
|
||||||
Parameters:
|
Parameters:
|
||||||
output_dir (Path): Directory where the generated index file will be written.
|
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).
|
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.output_dir = output_dir
|
||||||
self.people_subdir = people_subdir
|
self.people_subdir = people_subdir
|
||||||
|
self.filename_map = filename_map or {}
|
||||||
|
|
||||||
def generate_index(
|
def generate_index(
|
||||||
self, individuals: List[Individual], index_filename: str = "Index.md"
|
self, individuals: List[Individual], index_filename: str = "Index.md"
|
||||||
@@ -80,7 +82,13 @@ class IndexGenerator:
|
|||||||
f.write(f"\n## {current_letter}\n\n")
|
f.write(f"\n## {current_letter}\n\n")
|
||||||
|
|
||||||
# Write individual link with life span
|
# Write individual link with life span
|
||||||
filename = individual.get_file_name()
|
# 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()
|
birth_info = individual.get_birth_info()
|
||||||
death_info = individual.get_death_info()
|
death_info = individual.get_death_info()
|
||||||
|
|
||||||
|
|||||||
+3
-8
@@ -78,18 +78,13 @@ class Individual:
|
|||||||
|
|
||||||
def get_file_name(self) -> str:
|
def get_file_name(self) -> str:
|
||||||
"""
|
"""
|
||||||
Build a filename-like string for the individual in the form "FamilyName FirstName" optionally followed by the birth year.
|
Build a filename-like string for the individual in the form "FamilyName FirstName".
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
filename (str): The generated filename string, either "FamilyName FirstName BirthYear" or "FamilyName FirstName" when the birth year is not available; does not include a file extension; preserves original name capitalization.
|
filename (str): The generated filename string "FamilyName FirstName"; does not include a file extension; preserves original name capitalization.
|
||||||
"""
|
"""
|
||||||
first, last = self.get_names()
|
first, last = self.get_names()
|
||||||
name_part = f"{last} {first}".strip()
|
return f"{last} {first}".strip()
|
||||||
|
|
||||||
birth_year = self.element.get_birth_year()
|
|
||||||
if birth_year != -1:
|
|
||||||
return f"{name_part} {birth_year}"
|
|
||||||
return name_part
|
|
||||||
|
|
||||||
def get_birth_info(self) -> Dict[str, str]:
|
def get_birth_info(self) -> Dict[str, str]:
|
||||||
"""
|
"""
|
||||||
|
|||||||
+5
-1
@@ -269,7 +269,11 @@ def convert_gedcom_to_markdown(
|
|||||||
if create_index:
|
if create_index:
|
||||||
logger.info("Generating index file")
|
logger.info("Generating index file")
|
||||||
people_subdir_name = "" if use_flat_structure else "people"
|
people_subdir_name = "" if use_flat_structure else "people"
|
||||||
index_gen = IndexGenerator(output_dir, people_subdir=people_subdir_name)
|
index_gen = IndexGenerator(
|
||||||
|
output_dir,
|
||||||
|
people_subdir=people_subdir_name,
|
||||||
|
filename_map=generator.filename_map
|
||||||
|
)
|
||||||
index_path = index_gen.generate_index(individuals)
|
index_path = index_gen.generate_index(individuals)
|
||||||
logger.info(f"Created index file: {index_path}")
|
logger.info(f"Created index file: {index_path}")
|
||||||
|
|
||||||
|
|||||||
@@ -55,6 +55,31 @@ class MarkdownGenerator:
|
|||||||
self.stories_dir = stories_dir if stories_dir else output_dir
|
self.stories_dir = stories_dir if stories_dir else output_dir
|
||||||
self.use_subdirectories = use_subdirectories
|
self.use_subdirectories = use_subdirectories
|
||||||
self.generated_stories = {} # Track generated story files
|
self.generated_stories = {} # Track generated story files
|
||||||
|
self.filename_map = {} # Map from individual ID to actual filename used
|
||||||
|
|
||||||
|
def _get_unique_filename(self, base_name: str, individual_id: str) -> str:
|
||||||
|
"""
|
||||||
|
Get a unique filename for an individual, handling duplicates by adding (1), (2), etc.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
base_name (str): Base filename without extension (e.g., "Kucera Alexander Maximilian")
|
||||||
|
individual_id (str): The individual's GEDCOM ID
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: Unique filename without extension (e.g., "Kucera Alexander Maximilian (1)")
|
||||||
|
"""
|
||||||
|
# Check if this base name has been used before
|
||||||
|
counter = 1
|
||||||
|
used_names = set(self.filename_map.values())
|
||||||
|
unique_name = base_name
|
||||||
|
|
||||||
|
while unique_name in used_names:
|
||||||
|
unique_name = f"{base_name} ({counter})"
|
||||||
|
counter += 1
|
||||||
|
|
||||||
|
# Store the mapping
|
||||||
|
self.filename_map[individual_id] = unique_name
|
||||||
|
return unique_name
|
||||||
|
|
||||||
def generate_note(self, individual: Individual) -> Path:
|
def generate_note(self, individual: Individual) -> Path:
|
||||||
"""
|
"""
|
||||||
@@ -66,7 +91,9 @@ class MarkdownGenerator:
|
|||||||
Returns:
|
Returns:
|
||||||
Path: Path to the created markdown file.
|
Path: Path to the created markdown file.
|
||||||
"""
|
"""
|
||||||
filename = individual.get_file_name() + ".md"
|
base_name = individual.get_file_name()
|
||||||
|
unique_name = self._get_unique_filename(base_name, individual.get_id())
|
||||||
|
filename = unique_name + ".md"
|
||||||
file_path = self.output_dir / filename
|
file_path = self.output_dir / filename
|
||||||
|
|
||||||
logger.info(f"Generating note: {filename}")
|
logger.info(f"Generating note: {filename}")
|
||||||
@@ -203,7 +230,7 @@ class MarkdownGenerator:
|
|||||||
|
|
||||||
# Only write partner bullet if partner exists
|
# Only write partner bullet if partner exists
|
||||||
if family["partner"]:
|
if family["partner"]:
|
||||||
partner_name = family["partner"].get_file_name()
|
partner_name = self._get_actual_filename(family["partner"])
|
||||||
f.write(f"* Partner: {self._wiki_link(partner_name)}\n")
|
f.write(f"* Partner: {self._wiki_link(partner_name)}\n")
|
||||||
|
|
||||||
# Write marriage bullets if present
|
# Write marriage bullets if present
|
||||||
@@ -216,7 +243,7 @@ class MarkdownGenerator:
|
|||||||
if family["children"]:
|
if family["children"]:
|
||||||
f.write("\n**Children:**\n")
|
f.write("\n**Children:**\n")
|
||||||
for child in family["children"]:
|
for child in family["children"]:
|
||||||
f.write(f"* Child: {self._wiki_link(child.get_file_name())}\n")
|
f.write(f"* Child: {self._wiki_link(self._get_actual_filename(child))}\n")
|
||||||
|
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
|
|
||||||
@@ -240,7 +267,7 @@ class MarkdownGenerator:
|
|||||||
f.write("## Parents\n")
|
f.write("## Parents\n")
|
||||||
|
|
||||||
for parent in parents:
|
for parent in parents:
|
||||||
f.write(f"* Parent: {self._wiki_link(parent.get_file_name())}\n")
|
f.write(f"* Parent: {self._wiki_link(self._get_actual_filename(parent))}\n")
|
||||||
|
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
|
|
||||||
@@ -262,7 +289,7 @@ class MarkdownGenerator:
|
|||||||
f.write("## Children\n")
|
f.write("## Children\n")
|
||||||
|
|
||||||
for child in children:
|
for child in children:
|
||||||
f.write(f"* Child: {self._wiki_link(child.get_file_name())}\n")
|
f.write(f"* Child: {self._wiki_link(self._get_actual_filename(child))}\n")
|
||||||
|
|
||||||
f.write("\n")
|
f.write("\n")
|
||||||
|
|
||||||
@@ -392,7 +419,7 @@ class MarkdownGenerator:
|
|||||||
# Generate separate story files and link to them
|
# Generate separate story files and link to them
|
||||||
if stories:
|
if stories:
|
||||||
f.write("### Stories\n\n")
|
f.write("### Stories\n\n")
|
||||||
individual_name = individual.get_file_name()
|
individual_name = self._get_actual_filename(individual)
|
||||||
|
|
||||||
for story in stories:
|
for story in stories:
|
||||||
# Generate the story file
|
# Generate the story file
|
||||||
@@ -446,10 +473,25 @@ class MarkdownGenerator:
|
|||||||
"""
|
"""
|
||||||
f.write(f"({key}:: {value})\n")
|
f.write(f"({key}:: {value})\n")
|
||||||
|
|
||||||
|
def _get_actual_filename(self, individual: Individual) -> str:
|
||||||
|
"""
|
||||||
|
Get the actual filename for an individual, using the mapped name if available.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
individual (Individual): The individual to get the filename for
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
str: The actual filename without extension
|
||||||
|
"""
|
||||||
|
individual_id = individual.get_id()
|
||||||
|
if individual_id in self.filename_map:
|
||||||
|
return self.filename_map[individual_id]
|
||||||
|
return individual.get_file_name()
|
||||||
|
|
||||||
def _wiki_link(self, text: str) -> str:
|
def _wiki_link(self, text: str) -> str:
|
||||||
"""
|
"""
|
||||||
Format text as an Obsidian-style WikiLink.
|
Format text as an Obsidian-style WikiLink.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
wiki_link (str): The input text wrapped in double square brackets (e.g. `[[Name]]`).
|
wiki_link (str): The input text wrapped in double square brackets (e.g. `[[Name]]`).
|
||||||
"""
|
"""
|
||||||
|
|||||||
Reference in new issue
Block a user