aboutsummaryrefslogtreecommitdiff
path: root/blogrss
blob: 2c98b5967d29ce03249b1d9eeafa00f9da7d9177 (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
#!/usr/bin/env bash

YEAR="$(date +%Y)"

YLINK="https://wittamore.fr/articles/$YEAR"
DESC="Ramblings from Brittany, France"
RSSLINK="https://wittamore.fr/rss.xml"
FEEDNAME="/home/philip/web/rss.xml"
POSTDIR="/home/philip/web/articles/$YEAR"
UPDATED="$(date --iso-8601=ns)"

# Build RSS header & footer
header () {
echo "<?xml version='1.0' encoding='UTF-8' ?>
<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>
<channel>
<title>Bloggings - wittamore.fr</title>
<link>https://wittamore.com</link>
<description>Ramblings from Brittany, France</description>
<lastBuildDate>$UPDATED</lastBuildDate>
<atom:link href='$RSSLINK' rel='self' type='application/rss+xml' />
" >> ~/feedtop
}

footer () {
echo "
</channel>
</rss>
" >> ~/feedbottom
}

# Function: Build Item add to feed

builditem () {
  echo "<item>
  <title>$FULLTITLE</title><pubDate>$POSTDATE</pubDate>
  <link>$LINKADDR</link>
  <guid>$LINKADDR</guid>
  <category> </category>
  <description>$DESC</description>
</item>" >> ~/feed
}

# Function: Concatenate everything

combine () {
        header
        footer
        cat ~/feedtop ~/feed > ~/feedtb
        cat ~/feedtb ~/feedbottom > $FEEDNAME
        rm ~/feedtop ~/feed ~/feedtb ~/feedbottom
}

# Run through files and create rss.xml

if [[ -f $FEEDNAME ]]; then
  rm $FEEDNAME
fi
touch $FEEDNAME
mapfile -t POSTARRAY < <(ls -t "$POSTDIR"/*.html)
postNum=0
for POSTS in "${POSTARRAY[@]}"; do
  ((postNum+=1))
  POST=$POSTS
  FULLTITLE=$(grep -o '>.*</h2>' "$POST" | sed 's/\(>\|<\/h2>\)//g')
  POSTDATE=$(grep -o '>.*</h3>' "$POST" | sed 's/\(>\|<\/h3>\)//g')
  POSTNAME=${POST##*/}
  LINKADDR="$YLINK/$POSTNAME"
  DESC="$(sed -n '/<h4>.*/,/*.<\/h4>/{p;q;}' "$POST" | 
  sed -e 's/<[^>]\+>/ /g' -e 's|<h4>||g' -e 's|</h4>||g' -e 's|"||g' -e 's/^[ \t]*//;s/[ \t]*$//')"
  builditem "$POST"
done
  combine
exit