cashmere

cashmere

bashfeed.sh

A small script to generate and link the rss feed for cashmere.rs.

#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
POSTS_DIR="$SCRIPT_DIR/blog"

RSS_FILE="$SCRIPT_DIR/blog.xml"

# RSS feed header
cat > "$RSS_FILE" <<'EOF'
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>RSS feed title</title>
  <link>https://cashmere.rs</link>
  <description>cashmere RSS feed</description>
  <language>en-us</language>
  <atom:link href="https://cashmere.rs/blog.xml" rel="self" type="application/rss+xml"/>
EOF

for file in "$POSTS_DIR"/*.html; do
  [[ -f $file ]] || continue
  [[ $(basename "$file") == "index.html" ]] && continue

  post_title=$(awk -F'<h1>|</h1>' '/<h1>/{print $2; exit}' "$file")
  post_date=$(awk -F'<time>|</time>' '/<time>/{print $2; exit}' "$file")
  post_content=$(sed -n '/<article>/,/<\/article>/p' "$file" | sed '/^[[:space:]]*$/d' | tr -s ' ')
  post_id=$(basename "$file" .html)

  cat >> "$RSS_FILE" <<EOF
  <item>
    <title>$post_title</title>
    <link>https://cashmere.rs/blog/$post_id</link>
    <guid>https://cashmere.rs/blog/$post_id</guid>
    <pubDate>$post_date</pubDate>
    <description><![CDATA[$post_content]]></description>
  </item>
EOF
done

cat >> "$RSS_FILE" <<'EOF'
</channel>
</rss>
EOF

<!--  link the RSS feed in the <head> section of each post  -->
for file in "$POSTS_DIR"/*.html; do
  [[ -f $file ]] || continue
  [[ $(basename "$file") == "index.html" ]] && continue

  grep -q '<link.*blog.xml' "$file" && continue   # already present; skip

  awk '
    /<\/head>/ {
      print "  <link rel=\"alternate\" type=\"application/rss+xml\" title=\"cashmere RSS feed\" href=\"https://cashmere.rs/blog.xml\"/>"
    }
    { print }
  ' "$file" > "$file".tmp && mv "$file".tmp "$file"
done
echo "RSS blog feed generated successfully."