about summary refs log tree commit diff stats
path: root/sfeed_html.sh
blob: 3965a8757d935938a8699df0f24d7b0cd8066c2d (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
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
#!/usr/bin/env bash

echo() { printf '%s\n' "$*"; }

html() {
	: "${LIMIT:=12}"
	: "${FRESH_DAYS:=1}"
	: "${STALE_DAYS:=4}"
	aside="$(mktemp /tmp/sfeed_html_aside.XXXXXX)"
	cat <<EOF
$(html_head)
<body>
<header>
<h1>
<a href="index.html"><img src="mars-eyes.png"
   title="$(fortune | tr -d '"')"
   width="40" height="39"
   alt="mars, but with eyes" /></a>
Planet ACDW</h1>
<p class="last-updated">last updated at <time>$(date -R)</time></p>
</header>
<nav>
<a href="#" id="stalebutton">hide stale</a>
//
<a href="feeds-short.xml">rss</a>
//
<a href="feeds.html">all</a>
(<a href="feeds.xml">rss</a>)
//
<a href="feeds.txt">twtxt</a>
//
<a href="feeds.opml">opml</a>
</nav>
<main>
$(html_main "$@")
<aside><ul id="feedlist">$(cat "$aside")</ul></aside>
</main>
<footer>
Generated by <a href="https://codemadness.org/sfeed-simple-feed-parser.html">sfeed</a>
using <a href="https://git.acdw.net/sfeed/">this configuration</a>.
<a href="mailto:planet@me.acdw.net">suggest a feed!</a>
</footer>
<script>
var staleHidden = false;
var staleItems = new Set(document.getElementsByClassName("stale"));
var staleFeeds = new Set(document.getElementsByClassName("stale_feed"));
var feedlistFeeds = new Set(document.getElementById("feedlist").children);
function hideShowStale(){
    var display;
    if (staleHidden) {
       display = null; staleHidden = false;
       flBG = null; flBB = null;
    } else {
       display = 'none'; staleHidden = true;
       flBG = 'inherit'; flBB = '1px solid';
    }
    staleItems.forEach(e => { e.style.display = display; });
    staleFeeds.forEach(e => { e.style.display = display; });
    if (window.innerWidth > 720) {
      feedlistFeeds.forEach(e => { e.style.background = flBG; e.style.borderBottom = flBB; });
    }
}
var button = document.getElementById("stalebutton");
button.addEventListener("click", function() {
    hideShowStale();
    if (staleHidden) { button.textContent = "show stale"; }
    else { button.textContent = "hide stale"; }
});
</script>
</body>
</html>
EOF
	rm "$aside"
}

html_head() {
	cat <<EOF
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Planet ACDW</title>
<link rel="stylesheet" type="text/css" href="style.css">
<link rel="shortcut icon" type="image/png" href="mars-eyes.png">
<link rel="alternate" type="application/atom+xml" title="rss (full)" href="feeds.xml">
<link rel="alternate" type="application/atom+xml" title="rss (short)" href="feeds-short.xml">
<link rel="alternate" type="application/xml" title="opml" href="feeds.opml">
<link rel="alternate" type="text/plain" title="twtxt" href="feeds.txt">
</head>
EOF
}

html_main() {
	cat <<EOF
<section id="list">
$(for file in "$@"; do html_feed "$file"; done)
</section>
EOF
}

filter_invidious() {
	# Convert youtube links to invidious links
	sed -e "s@https://www\.youtube\.com@https://piped.kavin.rocks@" \
		-e 's@https://i\.reddit\.com@https://libreddit.spike.codes@' \
		-e 's@https://twitter\.com@https://nitter.net@'
}

html_feed() { # html_feed FEED(file) => HTML
	filename="$(basename "$1")"
	now="$(date +%s)"
	fresh_secs="$((FRESH_DAYS * 24 * 60 * 60))"
	stale_secs="$((STALE_DAYS * 24 * 60 * 60))"

	## ENTRIES
	entries="$(awk -v NOW="$now" \
		-v FRESH_SECS="$fresh_secs" -v STALE_SECS="$stale_secs" \
		-v NAME="$filename" -v FNAME="$filename" \
		-v ASIDE="$aside" -v limit="$LIMIT" \
		'BEGIN { FS="\t"; fresh_feed = 0; FRESH = (NOW - FRESH_SECS); }
		FNR == 1 { nitem = 0; }
		NAME ~ /\[yt\]$/ { sub(/ \[yt\]$/, "", NAME); yt = 1; }
		function unescape(t) {
			 t = html_escape(t);
			 gsub(/\\\t/,"\t",t);
			 gsub(/\\\n/,"\n",t);
			 gsub(/\\\\/,"\\",t);
			 return t
		}
		function html_escape(t) {
			 gsub(/</,"\\&lt;",t);
			 gsub(/>/,"\\&gt;",t);
			 gsub(/&/,"\\&amp;",t);
			 return t
		}
		{
			if (limit && (++nitem > limit)) next;
			timestamp=$1;
			title=$2;
			link=$3;
			content=unescape($4);
			content_type=$5;
			id=$6;
			author=$7;
			enclosure=$8;
			category=$9;

			if (limit && (timestamp < (NOW - STALE_SECS))) next;
			show_in_sidebar = 1;
			#print timestamp, title, link > "/dev/stderr";

			date_cmd = "date -d \"@" timestamp "\" +\"%F&nbsp;%R\""
			if (timestamp) {
			   date_cmd | getline ts;
			   close(date_cmd);
			}

			fresh = (timestamp >= FRESH)
			if (fresh) fresh_feed = 1;

			print "<tr class=\"entry " (fresh ? "fresh" : "stale") "\">"
			print "<td class=\"entry-timestamp\">" ts "</td>"
			printf "%s", "<td class=\"entry-extra\">"
			if (enclosure) {
				stamp = "@"
				extra_title = " title=\"enclosure\""
				printf "%s", "<a href=\"" enclosure "\"" extra_title " target=\"_blank\">" stamp "</a>"
			}
			if ((link != id) && (id != enclosure) && (id ~ /^https?:/)) {
				stamp = "&"
				extra_title = " title=\"alternate link\""
				printf "%s", "<a href=\"" id "\"" extra_title " target=\"_blank\">" stamp "</a>"
			}
			print "</td>"
			printf "<td class=\"entry-title%s", silo_links(link)
			print "\"><a href=\"" link "\" target=\"_blank\">" title "</a></td>"
			print "</tr>"
		}
		END {
			if (show_in_sidebar) {
				printf "<li class=\"%s\">", (fresh_feed?"fresh":"stale") >> ASIDE
				printf "<a%s href=\"#%s\">%s</a></li>\n", (yt ? " class=\"yt\"" : ""), FNAME, NAME >> ASIDE
			}
			printf "%s", (stamp ? stamp : ".") > "/dev/stderr"
		}
function silo_links(link) {
	 if (link ~ /youtube\.com/) return " youtube"
	 if (link ~ /facebook\.com/) return " facebook"
	 if (link ~ /twitter\.com/) return " twitter"
	 if (link ~ /reddit\.com/) return " reddit"
	 return ""
}
' "$1" | filter_invidious)"
	if [ -z "$entries" ]; then return 1; fi
	fresh_feed='stale_feed'
	if echo "$entries" | grep -q 'class="[^"]*fresh"'; then fresh_feed='fresh_feed'; fi
	printf '<section id="%s" class="%s">\n' "$filename" "$fresh_feed"
	case "$filename" in
	*\[yt\]) yt=" class=\"yt\"" ;;
	*) yt="" ;;
	esac
	fn="$(echo "$filename" | sed 's@ \[yt\]$@@')"
	printf '<header><h2%s><a href="#%s">#</a> %s</h2>\n' "$yt" "$filename" "$fn"
	echo "<span class=\"flinks\">"
	if [ -f "$SFEED_DATA/urls/$filename" ]; then
		feed_url="$(sed -n '1p;1q' "$SFEED_DATA/urls/$filename")"
		site_url="$(sed -n '2p;2q' "$SFEED_DATA/urls/$filename")"
		[ -n "$site_url" ] && printf '<a class="site-url" href="%s">%s</a>\n//\n' "$site_url" site
		[ -n "$feed_url" ] && printf '<a class="feed-url" href="%s">%s</a>\n//\n' "$feed_url" feed
	fi
	printf '<a class="top" href="#">%s</a>' top
	echo "</span></header>"
	echo "<table class=\"entries\">"
	echo "$entries"
	echo "</table>"
	echo "</section>"
}

if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
	html "$@"
fi