summaryrefslogtreecommitdiff
path: root/.zs/list
blob: 854eab03168bdf85a545f3936127b123468b8f74 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
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"