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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
|
#!/bin/sh
# Forked from https://github.com/chambln/pandoc-rss
# MIT/X Consortium License
# © 2020 Gregory Chamberlain <greg@cosine.blue>
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
SHARE=${0%/*}/../share/${0##*/}
TEMPLATES_DIR=$SHARE/data/templates
DOMAIN=${PWD##*/}
EMAIL=${EMAIL:-webmaster@$DOMAIN}
usage ()
{
cat <<EOF
Usage: ${0##*/} [OPTIONS] [FILE]...
-s Produce standalone XML
-t TITLE 'Example Feed'
-d DESCRIPTION 'Short description of the feed'
-l LINK http://example.net
-f FORMAT blog/%s.html => http://example.net/blog/%s.html
-w WEBMASTER 'webmaster@example.net (Real Name)'
-c COPYRIGHT 'Year © Name, CC BY-SA 4.0'
-n LANGUAGE en-GB
EOF
}
get_language_code ()
{
[ -n "$LANG" ] || exit 1
code=${LANG%%.*}
primary_code=${code%%_*}
subcode=${code#$primary_code}
subcode=${subcode#_}
printf %s\\n "$primary_code${subcode:+-$subcode}"
}
is_valid_language_code ()
{
arg=$(printf %s "$1" | tr [A-Z] [a-z])
while IFS=' ' read -r code _
do
[ x"$code" = x"#" ] && continue
[ x"$code" = x"$arg" ] && return 0
done < $SHARE/data/language-codes
return 1
}
get_user_fullname ()
{
while IFS=':' read -r user _ _ _ gecos _
do
if [ x"$user" = x"$LOGNAME" ]
then
user_fullname="${gecos%%,*}"
[ -n "$user_fullname" ] || exit 1
printf %s\\n "$user_fullname"
return 0
fi
done < /etc/passwd
return 1
}
get_user_contact ()
{
user_fullname="${NAME:-$(get_user_fullname)}"
printf '%s\n' "$EMAIL${user_fullname:+ ($user_fullname)}"
}
get_metadata_value ()
{
key=$1
shift
value=$(pandoc --template="$TEMPLATES_DIR/$key.value" "$@")
[ -n "$value" ] || return 1
printf %s\\n "$value"
}
get_date ()
{
input_file=$1
shift
if date=$(get_metadata_value date "$input_file")
then
if [ -f /opt/homebrew/bin/gdate ]; then
date_cmd=/opt/homebrew/bin/gdate
elif [ -f /usr/bin/date ]; then
date_cmd=/usr/bin/date
else
date_cmd=/bin/date
fi
$date_cmd --utc --date "$date" +'%a, %d %b %Y %T +0000'
fi
}
generate_feed_item ()
{
input_file=$1
shift
case $guid_format in
*%s*)
filename=${input_file##*/}
basename=${filename%.*}
guid=$(printf "$guid_format" "$basename")
set -- -V "guid:$guid" "$@"
esac
if date=$(get_date "$input_file")
then
set -- -V "date:$date" "$@"
fi
if $omit_body
then
set -- -V body: "$@"
fi
pandoc --template="$TEMPLATES_DIR/item.xml" --no-highlight "$@" "$input_file"
}
generate_feed_items ()
{
for input_file do
generate_feed_item "$input_file"
done
}
print_xml_element ()
{
[ -n "$2" ] || return 1
printf '<%s>%s</%s>\n' "$1" "$2" "$1"
}
generate_standalone_feed ()
{
if [ -z "$channel_language" ]
then
if language_code=$(get_language_code)
then
channel_language=$language_code
fi
fi
if ! is_valid_language_code "$channel_language"
then
>&2 printf 'Invalid language code: ‘%s’\n' "$channel_language"
>&2 printf 'See %s\n' "$SHARE"/data/language-codes
exit 1
fi
echo '<?xml version="1.0" encoding="UTF-8" ?>'
echo '<rss version="2.0">'
echo '<channel>'
print_xml_element title "$channel_title"
print_xml_element link "$channel_link"
print_xml_element description "$channel_description"
print_xml_element language "$channel_language"
print_xml_element webMaster "${channel_webmaster:=$(get_user_contact)}"
print_xml_element copyright "$channel_copyright"
print_xml_element docs https://www.rssboard.org/rss-specification
echo '<generator>pandoc-rss</generator>'
generate_feed_items "$@"
echo '</channel>'
echo '</rss>'
}
channel_title=$DOMAIN
channel_link=https://$DOMAIN
channel_description="Posts from $DOMAIN"
unset channel_webmaster
unset channel_copyright
unset channel_language
omit_body=false
guid_optarg=auto
standalone=false
while getopts t:l:d:w:c:n:of:hs OPT
do
case "$OPT" in
t) channel_title=$OPTARG ;;
l) channel_link=${OPTARG%/} ;;
d) channel_description=$OPTARG ;;
w) channel_webmaster=$OPTARG ;;
c) channel_copyright=$OPTARG ;;
n) channel_language=$OPTARG ;;
o) omit_body=true ;;
f) guid_optarg=$OPTARG ;;
s) standalone=true ;;
?) usage; exit 2 ;;
esac
done
shift $((OPTIND - 1))
case $guid_optarg in
auto) guid_optarg=%s.html
esac
case $guid_optarg in
omit) guid_format= ;;
*://*%s*) guid_format=$guid_optarg ;;
*%s*) [ "$channel_link" ] &&
guid_format=$channel_link/${guid_optarg#/} ;;
*) >&2 printf 'Invalid guid format ‘%s’\n' "$guid_optarg"
exit 1
esac
if $standalone
then
generate_standalone_feed "$@"
else
generate_feed_items "$@"
fi
|