Create an index.html

no hurry, no pause.

Last update: 2024-08-26

How to create1 an index.html that lists and links to numerous HTML files in descending order (like a blog index page). We can use a script to generate the file automatically. Below is a basic approach using a shell script that will:

  1. Find all .html files in the directory.
  2. Sort them by modification time in descending order.
  3. Exclude index.html, about.html, now.html.
  4. Generate an index.html file with links to these files.
#!/bin/bash

# Specify the directory where your HTML files are located
directory="."

# Start creating the index.html file
echo "<!DOCTYPE html>" > index.html
echo "<html lang='en'>" >> index.html
echo "<head>" >> index.html
echo "    <meta charset='UTF-8'>" >> index.html
echo "    <meta name='viewport' content='width=device-width, initial-scale=1.0'>" >> index.html
echo "    <link rel="stylesheet" href="pandoc.css" />" >> index.html
echo "    <link rel="icon" type="image/x-icon" href="favicon.png">" >> index.html
echo "    <meta name="author" content="no hurry, no pause." />" >> index.html
echo "    <title>ℂ𝕠𝕟đ•Ĩ𝕚𝕟đ•Ļđ•Ļ𝕞</title>" >> index.html
echo "</head>" >> index.html
echo "<body>" >> index.html
echo "    <h1>ℂ𝕠𝕟đ•Ĩ𝕚𝕟đ•Ļđ•Ļ𝕞</h1>" >> index.html
echo "    <h3>no hurry, no pause.</h3>" >> index.html
echo "    <ul>" >> index.html

# Find all HTML files excluding index.html, about.html, now.html, and sort them by modification time in descending order

# Files to exclude
exclude_files=("index.html" "about.html" "now.html")

# Build the exclusion pattern
exclude_pattern=""
for file in "${exclude_files[@]}"; do
    exclude_pattern+=" ! -name '$file'"
done

eval "find "$directory" -maxdepth 1 -name '*.html' -type f ! -name 'index.html' $exclude_pattern" | while read -r filepath; do
    # Use macOS-compatible stat command to get the modification time
    mod_time=$(stat -f "%m" "$filepath")
    echo "$mod_time $filepath"
done | sort -r -n | while read -r line; do
    filepath=$(echo "$line" | cut -d ' ' -f 2-)
    filename=$(basename "$filepath")
    
    # Extract the date part from the filename
    filedate=$(echo "$filename" | cut -d '-' -f 1-3)
    
    # Extract the HTML title by reading the file content with sed
    filetitle=$(sed -n 's:.*<title>\(.*\)</title>.*:\1:p' "$filepath")
    
    # Format the link text
    linktext="${filedate}: ${filetitle}"
    
    echo "        <li><a href=\"$filename\">$linktext</a></li>" >> index.html
done

echo "    </ul>" >> index.html
echo "<div id="footer">" >> index.html
echo "<a href="index.html">⌘ Index</a>" >> index.html
echo "<a href="about.html">⌘ About</a>" >> index.html
echo "<a href="now.html">⌘ Now</a>" >> index.html
echo "<a href="feed.rss">⌘ RSS</a>" >> index.html
echo "</div>" >> index.html
echo "</body>" >> index.html
echo "</html>" >> index.html

echo "index.html has been generated."

Footnotes


  1. With the help of ChatGPT.↩ī¸Ž