Skip to content

Instantly share code, notes, and snippets.

@lee2sman
Created May 6, 2024 19:06
Show Gist options
  • Save lee2sman/00ff4d7363e92724b906df75577aa8b8 to your computer and use it in GitHub Desktop.
Save lee2sman/00ff4d7363e92724b906df75577aa8b8 to your computer and use it in GitHub Desktop.
an in-progress buggy RSS feed generator
blog_name="My blog"
blog_link=https://website.com/blog
blog_description="My blog description"
blog_feed=rss.xml
blog_dir=site
blog_posts=posts
#!/bin/bash
function set_paths {
# change if you have a different config file
CONFIG_PATH=config.conf
}
function source_config {
# set feed_name to blog_feed value in config
feed_name=$(grep -oP '(?<=blog_feed=).*' $CONFIG_PATH)
touch $feed_name
# set posts folder to value in config
POSTS_PATH=$(grep -oP '(?<=blog_posts=).*' $CONFIG_PATH)
# make posts folder if it doesn't exist
mkdir -p $POSTS_PATH
# set site destination folder
site_folder=$(grep -oP '(?<=blog_dir=).*' $CONFIG_PATH)
# make site folder if doesn't exist
mkdir -p $site_folder
}
function check_valid {
# source config file
if [ ! -e $CONFIG_PATH ]; then
echo "$CONFIG_PATH not found"
exit 1
fi
# Source the config file
. $CONFIG_PATH
if [[ -z $blog_name || -z $blog_link || -z $blog_description || -z $blog_feed || -z $blog_dir || -z $blog_posts ]]; then
echo "$CONFIG_PATH missing arguments."
exit 1
fi
}
function create_feed {
# feed meta
> $feed_name #erases file, start from scratch
echo '<rss version="2.0">'>> $feed_name
echo "<channel>" >> $feed_name
echo "<title>$blog_name</title>" >> $feed_name
echo "<link>$blog_link</link>" >> $feed_name
echo "<description>$blog_description</description>" >> $feed_name
# individual feed items
for file in $POSTS_PATH/*.md
do
echo "<item>" >> $feed_name
# get individual title
echo "<title>" >> $feed_name
grep -oP '(?<=title: ).*' $file >> $feed_name
echo "</title>" >> $feed_name
# get individual url
echo "<link>" >> $feed_name
echo $blog_link/${file// /-} >> $feed_name
echo "</link>" >> $feed_name
# echo formatted pubdate
echo "<pubDate>" >> $feed_name
# thanks to https://lynxbee.com/create-pubdate-tag-in-rss-xml-using-linux-date-command/#.ZA9akY7MJhF
pubDate=$(grep -oP '(?<=date: ).*' $file)
date -d "$pubDate" +"%a, %d %b %Y %H:%M:%S %z" >> $feed_name
echo "</pubDate>" >> $feed_name
# echo description of each item
echo "<description>" >> $feed_name
## wrap html content in a CDATA for rss 2.0 spec
echo "<![CDATA[" >> $feed_name
pandoc --to=html5 -o - $file | cat >> $feed_name
echo "]]>" >> $feed_name
echo "</description>" >> $feed_name
echo "</item>" >> $feed_name
done
echo -e '</channel>
</rss>' >> $feed_name
}
set_paths
check_valid
source_config
create_feed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment