Skip to content

Commit 8d55b0f

Browse files
committed
Refactor generate_markdown_document function to include base_name parameter
The generate_markdown_document function now includes a new parameter called base_name. This parameter is used to specify the base name for the title of the markdown document. The function also checks if the input_path is a relative path and uses its basename as the base_name if it is. This change simplifies the function and improves its flexibility. Refactor manage_output_directory function to include input_path parameter The manage_output_directory function now includes a new parameter called input_path. This parameter is used to determine the base_name for the output file. If the input_path is a relative path, the function uses its basename as the base_name. This change enhances the functionality of the function and makes it more robust. Update main function to pass base_name parameter to generate_markdown_document and manage_output_directory The main function has been updated to pass the base_name parameter to both the generate_markdown_document and manage_output_directory functions. This ensures that the correct base_name is used for the title of the markdown document and the output file name. This change improves the accuracy and consistency of the generated markdown content.
1 parent 3c9d5b1 commit 8d55b0f

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

src/codemapper/codemapper.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ def generate_markdown_document(
443443
gitignore_spec: pathspec.PathSpec,
444444
include_ignored: bool = False,
445445
source: str = "",
446+
base_name: str = "",
446447
) -> str:
447448
"""
448449
Generate a markdown document from the directory structure.
@@ -452,11 +453,11 @@ def generate_markdown_document(
452453
gitignore_spec (pathspec.PathSpec): The gitignore specifications to apply.
453454
include_ignored (bool): Whether to include files ignored by .gitignore.
454455
source (str): The source of the repo (local directory or GitHub URL).
456+
base_name (str): The base name to use for the title of the markdown document.
455457
456458
Returns:
457459
str: The generated markdown content as a string.
458460
"""
459-
base_name = os.path.basename(directory_path)
460461
md_content = f"# {base_name}\n\n"
461462
md_content += f"> CodeMap Source: {source}\n\n"
462463
md_content += (
@@ -572,20 +573,25 @@ def clone_github_repo(repo_url: str) -> str:
572573
return repo_path
573574

574575

575-
def manage_output_directory(base_name: str) -> str:
576+
def manage_output_directory(base_name: str, input_path: str) -> str:
576577
"""
577578
Manage the output directory for the markdown output.
578579
579580
Args:
580581
base_name (str): The base name for the output file
581582
(usually the repository or directory name).
583+
input_path (str): The original input path provided by the user.
582584
583585
Returns:
584586
str: The full path for the output markdown file.
585587
"""
586588
output_dir = os.path.join(".", "_codemaps")
587589
os.makedirs(output_dir, exist_ok=True)
588590

591+
# If input_path is a relative path, use its basename
592+
if not os.path.isabs(input_path) and not input_path.startswith(('http://', 'https://')):
593+
base_name = os.path.basename(os.path.abspath(input_path))
594+
589595
file_name = f"{base_name}_codemap.md"
590596
return os.path.join(output_dir, file_name)
591597

@@ -638,13 +644,18 @@ def main():
638644
else:
639645
directory_path = path
640646

647+
# Determine the base_name for the title
648+
if not os.path.isabs(args.input_path) and not args.input_path.startswith(('http://', 'https://')):
649+
base_name = os.path.basename(os.path.abspath(args.input_path))
650+
else:
651+
base_name = os.path.basename(directory_path)
652+
641653
gitignore_spec = load_gitignore_specs(directory_path)
642654
markdown_content = generate_markdown_document(
643-
directory_path, gitignore_spec, args.include_ignored, source
655+
directory_path, gitignore_spec, args.include_ignored, source, base_name
644656
)
645657

646-
base_name = os.path.basename(directory_path)
647-
output_file_path = manage_output_directory(base_name)
658+
output_file_path = manage_output_directory(base_name, args.input_path)
648659

649660
with open(output_file_path, "w", encoding="utf-8") as md_file:
650661
md_file.write(markdown_content)

0 commit comments

Comments
 (0)