about summary refs log tree commit diff stats
path: root/sfeed_html.awk
blob: 132879eb10dd4fa7ccdf11a7409816e47c484dc0 (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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
#!/bin/awk -f
# Convert sfeed(1) formatted files into an HTML webpage.
# Usage: sfeed_html.awk -- FILES...

## Set up variables
BEGIN {
	# Sidebar
	if (! ASIDE) {
		ASIDE = envor("SFEED_HTML_ASIDE", "/tmp/sfeed-aside.html")
	}
	print > ASIDE	# clear out ASIDE
	in_aside = 0	# Don't show the feed in the sidebar
	# Formatting
	if (! DATEFMT) {
		ddf = "<time title=\"%F %R UTC\">"
		ddf = ddf "<span class=\"date\">%m-%d</span>" " "
		ddf = ddf "<span class=\"time\">%R</span>"
		ddf = ddf "</time>"
		DATEFMT = envor("SFEED_HTML_DATEFMT", ddf)
	}
	if (! TITLE) {
		TITLE = envor("SFEED_HTML_TITLE", "Planet ACDW")
	}
	if (! SEPARATOR) {
		SEPARATOR = envor("SFEED_HTML_SEPARATOR", "//")
	}
	if (! ALT_LINK_STAMP) {
		ALT_LINK_STAMP = envor("SFEED_ALT_LINK_STAMP", "&")
	}
	if (! ENCLOSURE_STAMP) {
		ENCLOSURE_STAMP = envor("SFEED_ENCLOSURE_STAMP", "@")
	}
	if (! SILO_STAMP) {
		SILO_STAMP = envor("SFEED_SILO_STAMP", "%")
	}
	# Limiting posts...
	## by time
	if (! NOW) {
		datecmd = "date +%s"
		datecmd | getline NOW
		close(datecmd)
	}
	if (! FRESHDAYS) {
		FRESHDAYS = envor("SFEED_FRESHDAYS", 1.5)
	}
	if (! STALEDAYS) {
		STALEDAYS = envor("SFEED_STALEDAYS", 4)
	}
	fresh_secs = FRESHDAYS * 24 * 60 * 60
	stale_secs = STALEDAYS * 24 * 60 * 60
	fresh_age = (NOW - fresh_secs)
	stale_age = (NOW - stale_secs)
	## by number
	if (! LIMIT) {
		# If LIMIT == -1, ignore time limit as well.
		LIMIT = envor("SFEED_LIMIT", 20)
	}
	# Alternate URLs for siloed content
	if (! YOUTUBE_ALT_URL) {
		YOUTUBE_ALT_URL = envor("SFEED_YOUTUBE_ALT_URL", "https://piped.kavin.rocks")
	}
	if (! TWITTER_ALT_URL) {
		TWITTER_ALT_URL = envor("SFEED_TWITTER_ALT_URL", "https://nitter.net")
	}
	if (! REDDIT_ALT_URL) {
		REDDIT_ALT_URL = envor("SFEED_REDDIT_ALT_URL", "https://libreddit.de")
	}
	if (! MEDIUM_ALT_URL) {
		MEDIUM_ALT_URL = envor("SFEED_MEDIUM_ALT_URL", "https://scribe.rip")
	}
	# Paywalls
	PAYWALL_LADDER = "https://archive.ph/newest/"
	PAYWALLED[1] = "washingtonpost\\.com"
	PAYWALLED[2] = "nytimes\\.com"
	# Awk and convenience constants
	FS = "\t"
	STDERR = "/dev/stderr"
}

BEGIN {
	html_top()
}

FNR == 1 {
	if (file_count) {
		# End the previous file before beginning the current one
		end_file()
	}
	# Filename
	name = FILENAME
	sub(/.*\//, "", name)
	safe_name = name
	sub(/\//, "_", safe_name)
	dir = FILENAME
	sub(/[^\/]*$/, "", dir)
	if (name ~ /\[yt\]$/) {
		sub(/ \[yt\]$/, "", name)
		yt = " class=\"yt\""
	} else {
		yt = ""
	}
	# extra metadata, yaaaaay
	feed_url = ""
	site_url = ""
	URLS = envor("SFEED_DATA", ENVIRON["HOME"] "/.sfeed") "/urls/" safe_name
	getline feed_url < URLS
	getline site_url < URLS
	getline feed_tags < URLS
	# put more lines to read here
	close(URLS)
	# State variables
	stamp = "."
	buf = ""
	ni = 0
	stale_total += stale_count
	stale_count = 0
	fresh_total += fresh_count
	fresh_count = 0
	item_total += item_count
	item_count = 0
	file_count++
	siloed = 0
}

{
	# Skip if we have too many items
	if ((LIMIT > 0) && (++ni > LIMIT)) {
		next
	}
	# Collect fields
	timestamp = $1
	title = $2
	silo = $3
	link = silo_link(silo)
	content = unescape($4)
	content_type = $5
	id = $6
	author = $7
	enclosure = $8
	category = $9
	# Skip if the item is too old
	if ((LIMIT >= 0) && (timestamp < stale_age)) {
		next
	}
	# Otherwise, we're showing it
	stale_count++
	# Is this item fresh?
	if (timestamp >= fresh_age) {
		is_fresh = 1
		stamp = "!"
		fresh_count++
	} else {
		is_fresh = 0
	}
	# Debugging comment
	# bufprint("<!-- timestamp title silolink type id author enclosure category -->")
	# bufprint("<!-- " $1 " -- " $2 " -- " $3 " -- " $5 " -- " $6 " -- " $7 " -- " $8 " -- " $9 " -->")
	# Print!
	bufprint("<tr class=\"entry " (is_fresh ? "fresh" : "stale") "\">")
	# Timestamp
	datecmd = "date -d \"@" timestamp "\" +'" DATEFMT "'"
	if (timestamp) {
		datecmd | getline timestamp
		close(datecmd)
	}
	bufprint("<td class=\"entry-timestamp\"><time>" timestamp "</time></td>")
	# Extra links
	bufprint("<td class=\"entry-extra\">")
	if (siloed) {
		# "siloed" links like youtube, facebook, etc. --- I convert them
		# to more privacy-friendly links, but sometimes those don't
		# work. TODO: also try to circumvent paywalls.
		stamp = SILO_STAMP
		print_link(silo, "Silo: " silo, stamp)
	}
	if (enclosure) {
		# enclosures --- podcast files, etc.
		stamp = ENCLOSURE_STAMP
		print_link(enclosure, "Enclosure: " enclosure, stamp)
	}
	# Test id against silo here, since silo is the raw link value.
	if ((id != silo) && (id != enclosure) && (id ~ /^https?:/)) {
		# alternate links (comments, etc.)
		stamp = ALT_LINK_STAMP
		print_link(silo_link(id), "alternate link", stamp)
	}
	bufprint("</td>")
	# Title
	bufprint("<td class=\"entry-title" silo_class(silo) "\">")
	# bufprint("<details><summary>", 0)
	print_link(link, "", title)
	# bufprint("</summary>")
	# bufprint(content)
	# bufprint("</details>")
	bufprint("</td>")
	# End row
	bufprint("</tr>")
	item_count++
}

END {
	# End the last file and the #list section
	end_file()
	print "</section>"
	# Sidebar
	print "<aside><ul id=\"feedlist\">"
	system("cat " ASIDE)
	close(ASIDE)
	print "</ul></aside>"
	# End of <main>
	print "</main>"
	# footer
	printf "<footer>"
	print "Generated by <a href=\"https://codemadness.org/sfeed-simple-feed-parser.html\">sfeed</a>"
	print "using <a href=\"https://git.acdw.net/sfeed/\">this configuration</a>."
	print "<a href=\"mailto:planet@me.acdw.net\">suggest&nbsp;a&nbsp;feed!</a></footer>"
	# end of HTML
	print "<script src=\"script.js\"> </script>"
	print "</body>"
	print "</html>"
	printf(SEPARATOR) > STDERR
}


function bufprint(text, sep)
{
	buf = buf text (sep ? sep : ((sep == 0) ? "" : "\n"))
	sep = ""
}

function end_file()
{
	if (! item_count) {
		return 1
	}
	# Header
	printf "<section id=\"%s\" class=\"feed %s\">\n", name, (fresh_count ? "fresh" : "stale")
	printf "<header><h2%s><a class=\"anchor\" href=\"#%s\">#</a> %s</h2>\n", yt, name, name
	# Feed links
	printf "<nav class=\"flinks\">"
	if (site_url) {
		#print_link(site_url, "", (yt ? "chan" : "site"), "site-url")
		printf "<a class=\"site-url\" href=\"%s\">%s</a>\n", site_url, (yt ? "chan" : "site")
	}
	if (feed_url) {
		#print_link(feed_url, "", "feed", "feed-url")
		printf "<a class=\"feed-url\" href=\"%s\">feed</a>\n", feed_url
	}
	# Top link
	#print_link("#", "back to top", "top", "top")
	printf "<a class=\"top\" href=\"#\">top</a>"
	printf "</nav>"
	printf "</header>\n"
	# Feed entries
	printf "<table class=\"entries\">\n"
	printf "%s", buf
	printf "</table>\n</section>\n"
	# Sidebar
	if (stale_count) {
		printf("<li class=\"%s\">", (fresh_count ? "fresh" : "stale")) >> ASIDE
		printf("<a href=\"#%s\"%s>%s</a></li>\n", name, (yt ? " class=\"yt\"" : ""), name) >> ASIDE
	}
	# Log
	printf("%s", stamp) > STDERR
}

function envor(var, def)
{
	return (ENVIRON[var] ? ENVIRON[var] : def)
}

function html_escape(t)
{
	gsub(/</, "\\&lt;", t)
	gsub(/>/, "\\&gt;", t)
	gsub(/&/, "\\&amp;", t)
	return t
}

function html_top()
{
	# Print HTML header
	fortunecmd = "fortune"
	fortunecmd | getline LOGO_TITLE
	close(fortunecmd)
	sub(/"/, "", LOGO_TITLE)
	datecmd = "date -u +'" DATEFMT "'"
	datecmd | getline UPDATE_TIME
	close(datecmd)
	print "<!DOCTYPE html>"
	print "<html>"
	# <head>
	print "<head>"
	print "<meta charset=\"utf-8\">"
	print "<meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge\">"
	print "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=UTF-8\">"
	print "<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">"
	print "<title>" TITLE "</title>"
	print "<link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\">"
	print "<link rel=\"shortcut icon\" type=\"image/png\" href=\"mars-eyes.png\">"
	print "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"rss (full)\" href=\"feeds.xml\">"
	print "<link rel=\"alternate\" type=\"application/atom+xml\" title=\"rss (short)\" href=\"feeds-short.xml\">"
	print "<link rel=\"alternate\" type=\"application/xml\" title=\"opml\" href=\"feeds.opml\">"
	print "<link rel=\"alternate\" type=\"text/plain\" title=\"twtxt\" href=\"feeds.txt\">"
	print "<link rel=\"alternate\" type=\"text/plain\" title=\"ass\" href=\"feed.ass\">"
	print "</head>"
	print "<body>"
	# <header>
	print "<header id=\"page-header\">"
	print "<h1>"
	printf "<a id=\"logo\" href=\"index.html\"><img src=\"mars-eyes.png\""
	printf " title=\"%s\" ", LOGO_TITLE
	printf "width=\"40\" height=\"39\" "
	print "alt=\"mars, but with eyes\" /></a>"
	print TITLE "</h1>"
	print "<p id=\"last-updated\">last updated at <time>" UPDATE_TIME "</time></p>"
	print "</header>"
	# <nav>
	print "<nav id=\"page-nav\">"
	print "<a href=\"#\" id=\"stalebutton\">hide stale</a>", SEPARATOR
	print "<a href=\"feeds-short.xml\">rss</a>", SEPARATOR
	print "<a href=\"feeds.html\">all</a>", "(<a href=\"feeds.xml\">rss</a>)", SEPARATOR
	print "<a href=\"feeds.txt\">twtxt</a>", SEPARATOR
	print "<a href=\"feed.ass\">ass</a>", SEPARATOR
	print "<a href=\"feeds.opml\">opml</a>"
	print "</nav>"
	# first part of <main>
	print "<main>"
	print "<section id=\"list\">"
}

function print_link(href, title, text, class)
{
	l = "<a href=\"" href "\""
	if (class) {
		l = l " class=\"" class "\""
	}
	l = l " title=\"" title "\""
	l = l " rel=\"nofollow\" target=\"_blank\">"
	l = l text
	l = l "</a>"
	bufprint(l, 0)
}

function silo_class(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"
	}
	if (link ~ /meedium\.com/) {
		return "medium"
	}
	return ""
}

function silo_link(link)
{
	ret = link
	http = "https?://[^\\.]*\\.?"
	siloed = 1
	if (ret ~ (http "youtube\\.com")) {
		sub(http "youtube\\.com", YOUTUBE_ALT_URL, ret)
	} else if (ret ~ (http "i\\.reddit\\.com")) {
		sub(http "i\\.reddit\\.com", REDDIT_ALT_URL, ret)
	} else if (ret ~ (http "twitter\\.com")) {
		sub(http "twitter\\.com", TWITTER_ALT_URL, ret)
	} else if (ret ~ (http "medium\\.com")) {
		sub(http "medium\\.com", MEDIUM_ALT_URL, ret)
	} else {
		siloed = 0
		for (site in PAYWALLED) {
			if (link ~ (http PAYWALLED[site])) {
				ret = PAYWALL_LADDER link
				siloed = 1
				break
			}
		}
	}
	return ret
}

function unescape(t)
{
	# t = html_escape(t)
	gsub(/\\t/, "\t", t)
	gsub(/\\n/, "\n", t)
	gsub(/\\\\/, "\\", t)
	return t
}