about summary refs log tree commit diff stats
path: root/shatom
diff options
context:
space:
mode:
Diffstat (limited to 'shatom')
-rwxr-xr-xshatom164
1 files changed, 164 insertions, 0 deletions
diff --git a/shatom b/shatom new file mode 100755 index 0000000..48b99ff --- /dev/null +++ b/shatom
@@ -0,0 +1,164 @@
1#!/bin/sh
2# generate a Atom feed from a directory, recursively
3# using POSIX sh
4# AUTHOR: Case Duckworth <acdw@acdw.net>
5# LICENSE: MIT
6
7usage() {
8 cat <<END
9$0: generate an Atom feed from directories of files
10INVOCATION:
11 $0 [-h] [-c CONFIG] DIRECTORY...
12
13OPTIONS:
14 -h show this help
15 -c CONFIG change the CONFIG file.
16 Default: $PWD/$0.conf.sh
17END
18}
19
20# FEED VARIABLES
21FEED_TITLE=example
22FEED_SUBTITLE="an example"
23FEED_URL="https://example.com/atom.xml"
24SITE_URL="https://example.com"
25FEED_ID="${SITE_URL#*//}"
26FEED_AUTHOR="nobody"
27FEED_COPYRIGHT="(c) 2020 CC-BY-SA $FEED_AUTHOR"
28FEED_UPDATED=$(date -u +'%FT%TZ')
29
30# FUNCTIONS
31
32recent_files() { # recent_files DIRECTORY FIND_ARG...
33 # list files, in order of recency
34 # BOO: REQUIRES GNU FIND :( :( :( :(
35 # possibly look into https://unix.stackexchange.com/questions/9247/
36 # for more possibilities if you don't have GNU find.
37 dir="$1"
38 shift
39
40 find "$dir" -maxdepth 1 \
41 "$@" \
42 -not -name '.*' \
43 -printf '%T@\t%p\n' |
44 sort -nr |
45 cut -f2
46}
47
48# ENTRY FUNCTIONS
49
50skip_entry() {
51 return 1
52}
53
54entry_url() {
55 basename="${1#$DIRECTORY}"
56 echo "$FEED_URL/$basename"
57}
58
59entry_title() {
60 awk '/^#+[ ]\S/{
61 for(i=2;i<=NF;i++) {
62 printf $i;
63 if (i!=NF) printf " ";
64 }
65 printf "\n";exit}' "$1"
66}
67
68entry_summary() {
69 return 1
70}
71
72entry_author() {
73 echo "$FEED_AUTHOR"
74}
75
76entry_content() {
77 cat "$1"
78}
79
80entry_updated() {
81 # requires stat(1).
82 # probably another way.
83 # possibly using ls(1).
84 stat -c '%y' "$1" |
85 awk '{
86 sub(/\..*/,"",$2);
87 sub(/[0-9][0-9]/,"&:",$3);
88 print $1"T"$2$3;}'
89}
90
91# ATOM FUNCTIONS
92
93atom_header() {
94 cat <<END
95<?xml version="1.0" encoding="utf-8"?>
96<feed xmlns="http://www.w3.org/2005/Atom">
97<title>$FEED_TITLE</title>
98<subtitle>$FEED_SUBTITLE</subtitle>
99<link href="$FEED_URL" rel="self" />
100<link href="$SITE_URL" />
101<id>$FEED_ID</id>
102<generator uri="https://git.sr.ht/~acdw/shatom" version="infinite">shatom</generator>
103<rights>$FEED_COPYRIGHT</rights>
104<updated>$FEED_UPDATED</updated>
105END
106}
107
108atom_footer() {
109 cat <<END
110</feed>
111END
112}
113
114atom_entry() { # atom_entry FILE
115 ENTRY_URL="$(entry_url "$1")"
116 ENTRY_TITLE="$(entry_title "$1")"
117 ENTRY_SUMMARY="$(entry_summary "$1")"
118 ENTRY_AUTHOR="$(entry_author "$1")"
119 ENTRY_CONTENT="$(entry_content "$1")"
120 ENTRY_UPDATED="$(entry_updated "$1")"
121 cat <<END
122<entry>
123<id>$ENTRY_URL</id>
124<link rel="alternate" href="$ENTRY_URL" />
125<title>$ENTRY_TITLE</title>
126<summary>$ENTRY_SUMMARY</summary>
127<updated>$ENTRY_UPDATED</updated>
128<author><name>$ENTRY_AUTHOR</name></author>
129<content type="text"><![CDATA[$ENTRY_CONTENT]]></content>
130</entry>
131END
132}
133
134main() {
135 CONFIGFILE="$PWD/$0.conf.sh"
136 case "$1" in
137 -h)
138 usage
139 exit 0
140 ;;
141 -c)
142 CONFIGFILE="$2"
143 shift 2
144 ;;
145 esac
146
147 if [ -f "CONFIGFILE" ]; then
148 . "$CONFIGFILE"
149 fi
150
151 atom_header
152 for DIR; do
153 for entry in $(recent_files "$DIR" -type f); do
154 if skip_entry "$entry"; then continue; fi
155
156 atom_entry "$entry"
157 done
158 done
159 atom_footer
160}
161
162if [ $DEBUG ]; then set -x; fi
163
164main "$@"