aboutsummaryrefslogtreecommitdiff
path: root/blogrss
blob: 36c90e383b210598d1cfc0acad9d3693b7018380 (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
80
81
82
83
84
85
86
87
#!/usr/bin/env bash


# settings
TYEAR="$(date +%Y)"
TITLE="Bloggings"
YLINK="https://wittamore.fr/articles/$TYEAR"
FDESC="Ramblings from Brittany"
WSITE="https://wittamore.fr"
RLINK="$WSITE/rss.xml"
FEEDN="/home/philip/web/rss.xml"
ARTDR="/home/philip/web/articles/$TYEAR"
UPDAT="$(date --iso-8601=ns)"

# convert tring to htmlentities
htmlentities () {
	echo $1 | sed 's/&/\&amp;/g; s/</\&lt;/g; s/>/\&gt;/g; s/"/\&quot;/g; s/'"'"'/\&#39;/g'
}

# 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>$TITLE</title>
<link>$WSITE</link>
<description>$FDESC</description>
<lastBuildDate>$UPDAT</lastBuildDate>
<atom:link href='$RLINK' rel='self' type='application/rss+xml' />
" >> ~/feedtop
}

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

# Function: Build Item add to feed

builditem () {
  echo "<item>
  <title>$POSTTIT</title>
  <pubDate>$POSTDAT</pubDate>
  <author>Philip A. Wittamore</author>
  <link>$POSTADD</link>
  <guid>$POSTADD</guid>
  <category> </category>
  <description><![CDATA[$POSTDES]]></description>
</item>" >> ~/feed
}

# Function: Concatenate everything

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

# Run through files and create rss.xml

if [[ -f $FEEDN ]]; then
  rm $FEEDN
fi
touch $FEEDN
mapfile -t ARTICLEARRAY < <(ls -t "$ARTDR"/*.html)
postNum=0
for POSTS in "${ARTICLEARRAY[@]}"; do
  ((postNum+=1))
  POST=$POSTS
  POSTTIT="$(grep -o '<h2>.*</h2>' $POST | sed 's/\(<h2>\|<\/h2>\)//g')"
  POSTDAT="$(grep -o '<h3>.*</h3>' $POST | sed 's/\(<h3>\|<\/h3>\)//g')"
  # extract from first <h4> to last </p>
  POSTDES="$(awk '/<h4>/,/<\/p>/' $POST)"    
  POSTNAM="$(htmlentities "${POST##*/}")"
  POSTTIT="$(htmlentities "$POSTTIT")"
  POSTDES="$(htmlentities "$POSTDES")"
  POSTADD="$YLINK/$POSTNAM"
  builditem "$POST"
done
  combine
exit