mirror of
https://git.mills.io/prologic/zs
synced 2025-02-06 23:29:23 +03:00
91 lines
2.8 KiB
Bash
Executable File
91 lines
2.8 KiB
Bash
Executable File
#!/bin/sh
|
|
|
|
# Define the directory to iterate over from the first argument
|
|
dir="$1"
|
|
|
|
# Initialize the variable to hold the HTML list and date-file pairs
|
|
html=""
|
|
date_file_pairs=""
|
|
|
|
# Check if the directory exists
|
|
if [ -d "$dir" ]; then
|
|
# Find all Markdown files in the directory and store them in a variable
|
|
md_files=$(find "$dir" -maxdepth 1 -type f -name "*.md")
|
|
|
|
# Process each Markdown file in a regular loop (not in a subshell)
|
|
for md_file in $md_files; do
|
|
# Extract front matter using sed, ensure the file is quoted
|
|
front_matter=$(sed -n '/^---$/,/^---$/p' "$md_file")
|
|
|
|
# Extract date
|
|
date=$(echo "$front_matter" | grep '^date:' | sed 's/^date:[[:space:]]*//')
|
|
|
|
# If no date is found, use a default date
|
|
if [ -z "$date" ]; then
|
|
date="1970-01-01" # Default date if none is found
|
|
fi
|
|
|
|
# Add the date and file to the date_file_pairs variable, using printf to correctly append newlines
|
|
date_file_pairs=$(printf "%s\n%s|%s" "$date_file_pairs" "$date" "$md_file")
|
|
done
|
|
|
|
# Sort the date and file pairs by date
|
|
sorted_date_file_pairs=$(printf "%s" "$date_file_pairs" | sort)
|
|
|
|
# Create a file descriptor to avoid subshell issues
|
|
exec 3<<EOF
|
|
$sorted_date_file_pairs
|
|
EOF
|
|
|
|
# Generate the navigation list
|
|
html="<ul class=\"list\">"
|
|
while IFS= read -r pair <&3; do
|
|
# Split the pair into date and filename using | as a delimiter
|
|
date=$(printf "%s" "$pair" | cut -d'|' -f1)
|
|
md_file=$(printf "%s" "$pair" | cut -d'|' -f2)
|
|
|
|
# Ensure md_file is not empty (safety check)
|
|
[ -z "$md_file" ] && continue
|
|
|
|
# Extract front matter again using sed, ensure md_file is quoted
|
|
front_matter=$(sed -n '/^---$/,/^---$/p' "$md_file")
|
|
|
|
# Extract title
|
|
title=$(echo "$front_matter" | grep '^title:' | sed 's/^title:[[:space:]]*//')
|
|
|
|
# Use filename as a fallback if title is empty
|
|
if [ -z "$title" ]; then
|
|
# Get the base filename without extension, quote the argument
|
|
base_name=$(basename "$md_file" .md)
|
|
# Replace hyphens and underscores with spaces for display
|
|
title=$(echo "$base_name" | sed 's/-/ /g' | sed 's/_/ /g')
|
|
# Capitalize each word
|
|
title=$(echo "$title" | awk '{ for (i=1; i<=NF; i++) $i=toupper(substr($i,1,1)) substr($i,2); print }')
|
|
fi
|
|
|
|
# Format the date using POSIX-compliant date formatting
|
|
if formatted_date=$(date -d "$date" '+%Y-%m-%d' 2>/dev/null); then
|
|
: # formatted_date is valid
|
|
else
|
|
formatted_date="$date"
|
|
fi
|
|
|
|
# Construct the link href, quote the argument
|
|
href="/$dir/$(basename "${md_file%.md}.html")"
|
|
|
|
# Append to the HTML list
|
|
html="$html<li><a href=\"$href\">$title</a> (published: $formatted_date)</li>"
|
|
done
|
|
html="$html</ul>"
|
|
|
|
# Close file descriptor
|
|
exec 3<&-
|
|
|
|
else
|
|
# If the directory doesn't exist, set a message
|
|
html="<p>No pages found.</p>"
|
|
fi
|
|
|
|
# Output the HTML list
|
|
echo "$html"
|