cdaniels.net/bin/rssg -rwxr-xr-x 3.9 KiB View raw
                                                                                
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
#!/bin/sh
#
# https://www.romanzolotarev.com/bin/rssg
# Copyright 2018 Roman Zolotarev <hi@romanzolotarev.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#
set -e


main () {
	test -n "$1" || usage
	test -f "$1" || no_file "$1"

	index_file="$1"
	test -z "${index_file##*html}" && html=$(cat "$index_file")
	test -z "${index_file##*md}" && html=$(md_to_html "$index_file")
	test -n "$html" || usage

	base="$(dirname "$(readlink -f "$index_file")")"
	base_url="$(echo "$html" | get_base_url)"

	url=$(		echo "$html" | get_url)
	title=$(	echo "$html" | get_title)
	description=$(	echo "$html" | get_description |
			remove_tags |
			remove_nbsp )

	items=$(	echo "$html" | get_items)

	rss=$(		echo "$items" |
			render_items "$base" "$base_url" |
			render_feed "$url" "$title" "$description")

	>&2 echo "[rssg] ${index_file##$(pwd)/} $(echo "$rss" | grep -c '<item>') items"
	echo "$rss"
}


usage() {
	echo "usage: ${0##*/} index.{html,md} > rss.xml" >&2
	exit 1
}


no_file() {
	echo "${0##*/}: $1: No such file" >&2
	exit 2
}


md_to_html() {
	test -x "$(which lowdown)" || exit 3
	lowdown \
	-D html-skiphtml \
	-D smarty \
	-d metadata \
	-d autolink "$1"
}


get_base_url() {
	get_url | sed 's#\(.*\)/[^/]*#\1#'
}


get_title() {
	awk 'tolower($0)~/^<h1/{gsub(/<[^>]*>/,"",$0);print;exit}'
}


get_url() {
	grep -i '<a .*rss' | head -1 |
	sed 's#.*href="\(.*\)".*#\1#'
}


get_items() {
	grep -i 'href=".*" title="' |
	sed 's#.*href="\(.*\)" title="\(.*\)">\(.*\)</a>.*#\1 \2 \3#'
}


get_description() {
	start='sub("^.*<"s"*"t"("s"[^>]*)?>","")'
	stop='sub("</"s"*"t""s"*>.*","")&&x=1'
	awk -v 's=[[:space:]]' -v 't=[Pp]' "$start,$stop;x{exit}"
}

remove_tags() {
	sed 's#<[^>]*>##g;s#</[^>]*>##g'
}


remove_nbsp() {
	sed 's#\&nbsp;# #g'
}


rel_to_abs_urls() {
	site_url="$1"
	base_url="$2"

	abs='s#(src|href)="/([^"]*)"#\1="'"$site_url"/'\2"#g'
	rel='s#(src|href)="([^:/"]*)"#\1="'"$base_url"/'\2"#g'
	sed -E "$abs;$rel"
}


date_rfc_822() {
	if [ "$(uname)" = "Linux" ] ; then
		date '+%a, %d %b %Y %H:%M:%S %z'
	else
		date -j '+%a, %d %b %Y %H:%M:%S %z' \
		"$(echo "$1"| tr -cd '[:digit:]')0000"
	fi
}


render_items() {
	while read -r i
	do render_item "$1" "$2" "$i"
	done
}


render_item() {
	base="$1"
	base_url="$2"
	item="$3"

	site_url="$(echo "$base_url"| sed 's#\(.*//.*\)/.*#\1#')"

	date=$(echo "$item"|awk '{print$2}')
	url=$(echo "$item"|awk '{print$1}')

	f="$base/$url"
	test -f "$f" && html=$(cat "$f")
	test -f "${f%\.html}.md" && html=$(md_to_html "${f%\.html}.md")

	description=$(
		echo "$html" |
		rel_to_abs_urls "$site_url" "$base_url" |
		remove_nbsp
	)
	title=$(echo "$description" | get_title)

	echo '
<item>
<guid>'"$base_url/$url"'</guid>
<link>'"$base_url/$url"'</link>
<pubDate>'"$(date_rfc_822 "$date")"'</pubDate>
<title>'"$title"'</title>
<description><![CDATA[

'"$description"'

]]></description>
</item>'
}


render_feed() {
	url="$1"
	title="$2"
	description="$3"

	base_url="$(echo "$url" | cut -d '/' -f1-3)"

	echo '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="'"$url"'" rel="self" type="application/rss+xml" />
<title>'"$title"'</title>
<description>'"$description"'</description>
<link>'"$base_url"'/</link>
<lastBuildDate>'"$(date_rfc_822 date)"'</lastBuildDate>
'"$(cat)"'
</channel></rss>'
}


main "$@"