summaryrefslogtreecommitdiff
path: root/update-feed.sh
blob: 5b0785b5940b8fd66e4dda092c74485e9edc7388 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env bash
set -ex

# static info
## tokens
TKN_BUILDDATE='#BUILDDATE'
TKN_PUBDATE='#PUBDATE'
TKN_TITLE='#TITLE'
TKN_LINK='#LINK'
TKN_PERMALINK='#PERMALINK'

URL_PREFIX="https://www.senders.io"
TKN_URL_STRIP="./www" # remove www file prefix for URL/LINK
TKN_INDEX_STRIP="index.html" # remove index.html for LINK

FILE_RSS_HEADER=./templates/rss/rss-header.xml
FILE_RSS_FOOTER=./templates/rss/rss-footer.xml
FILE_RSS_ITEM_HEADER=./templates/rss/rss-item-header.xml
FILE_RSS_ITEM_FOOTER=./templates/rss/rss-item-footer.xml
FILE_ITEM_OUTPUT=./rss/items.xml
FILE_RSS_OUTPUT=./www/blog/feed.rss


# dynamic info
FILE_TMP_FEED=$(mktemp --suffix=.xml)
FILE_TMP_ITEM=$(mktemp --suffix=.xml)
BUILDDATE=$(date -R)

# user input

if [ $# -ne 3 ]; then
  echo "./update-feed.sh PUBDATE TITLE FILE_PATH
    Examples:
      ./update-feed.sh 2022-12-31 \"Generating RSS feeds with bash\" ./www/blog/2022-12-31/index.html"
  exit 1
fi

PUBDATE=$(date -d "$1" -R)
TITLE=$2
FILE_PATH=$3
PERMALINK=$(echo "${FILE_PATH}" | sed -e "s,${TKN_URL_STRIP},${URL_PREFIX},g")
LINK=$(echo "${PERMALINK}" | sed -e "s,${TKN_INDEX_STRIP},,g")

# Generate TMP FEED File Header

cat -s $FILE_RSS_HEADER > $FILE_TMP_FEED
sed -i -E "s/${TKN_BUILDDATE}/${BUILDDATE}/g" $FILE_TMP_FEED
sed -i -E "s/${TKN_PUBDATE}/${PUBDATE}/g" $FILE_TMP_FEED

# Generate TMP Item File

cat -s $FILE_RSS_ITEM_HEADER > $FILE_TMP_ITEM
sed -i -E "s~${TKN_TITLE}~${TITLE}~g" $FILE_TMP_ITEM
sed -i -E "s/${TKN_PUBDATE}/${PUBDATE}/g" $FILE_TMP_ITEM
sed -i -E "s,${TKN_PERMALINK},${PERMALINK},g" $FILE_TMP_ITEM
sed -i -E "s,${TKN_LINK},${LINK},g" $FILE_TMP_ITEM
sed -n "/<article>/,/<\/article>/p" $FILE_PATH >> $FILE_TMP_ITEM
cat -s $FILE_RSS_ITEM_FOOTER >> $FILE_TMP_ITEM

# Prepend Item to items list and overwrite items file w/ prepended item
## In order to "prepend" the item (so it's on top of the others)
## We need to concat the tmp item file with the existing list, then
## we can push the contents over the existing file
## We use cat -s to squeeze the blank lines
cat -s $FILE_ITEM_OUTPUT >> $FILE_TMP_ITEM
cat -s $FILE_TMP_ITEM > $FILE_ITEM_OUTPUT

# Push items to TMP FEED
cat -s $FILE_ITEM_OUTPUT >> $FILE_TMP_FEED

# Push RSS footer to TMP FEED
cat -s $FILE_RSS_FOOTER >> $FILE_TMP_FEED
echo $FILE_TMP_FEED

# Publish feed
cat -s $FILE_TMP_FEED > $FILE_RSS_OUTPUT

echo "Finished generating feed"