Skip to content

Utility - Remake All Index Pages #4

Utility - Remake All Index Pages

Utility - Remake All Index Pages #4

name: Utility - remake all index pages
on:
workflow_dispatch:
jobs:
remake-all-indexes:
runs-on: ubuntu-latest
name: Remake file index pages
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Generate index files
run: |
header_html="$(cat template_header.html)"
footer_html="$(cat template_footer.html)"
## RECURSIVE FUNCTION TO PROCESS DIRECTORIES ##
generate_index_for_directory() {
local dir_path="$1"
local parent_link="$2"
echo "Working on directory: $dir_path"
table_html=""
table_html+="<table class=\"table table-striped table-bordered table-hover\">"
table_html+="<thead class=\"table-dark\"><tr><th>Item Name</th><th>Type</th><th>File Count / Size</th><th>Last Modified</th></tr></thead>"
table_html+=$'\n'
# Add directories to the index
for subdir in $(find "$dir_path" -mindepth 1 -maxdepth 1 -type d ! -name ".*" | sort)
do
folder=$(basename $subdir)
filecount=$(find "$subdir" -mindepth 1 -type f ! -name "*.html" | wc -l)
modified=$(git log -1 --format=%cd --date=format:'%d %b %Y %l:%M %p' -- "$subdir")
table_html+="<tr><td><a class=\"file-link\" href=\"$folder/index.html\">$folder</a></td><td>Directory</td><td>$filecount files</td><td>$modified</td></tr>"
table_html+=$'\n'
# Recursively generate index for the subdirectory
generate_index_for_directory "$subdir" "../"
done
# Add XML files to the index
for file in $(find "$dir_path" -mindepth 1 -maxdepth 1 -type f -name "*.xml" | sort)
do
filename=$(basename "$file")
filesize=$(du -h "$file" | cut -f1)
modified=$(git log -1 --format=%cd --date=format:'%d %b %Y %l:%M %p' -- "$file")
table_html+="<tr><td><a class=\"file-link\" href=\"$filename\">$filename</a></td><td>XML File</td><td>$filesize</td><td>$modified</td></tr>"
table_html+=$'\n'
done
table_html+="</table>"
# Add parent link if not the root directory
if [[ "$parent_link" != "" ]]; then
table_html+='<p><a href="'$parent_link'">Parent directory</a></p>'
fi
# Assemble and save HTML
folder_html="$header_html$table_html$footer_html"
echo "$folder_html" > "$dir_path/index.html"
}
## START FROM ROOT DIRECTORY ##
generate_index_for_directory "." ""
- uses: stefanzweifel/git-auto-commit-action@v4
with:
commit_message: Update file index pages
file_pattern: 'index.html **/index.html'