Skip to content

Commit

Permalink
Update generate_index to include relative path
Browse files Browse the repository at this point in the history
  • Loading branch information
kelvinnguyenn committed Dec 12, 2024
1 parent f3448fb commit 5a51930
Showing 1 changed file with 10 additions and 8 deletions.
18 changes: 10 additions & 8 deletions generate_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,24 @@ 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 != "":
parent_dir_url = os.path.dirname(dir_path)
if parent_dir_url != base_dir:
# Use `..` to go one level up from the current directory
index_content += f'<p><a href="../">.. (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 @@ -47,8 +49,8 @@ def generate_index(dir_path):
has_content = True
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 5a51930

Please sign in to comment.