blob: 05b6052d8779d9f2ae6f5501151259a0c6b95f9f (
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
#!/bin/sh
# SHATOM: generate a feed from a directory of files
# Copyright (C) 2020--2022 Case Duckworth <acdw@acdw.net>
# Licensed under the Good Choices License. See COPYING for details.
### Entry point
usage() {
cat <<EOF
SHATOM: generate a web feed from directories, recursively
USAGE: shatom -h
shatom [-c CONFIG] [-C DIR] [VAR=VALUE...] DIRECTORY...
FLAGS:
-h Show this help and exit.
OPTIONS:
-c CONFIG Specify shatom's configuration path, a shell
script overriding functions in shatom itself.
Default: \$PWD/shatom.conf.sh
-C DIR Change the current working directory to DIR
before executing the rest of the script.
PARAMETERS:
VAR=VALUE... Override configuration variables for this
invocation of shatom. These are eval'd, so
be careful!
DIRECTORY... Directory(s) containing feed content. These
can be files as well, but really directories
makes more sense, don't you think?
EOF
exit "${1:-0}"
}
main() {
# Command-line flags
while getopts hc:C: opt; do
case "$opt" in
h) usage ;;
c) CONFIG="$OPTARG" ;;
C) cd "$OPTARG" || exit 2 ;;
*) usage 1 ;;
esac
done
shift $((OPTIND - 1))
# Configuration file
if [ -f "${CONFIG:=$PWD/shatom.conf.sh}" ]; then
# shellcheck source=/dev/null
. "$CONFIG"
fi
# Command-line variable assignment
for arg; do
case "$arg" in
*=*) eval "$arg" ;;
*) break ;;
esac
done
# Finally, default values for variables
FEED_TITLE="${FEED_TITLE:-example}"
FEED_SUBTITLE="${FEED_SUBTITLE:-an example}"
FEED_AUTHOR="${FEED_AUTHOR:-nobody}"
FEED_COPYRIGHT="${FEED_COPYRIGHT:-Copyright (C) $(date +%Y) $FEED_AUTHOR}"
FEED_URL="${FEED_URL:-https://example.com/feed.xml}"
FEED_ID="${FEED_ID:-${FEED_URL#*//}}"
FEED_UPDATED="${FEED_UPDATED:-$(date -u +'%F %TZ')}"
SITE_URL="${SITE_URL:-https://example.com/}"
# Add PWD if no directories are given.
if [ $# -eq 0 ]; then
set -- .
fi
# Do the damn thing already
feed_header
for DIR; do
recent_files "$DIR" -type f |
while read -r item; do
skip_item "$item" && continue
feed_item "$item"
done
done
feed_footer
}
### Library functions
## File-listing functions
recent_files() {
# requires stat(1)
dir="$1"
shift
find "$dir" "$@" \
-exec stat -c '%Y %n' '{}' + |
sort -k1,1 -nr |
cut -d' ' -f2-
}
## Feed-level functions
# by default, generate Atom feeds
# (https://datatracker.ietf.org/doc/html/rfc4287), but an RSS configuration file
# is included in rss.conf.sh.
feed_header() {
cat <<END
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>$FEED_TITLE</title>
<subtitle>$FEED_SUBTITLE</subtitle>
<link href="$FEED_URL" rel="self" />
<link href="$SITE_URL" />
<id>$FEED_ID</id>
<generator uri="https://git.sr.ht/~acdw/shatom" version="infinite">shatom</generator>
<rights>$FEED_COPYRIGHT</rights>
<updated>$FEED_UPDATED</updated>
END
}
feed_footer() {
cat <<END
</feed>
END
}
feed_item() {
ITEM_URL="$(item_url "$1")"
cat <<END
<entry>
<id>$ITEM_URL</id>
<link rel="alternate" href="$ITEM_URL" />
<title>$(item_title "$1")</title>
<summary>$(item_summary "$1")</summary>
<updated>$(item_updated "$1")</updated>
<author>$(item_author)</author>
<content type="text"><![CDATA[$(item_content "$1")]]></content>
</entry>
END
}
## Item-level functions
# These can all be, and in fact should be, changed in the configuration file.
skip_item() {
return 1
}
item_url() {
basename="${1#$DIRECTORY}"
echo "$FEED_URL/$basename"
}
item_title() {
basename="${1#$DIRECTORY}"
echo "$basename"
}
item_summary() {
return 1
}
item_author() {
echo "$FEED_AUTHOR"
}
item_content() {
cat "$1"
}
item_updated() {
# requires stat(1)
stat -c %y "$1"
}
### Execution section
test -n "$DEBUG" && set -x
test -n "$SOURCE" || main "$@"
|