aboutsummaryrefslogtreecommitdiff
path: root/phlogrss
blob: 192b2c0ef83c55e034d48d85b84366718c5c7c56 (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
#!/usr/bin/env bash

# create a gopher rss feed

phloglink="gopher://spike.nagatha.fr/0/phlog"
description="Phil's Phlog"
rsslink=gopher://spike.nagatha.fr/phlog/rss.xml
filename=$HOME/src/gopher/phlog/rss.xml
postdir=$HOME/src/gopher/phlog
builddate=$(date --iso-8601=ns)
buildyear=$(date +%Y)

# Build RSS header & footer
build_header () {
echo "<?xml version='1.0' encoding='UTF-8' ?>
<rss version='2.0' xmlns:atom='http://www.w3.org/2005/Atom'>" > ~/header
echo "<channel>
<title>Phil&#39;s Phlog</title>
<link>gopher://spike.nagatha.fr/1/phlog</link>
<atom:link href='$rsslink' rel='self' type='application/rss+xml' />
<language>en-us</language>
<copyright>© $buildyear Philip A. Wittamore - All Rights Reserved</copyright>
<lastBuildDate>$builddate</lastBuildDate>
<description>A Gopher phlog from Brittany, France</description>
" >> ~/header
}

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

build_item () {
  echo "<item>
  <title>$title</title>
  <pubDate>$postdate</pubDate>
  <link>$linkadd</link>
  <guid>$linkadd</guid>
  <description>$description</description>
</item>" >> ~/feed
}

build_xml_file () {
        build_header
        build_footer
        cat ~/header ~/feed > ~/rss_tmp
        cat ~/rss_tmp ~/footer > "$filename"
        rm ~/header ~/feed ~/rss_tmp ~/footer
}

# list files and create rss.xml

if [[ -f $filename ]]; then
  rm "$filename"
fi

touch "$filename"
mapfile -t post_array < <(ls -t "$postdir"/*.txt)
postNum=0

for posts in "${post_array[@]}"; do
  ((postNum+=1))
  post=$posts
  title=$(head -n1 "$post" | sed 's/title://g')
  postdate=$(cat "$post" | sed -n '2p' | sed 's/date://g')
  postname=${post##*/}
  linkadd="$phloglink/$postname"
  description="$(cat "$post" | sed -n "/^$/,/^$/p" | head --lines 3) …"
  build_item "$post"
done

  # put it all together
  build_xml_file

exit