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:
.html
files in the directory.index.html
, about.html
,
now.html
.#!/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."
With the help of ChatGPT.âŠī¸