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

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

html() {
	: "${LIMIT:=1}"
	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)"
   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="feeds.html">all feeds</a>
//
<a href="feeds.xml">rss (full)</a>
//
<a href="feeds-short.xml">rss (short)</a>
//
<a href="feeds.opml">opml</a>
</nav>
<main>
$(html_main "$@")
<aside><ul>$(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>.
</footer>
</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">
</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
	host="$(cat /tmp/invidious.host)"
	sed "s@https://www\.youtube\.com@${host}@"
}

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

	## ENTRIES
	entries="$(awk -v NOW="$now" -v FRESH_SECS="$fresh_secs" \
		-v NAME="$filename" -v ASIDE="$aside" -v limit="$LIMIT" \
		'BEGIN { FS="\t"; fresh_feed = 0; FRESH = (NOW - FRESH_SECS); }
		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
		}
		{
			timestamp=$1;
			title=$2;
			link=$3;
			content=unescape($4);
			content_type=$5;
			id=$6;
			author=$7;
			enclosure=$8;
			category=$9;

			if (limit && (timestamp < (NOW - (FRESH_SECS * 21)))) 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" : "") "\">"
			print "<td class=\"entry-timestamp\">" ts "</td>"
			printf "%s", "<td class=\"entry-extra\">"
			if (enclosure) {
				stamp = "@"
				printf "%s", "<a href=\"" enclosure "\" target=\"_blank\">" stamp "</a>"
			}
			if ((link != id) && (id != enclosure) && (id ~ /^https?:/)) {
				stamp = "#"
				printf "%s", "<a href=\"" id "\" target=\"_blank\">" stamp "</a>"
			}
			print "</td>"
			printf "<td class=\"entry-title%s", (link ~ /youtube/ ? " youtube" : "")
			print "\"><a href=\"" link "\" target=\"_blank\">" title "</a></td>"
			print "</tr>"
		}
		END {
			if (show_in_sidebar) {
				printf "<li%s>", (fresh_feed?" class=\"fresh\"":"") >> ASIDE
				printf "<a href=\"#%s\">%s</a></li>\n", NAME, NAME >> ASIDE
			}
			printf "%s", (stamp ? stamp : ".") > "/dev/stderr"
		}' "$1" | filter_invidious)"
	if [ -z "$entries" ]; then return 1; fi
	echo "<section id=\"$filename\">"
	# TODO: Include a link back to the website
	printf '<header><h2><a href="#%s">#</a> %s</h2>\n' "$filename" "$filename"
	printf '<a class="top" href="#">%s</a></header>' "[back to top]"
	echo "<table class=\"entries\">"
	echo "$entries"
	echo "</table>"
	echo "</section>"
}

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