diff options
Diffstat (limited to '.zs/list')
-rwxr-xr-x | .zs/list | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/.zs/list b/.zs/list new file mode 100755 index 0000000..854eab0 --- /dev/null +++ b/.zs/list @@ -0,0 +1,90 @@ +#!/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="01-01-1970" # 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 -r -t "|" -k 1,1.2n -k 1.4,1.6n -k 1.8,1.12n) + + # Create a file descriptor to avoid subshell issues + exec 3<<EOF +$sorted_date_file_pairs +EOF + + # Generate the navigation list + html="<ol 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><span class="subtle">(published: <time datetime=$formatted_date>$formatted_date</time>)</span></li>" + done + html="$html</ol>" + + # 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" |