commit d64cab4d638e26df0d8bd29cfc73764c4c101284
parent 9aa52fcbbcdaf3808b4012e0099a5672ff0f9a55
Author: Henry Unite <unitehenry@gmail.com>
Date: Sat, 28 Nov 2026 20:24:42 +0000
refactor(rss): parse homepage blog entries with htmlq
Diffstat:
| M | scripts/rss | | | 147 | ++++++++++++++++++++++++------------------------------------------------------- |
1 file changed, 44 insertions(+), 103 deletions(-)
diff --git a/scripts/rss b/scripts/rss
@@ -4,12 +4,27 @@ RSS_TITLE="unitehenry"
RSS_DESCRIPTION="cracked software engineer"
PUBLIC_URL="https://unitehenry.com"
BLOG_INDEX="content/index.md"
+BLOG_HTML="$(mktemp)"
+
+trap 'rm -f "$BLOG_HTML"' EXIT
if [ ! -f "$BLOG_INDEX" ]; then
echo "error: $BLOG_INDEX not found" >&2
exit 1
fi
+if ! pandoc "$BLOG_INDEX" -t html5 -o "$BLOG_HTML"; then
+ echo "error: failed to render $BLOG_INDEX" >&2
+ exit 1
+fi
+
+# Collapse the whitespace htmlq adds around formatted HTML.
+get_text() {
+ htmlq -t "$1" |
+ tr '\n' ' ' |
+ sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//' -e 's/[[:space:]][[:space:]]*/ /g'
+}
+
echo '<?xml version="1.0" encoding="UTF-8"?>'
echo '<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">'
echo '<channel>'
@@ -18,115 +33,41 @@ echo "<link>$PUBLIC_URL/blog</link>"
echo "<description>$RSS_DESCRIPTION</description>"
echo '<language>en-us</language>'
-# Read the blog entries from the details blocks in the homepage's blog section.
-awk '
- function trim(s) {
- gsub(/^[[:space:]]+|[[:space:]]+$/, "", s)
- return s
- }
-
- function add_paragraph(text) {
- text = trim(text)
-
- if (date == "") {
- date = text
- } else if (description == "") {
- description = text
- }
- }
-
- /^## blog[[:space:]]*$/ {
- in_blog = 1
- next
- }
-
- in_blog && /^## / {
- exit
- }
-
- in_blog && /<details>/ {
- in_item = 1
- title = ""
- date = ""
- description = ""
- slug = ""
- summary = ""
- paragraph = ""
- in_summary = 0
- in_paragraph = 0
- next
- }
-
- in_blog && in_item && /<\/details>/ {
- if (slug != "") {
- printf "%s\t%s\t%s\t%s\n", title, date, description, slug
- }
-
- in_item = 0
- next
- }
-
- in_blog && in_item {
- line = trim($0)
-
- if (line ~ /<summary>/) {
- in_summary = 1
- sub(/^.*<summary>/, "", line)
- }
-
- if (in_summary) {
- if (line ~ /<\/summary>/) {
- sub(/<\/summary>.*$/, "", line)
- summary = trim(summary " " line)
-
- index_separator = index(summary, " • ")
- title = index_separator ? trim(substr(summary, 1, index_separator - 1)) : summary
-
- if (match(summary, /\(\/blog\/[^)]+\)/)) {
- slug = substr(summary, RSTART + 1, RLENGTH - 2)
- }
-
- in_summary = 0
- } else {
- summary = summary " " line
- }
-
+htmlq -f "$BLOG_HTML" '#blog ~ details' |
+ awk '
+ /<details>/ {
+ in_block = 1
+ block = $0 "\n"
next
}
- if (line ~ /<p>/) {
- in_paragraph = 1
- sub(/^.*<p>/, "", line)
+ in_block {
+ block = block $0 "\n"
}
- if (in_paragraph) {
- if (line ~ /<\/p>/) {
- sub(/<\/p>.*$/, "", line)
- add_paragraph(paragraph " " line)
- paragraph = ""
- in_paragraph = 0
- } else {
- paragraph = paragraph " " line
- }
-
- next
+ in_block && /<\/details>/ {
+ printf "%s%c", block, 0
+ in_block = 0
+ block = ""
}
- }
-' "$BLOG_INDEX" | while IFS=$'\t' read -r TITLE DATE DESCRIPTION SLUG; do
- [ -n "$SLUG" ] || continue
-
- CONTENT=$(cat "build$SLUG.html" | htmlq main | sed 's/<main>//;s/<\/main>//')
- CLEAN_DATE=$(echo "$DATE" | sed 's/th//;s/st//;s/nd//;s/rd//')
- PUBDATE=$(date -R -d "$CLEAN_DATE" 2>/dev/null || echo "$DATE")
-
- echo '<item>'
- echo "<title>$TITLE</title>"
- echo "<link>$PUBLIC_URL$SLUG</link>"
- echo "<description>$DESCRIPTION</description>"
- echo "<pubDate>$PUBDATE</pubDate>"
- echo "<content:encoded><![CDATA[$CONTENT]]></content:encoded>"
- echo '</item>'
-done
+ ' |
+ while IFS= read -r -d '' block; do
+ TITLE=$(printf '%s\n' "$block" | get_text summary | sed 's/ •.*$//')
+ DATE=$(printf '%s\n' "$block" | get_text 'p:nth-of-type(1)')
+ DESCRIPTION=$(printf '%s\n' "$block" | get_text 'p:nth-of-type(2)')
+ SLUG=$(printf '%s\n' "$block" | htmlq -a href 'summary a[href^="/blog/"]' | sed -n '1p')
+ CONTENT=$(cat "build$SLUG.html" | htmlq main | sed 's/<main>//;s/<\/main>//')
+ CLEAN_DATE=$(echo "$DATE" | sed 's/th//;s/st//;s/nd//;s/rd//')
+ PUBDATE=$(date -R -d "$CLEAN_DATE" 2>/dev/null || echo "$DATE")
+
+ echo '<item>'
+ echo "<title>$TITLE</title>"
+ echo "<link>$PUBLIC_URL$SLUG</link>"
+ echo "<description>$DESCRIPTION</description>"
+ echo "<pubDate>$PUBDATE</pubDate>"
+ echo "<content:encoded><![CDATA[$CONTENT]]></content:encoded>"
+ echo '</item>'
+ done
echo '</channel>'
echo '</rss>'