diff options
Diffstat (limited to 'sfeed_html.sh')
-rwxr-xr-x | sfeed_html.sh | 148 |
1 files changed, 148 insertions, 0 deletions
diff --git a/sfeed_html.sh b/sfeed_html.sh new file mode 100755 index 0000000..58fce30 --- /dev/null +++ b/sfeed_html.sh | |||
@@ -0,0 +1,148 @@ | |||
1 | #!/usr/bin/env bash | ||
2 | |||
3 | echo() { printf '%s\n' "$*"; } | ||
4 | |||
5 | html() { | ||
6 | : "${LIMIT:=1}" | ||
7 | aside="$(mktemp /tmp/sfeed_html_aside.XXXXXX)" | ||
8 | cat <<EOF | ||
9 | $(html_head) | ||
10 | <body> | ||
11 | <header> | ||
12 | <h1> | ||
13 | <a href="index.html"><img src="mars-eyes.png" | ||
14 | title="$(fortune)" | ||
15 | width="40" height="39" | ||
16 | alt="mars, but with eyes" /></a> | ||
17 | Planet ACDW</h1> | ||
18 | <p class="last-updated">last updated at <time>$(date -R)</time></p> | ||
19 | </header> | ||
20 | <nav> | ||
21 | <a href="feeds.html">all feeds</a> | ||
22 | // | ||
23 | <a href="feeds.xml">rss (full)</a> | ||
24 | // | ||
25 | <a href="feeds-short.xml">rss (short)</a> | ||
26 | // | ||
27 | <a href="feeds.opml">opml</a> | ||
28 | </nav> | ||
29 | <main> | ||
30 | $(html_main "$@") | ||
31 | <aside><ul>$(cat "$aside")</ul></aside> | ||
32 | </main> | ||
33 | </body> | ||
34 | </html> | ||
35 | EOF | ||
36 | rm "$aside" | ||
37 | } | ||
38 | |||
39 | html_head() { | ||
40 | cat <<EOF | ||
41 | <!DOCTYPE html> | ||
42 | <html> | ||
43 | <head> | ||
44 | <meta charset="utf-8"> | ||
45 | <meta http-equiv="X-UA-Compatible" content="IE=edge"> | ||
46 | <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"> | ||
47 | <meta name="viewport" content="width=device-width, initial-scale=1"> | ||
48 | <title>Planet ACDW</title> | ||
49 | <link rel="stylesheet" type="text/css" href="style.css"> | ||
50 | <link rel="shortcut icon" type="image/png" href="mars-eyes.png"> | ||
51 | <link rel="alternate" type="application/atom+xml" title="rss (full)" href="feeds.xml"> | ||
52 | <link rel="alternate" type="application/atom+xml" title="rss (short)" href="feeds-short.xml"> | ||
53 | <link rel="alternate" type="application/xml" title="opml" href="feeds.opml"> | ||
54 | </head> | ||
55 | EOF | ||
56 | } | ||
57 | |||
58 | html_main() { | ||
59 | cat <<EOF | ||
60 | <section id="list"> | ||
61 | $(for file in "$@"; do html_feed "$file"; done) | ||
62 | </section> | ||
63 | EOF | ||
64 | } | ||
65 | |||
66 | html_feed() { # html_feed FEED(file) => HTML | ||
67 | filename="$(basename "$1")" | ||
68 | now="$(date +%s)" | ||
69 | fresh_days=7 | ||
70 | fresh_secs="$((fresh_days * 24 * 60 * 60))" | ||
71 | |||
72 | ## ENTRIES | ||
73 | entries="$(awk -v NOW="$now" -v FRESH_SECS="$fresh_secs" \ | ||
74 | -v NAME="$filename" -v ASIDE="$aside" -v limit="$LIMIT" \ | ||
75 | 'BEGIN { FS="\t"; fresh_feed = 0; FRESH = (NOW - FRESH_SECS); } | ||
76 | function unescape(t) { | ||
77 | t = html_escape(t); | ||
78 | gsub(/\\\t/,"\t",t); | ||
79 | gsub(/\\\n/,"\n",t); | ||
80 | gsub(/\\\\/,"\\",t); | ||
81 | return t | ||
82 | } | ||
83 | function html_escape(t) { | ||
84 | gsub(/</,"\\<",t); | ||
85 | gsub(/>/,"\\>",t); | ||
86 | gsub(/&/,"\\&",t); | ||
87 | return t | ||
88 | } | ||
89 | { | ||
90 | timestamp=$1; | ||
91 | title=html_escape($2); | ||
92 | link=$3; | ||
93 | content=unescape($4); | ||
94 | content_type=$5; | ||
95 | id=$6; | ||
96 | author=$7; | ||
97 | enclosure=$8; | ||
98 | category=$9; | ||
99 | |||
100 | if (limit && (timestamp < (NOW - (FRESH_SECS * 3)))) next; | ||
101 | show_in_sidebar = 1; | ||
102 | #print timestamp, title, link > "/dev/stderr"; | ||
103 | |||
104 | date_cmd = "date -d \"@" timestamp "\" +\"%F %R\"" | ||
105 | if (timestamp) { | ||
106 | date_cmd | getline ts; | ||
107 | close(date_cmd); | ||
108 | } | ||
109 | |||
110 | fresh = (timestamp >= FRESH) | ||
111 | if (fresh) fresh_feed = 1; | ||
112 | |||
113 | print "<tr class=\"entry " (fresh ? "fresh" : "") "\">" | ||
114 | print "<td class=\"entry-timestamp\">" ts "</td>" | ||
115 | printf "%s", "<td class=\"entry-extra\">" | ||
116 | if (enclosure) { | ||
117 | stamp = "@" | ||
118 | printf "%s", "<a href=\"" enclosure "\" target=\"_blank\">" stamp "</a>" | ||
119 | } | ||
120 | if ((link != id) && (id != enclosure) && (id ~ /^https?:/)) { | ||
121 | stamp = "#" | ||
122 | printf "%s", "<a href=\"" id "\" target=\"_blank\">" stamp "</a>" | ||
123 | } | ||
124 | print "</td>" | ||
125 | print "<td class=\"entry-title\"><a href=\"" link "\" target=\"_blank\">" title "</a></td>" | ||
126 | print "</tr>" | ||
127 | } | ||
128 | END { | ||
129 | if (show_in_sidebar) { | ||
130 | printf "<li%s>", (fresh_feed?" class=\"fresh\"":"") >> ASIDE | ||
131 | printf "<a href=\"#%s\">%s</a></li>\n", NAME, NAME >> ASIDE | ||
132 | } | ||
133 | printf "%s", (stamp ? stamp : ".") > "/dev/stderr" | ||
134 | }' "$1")" | ||
135 | if [ -z "$entries" ]; then return 1; fi | ||
136 | echo "<section id=\"$filename\">" | ||
137 | # TODO: Include a link back to the website | ||
138 | printf '<header><h2><a href="#%s">#</a> %s</h2>\n' "$filename" "$filename" | ||
139 | printf '<a class="top" href="#">%s</a></header>' "[back to top]" | ||
140 | echo "<table class=\"entries\">" | ||
141 | echo "$entries" | ||
142 | echo "</table>" | ||
143 | echo "</section>" | ||
144 | } | ||
145 | |||
146 | if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then | ||
147 | html "$@" | ||
148 | fi | ||