Skip to content

Commit

Permalink
Update generate index to have rel path
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
kelvinnguyenn committed Dec 12, 2024
1 parent 7b921f6 commit 2719e60
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions generate_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ def generate_index(dir_path):

files = sorted(os.listdir(dir_path))

# Get the name of the directory for the heading
dir_name = os.path.dirname(dir_path).replace(base_dir, "")
# Get the full relative path of the directory for the heading
dir_relative_path = os.path.relpath(dir_path, base_dir).replace(os.sep, "/")

# Create index.html file with a heading and a "Go to Parent Directory" link
index_content = f'<html><body>'

# Add a link to go to the parent directory
parent_dir_url = os.path.dirname(dir_path).replace(base_dir, "").replace(os.sep, "/")
if parent_dir_url != "":
index_content += f'<p><a href="../">.. (Parent Directory)</a></p>'

parent_dir_url = os.path.dirname(dir_path)
if parent_dir_url != base_dir:
# Calculate relative path to parent directory
parent_dir_relative = os.path.relpath(parent_dir_url, base_dir).replace(os.sep, "/")
index_content += f'<p><a href="../{parent_dir_relative}/">.. (Parent Directory)</a></p>'

if dir_path != base_dir and dir_path.split("_")[-1].isdigit():
if int(dir_path.split("_")[-1]) < 2000:
index_content += '<p>Destination MGRA in red; MGRA or TAZ w access in blue.</p>'

# Add the heading for the current directory
index_content += f'<h1>Index of {dir_name}</h1><ul>'
index_content += f'<h1>Index of {dir_relative_path}</h1><ul>'

# Track if there are any valid files or directories to list
has_content = False
Expand All @@ -48,7 +51,8 @@ def generate_index(dir_path):
elif os.path.isdir(file_path) and os.path.basename(file_path) != ".github":
# Add directories, but avoid creating links for excluded directories like `.github`
dir_url = os.path.basename(file_path) # Get the directory name directly
index_content += f'<li><a href="{dir_url}/">{file}/</a></li>'
dir_relative_url = os.path.relpath(file_path, base_dir).replace(os.sep, "/")
index_content += f'<li><a href="{dir_relative_url}/">{file}/</a></li>'
has_content = True
# If no valid files or directories were found, still create the index file with a message
if not has_content:
Expand Down

0 comments on commit 2719e60

Please sign in to comment.