shatom/shatom

165 lines
3.3 KiB
Plaintext
Raw Permalink Normal View History

2020-11-19 02:06:54 +00:00
#!/bin/sh
# generate a Atom feed from a directory, recursively
# using POSIX sh
# AUTHOR: Case Duckworth <acdw@acdw.net>
# LICENSE: MIT
usage() {
cat <<END
$0: generate an Atom feed from directories of files
INVOCATION:
2021-02-06 20:04:44 +00:00
$0 [-h] [-c CONFIG] DIRECTORY...
2020-11-19 02:06:54 +00:00
OPTIONS:
2021-02-06 20:04:44 +00:00
-h show this help
-c CONFIG change the CONFIG file.
Default: $PWD/$0.conf.sh
2020-11-19 02:06:54 +00:00
END
}
# FEED VARIABLES
FEED_TITLE=example
FEED_SUBTITLE="an example"
FEED_URL="https://example.com/atom.xml"
2020-11-19 02:39:36 +00:00
SITE_URL="https://example.com"
FEED_ID="${SITE_URL#*//}"
FEED_AUTHOR="nobody"
FEED_COPYRIGHT="(c) 2020 CC-BY-SA $FEED_AUTHOR"
2020-11-19 02:06:54 +00:00
FEED_UPDATED=$(date -u +'%FT%TZ')
# FUNCTIONS
recent_files() { # recent_files DIRECTORY FIND_ARG...
# list files, in order of recency
# BOO: REQUIRES GNU FIND :( :( :( :(
# possibly look into https://unix.stackexchange.com/questions/9247/
# for more possibilities if you don't have GNU find.
dir="$1"
shift
find "$dir" -maxdepth 1 \
"$@" \
-not -name '.*' \
-printf '%T@\t%p\n' |
sort -nr |
cut -f2
}
# ENTRY FUNCTIONS
skip_entry() {
return 1
}
entry_url() {
basename="${1#$DIRECTORY}"
echo "$FEED_URL/$basename"
}
entry_title() {
2021-02-06 20:04:44 +00:00
awk '/^#+[ ]\S/{
for(i=2;i<=NF;i++) {
printf $i;
if (i!=NF) printf " ";
}
printf "\n";exit}' "$1"
2020-11-19 02:06:54 +00:00
}
entry_summary() {
return 1
}
entry_author() {
2020-11-19 02:39:36 +00:00
echo "$FEED_AUTHOR"
2020-11-19 02:06:54 +00:00
}
entry_content() {
cat "$1"
}
entry_updated() {
# requires stat(1).
# probably another way.
# possibly using ls(1).
stat -c '%y' "$1" |
awk '{
2021-02-06 20:04:44 +00:00
sub(/\..*/,"",$2);
sub(/[0-9][0-9]/,"&:",$3);
print $1"T"$2$3;}'
2020-11-19 02:06:54 +00:00
}
# ATOM FUNCTIONS
atom_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>
2021-02-06 20:04:44 +00:00
<generator uri="https://git.sr.ht/~acdw/shatom" version="infinite">shatom</generator>
2020-11-19 02:06:54 +00:00
<rights>$FEED_COPYRIGHT</rights>
<updated>$FEED_UPDATED</updated>
END
}
atom_footer() {
cat <<END
</feed>
END
}
atom_entry() { # atom_entry FILE
ENTRY_URL="$(entry_url "$1")"
ENTRY_TITLE="$(entry_title "$1")"
ENTRY_SUMMARY="$(entry_summary "$1")"
ENTRY_AUTHOR="$(entry_author "$1")"
ENTRY_CONTENT="$(entry_content "$1")"
ENTRY_UPDATED="$(entry_updated "$1")"
cat <<END
<entry>
<id>$ENTRY_URL</id>
<link rel="alternate" href="$ENTRY_URL" />
<title>$ENTRY_TITLE</title>
<summary>$ENTRY_SUMMARY</summary>
<updated>$ENTRY_UPDATED</updated>
<author><name>$ENTRY_AUTHOR</name></author>
<content type="text"><![CDATA[$ENTRY_CONTENT]]></content>
</entry>
END
}
main() {
2021-02-06 20:04:44 +00:00
CONFIGFILE="$PWD/$0.conf.sh"
2020-11-19 02:06:54 +00:00
case "$1" in
-h)
usage
exit 0
;;
-c)
CONFIGFILE="$2"
shift 2
;;
esac
2020-11-19 02:39:36 +00:00
if [ -f "CONFIGFILE" ]; then
. "$CONFIGFILE"
fi
2020-11-19 02:06:54 +00:00
atom_header
for DIR; do
for entry in $(recent_files "$DIR" -type f); do
2020-11-19 02:39:36 +00:00
if skip_entry "$entry"; then continue; fi
2020-11-19 02:06:54 +00:00
atom_entry "$entry"
done
done
atom_footer
}
2020-11-19 02:39:36 +00:00
if [ $DEBUG ]; then set -x; fi
main "$@"