site

A custom static site generator written in C.
git clone https://unitehenry.com/git/site.git
Log | Files | Refs | README

rss (1772B)


      1 #!/usr/bin/env bash
      2 
      3 RSS_TITLE="unitehenry"
      4 RSS_DESCRIPTION="cracked software engineer"
      5 PUBLIC_URL="https://unitehenry.com"
      6 BLOG_INDEX="content/index.md"
      7 BLOG_HTML="$(mktemp)"
      8 
      9 trap 'rm -f "$BLOG_HTML"' EXIT
     10 
     11 if [ ! -f "$BLOG_INDEX" ]; then
     12   echo "error: $BLOG_INDEX not found" >&2
     13   exit 1
     14 fi
     15 
     16 if ! pandoc "$BLOG_INDEX" -t html5 -o "$BLOG_HTML"; then
     17   echo "error: failed to render $BLOG_INDEX" >&2
     18   exit 1
     19 fi
     20 
     21 # Collapse the whitespace htmlq adds around formatted HTML.
     22 get_text() {
     23   htmlq -t "$1" |
     24     tr '\n' ' ' |
     25     sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]][[:space:]]*/ /g'
     26 }
     27 
     28 echo '<?xml version="1.0" encoding="UTF-8"?>'
     29 echo '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'
     30 echo '<channel>'
     31 echo "<title>$RSS_TITLE</title>"
     32 echo "<link>$PUBLIC_URL/blog</link>"
     33 echo "<description>$RSS_DESCRIPTION</description>"
     34 echo '<language>en-us</language>'
     35 
     36 cat "$BLOG_HTML" | htmlq -a id '#blog ~ details summary' | while read -r line; do
     37   TITLE=$(cat "$BLOG_HTML" | get_text "#$line" | sed 's/ •.*$//')
     38   DATE=$(cat "$BLOG_HTML" | get_text "#$line + p")
     39   DESCRIPTION=$(cat "$BLOG_HTML" | get_text "#$line + p + p")
     40   SLUG=$(cat "$BLOG_HTML" | htmlq -a href "#$line a[href^=\"/blog/\"]" | sed -n '1p')
     41   CONTENT=$(cat "build$SLUG.html" | htmlq main | sed 's/<main>//;s/<\/main>//')
     42   CLEAN_DATE=$(echo "$DATE" | sed 's/th//;s/st//;s/nd//;s/rd//')
     43   PUBDATE=$(date -R -d "$CLEAN_DATE" 2>/dev/null || echo "$DATE")
     44 
     45   echo '<item>'
     46   echo "<title>$TITLE</title>"
     47   echo "<link>$PUBLIC_URL$SLUG</link>"
     48   echo "<description>$DESCRIPTION</description>"
     49   echo "<pubDate>$PUBDATE</pubDate>"
     50   echo "<content:encoded><![CDATA[$CONTENT]]></content:encoded>"
     51   echo '</item>'
     52 done
     53 
     54 echo '</channel>'
     55 echo '</rss>'