Skip to content

Instantly share code, notes, and snippets.

@rodmoioliveira
Created August 18, 2023 20:22
Show Gist options
  • Select an option

  • Save rodmoioliveira/90fe26565fdbdf08fe407dc2b3cc54d2 to your computer and use it in GitHub Desktop.

Select an option

Save rodmoioliveira/90fe26565fdbdf08fe407dc2b3cc54d2 to your computer and use it in GitHub Desktop.
rss.sh
#!/bin/bash
declare TRACE
[[ "${TRACE}" == 1 ]] && set -o xtrace
set -o errexit
set -o nounset
set -o pipefail
set -o noclobber
posts() {
local -a posts=()
while IFS= read -r line; do posts+=("${line}"); done < <(
fd . -e md content/blog |
xargs rg '^draft = false$' -l |
xargs rg '^(date) = ' --with-filename --no-line-number |
sd ':date = ' ' ' |
sort -rk2 |
awk '{print $1}'
)
for post in "${posts[@]}"; do
post_metadata=$(
rg \
'^(title|date|description|slug|categories|image) ?= ?' \
"${post}" \
--no-line-number |
sd '[\[\]]' "" |
sd '", {0,5}"' ', ' |
sd ' {0,5}= {0,5}' ' '
)
post_date=$(
echo "${post_metadata}" |
rg date |
awk '{print $2}' |
xargs -n1 bash -c 'date --date=$0 --rfc-email'
)
post_title=$(
echo "${post_metadata}" |
rg title |
awk -F '"' '{print $2}'
)
post_link=$(
echo "${post_metadata}" |
rg slug |
awk '{print $2}' |
sd '"' ''
)
post_description=$(
echo "${post_metadata}" |
rg description |
awk -F '"' '{print $2}'
)
post_categories=$(
echo "${post_metadata}" |
rg categories |
awk -F '"' '{print $2}'
)
post_image=$(
echo "${post_metadata}" |
rg image |
awk -F '"' '{print $2}'
)
post_image_ext=$(
echo "${post_image}" |
awk -F '.' '{print $NF}'
)
post_image_length=$(
stat -c "%s" static/images/"${post_image}"
)
cat <<EOF
<item>
<title>${post_title}</title>
<pubDate>${post_date}</pubDate>
<author>rodolfo.moi.oliveira@gmail.com (Rodolfo Mói de Oliveira)</author>
<link>https://rm-o.dev/blog/${post_link}/</link>
<guid>https://rm-o.dev/blog/${post_link}/</guid>
<description>${post_description}</description>
<category>${post_categories}</category>
<enclosure url="https://rm-o.dev/images/${post_image}" length="${post_image_length}" type="image/${post_image_ext}" />
</item>
EOF
done
}
rss() {
items=$(posts)
last_build_date=$(date --rfc-email)
cat <<EOF >|docs/rss.xml
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>Rodolfo Mói de Oliveira&#x27;s website.</title>
<link>https://rm-o.dev/</link>
<description>Rodolfo Mói de Oliveira&#x27;s website.</description>
<language>en</language>
<atom:link href="https://rm-o.dev/rss.xml" rel="self" type="application/rss+xml"/>
<lastBuildDate>${last_build_date}</lastBuildDate>
<ttl>60</ttl>
${items}
</channel>
</rss>
EOF
}
main() {
rss
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment