comments.dcgi - gophercgis - Collection of gopher CGI/DCGI for geomyidae
(HTM) hg clone https://bitbucket.org/iamleot/gophercgis
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) README
(DIR) LICENSE
---
comments.dcgi
---
1 #!/bin/sh
2
3 . ../common/config.sh
4
5 short_id="$2"
6
7 curl -Lgs -- "https://lobste.rs/s/${short_id}.json" |
8 jq -r '
9 .title + "\u001c" + .url + "\u001c" + ( .score | tostring ) + "\u001c" +
10 ( .created_at[0:19] + "Z" | sub("T"; " ") | sub("Z"; "") | sub(":[0-9]+$"; "") ) + "\u001c" +
11 .submitter_user.username + "\u001c" + .description + "\u001e",
12 ( .comments[] |
13 .commenting_user.username + "\u001c" +
14 ( .indent_level | tostring ) + "\u001c" +
15 ( .comment ) + "\u001c" +
16 ( .url ) + "\u001e" )
17 ' | awk '
18
19 BEGIN {
20 FS = "\034"
21 RS = "\036"
22 }
23
24 #
25 # Try to convert html to text.
26 #
27 # XXX: This is just a big kludge and not all HTML is nicely converted to
28 # XXX: text...
29 #
30 function html2text(s) {
31 gsub(/<\/a>/, "", s)
32 gsub(/<\/code>/, "", s)
33 gsub(/<\/del>/, "~~", s)
34 gsub(/<\/em>/, "", s)
35 gsub(/<\/li>/, "", s)
36 gsub(/<\/ol>/, "", s)
37 gsub(/<\/strong>/, "", s)
38 gsub(/<\/ul>/, "", s)
39 gsub(/<blockquote>(<p>)?/, "\n«", s)
40 gsub(/(<\/p>)?<\/blockquote>/, "»\n", s)
41 gsub(/<\/p>/, "\n", s)
42 gsub(/<br>/, "\n", s)
43 gsub(/<hr>/, "", s)
44 gsub(/<p>/, "\n", s)
45 gsub(/<ol>/, "", s)
46 gsub(/<ul>/, "", s)
47 gsub(/<li>/, "\n - ", s)
48 gsub(/<a href="[^"]+" rel="nofollow">/, "", s)
49 gsub(/<a href="[^"]+">/, "", s)
50 gsub(/<code>/, "", s)
51 gsub(/<del>/, "~~", s)
52 gsub(/<em>/, "", s)
53 gsub(/<strong>/, "", s)
54 gsub(/&/, "\\&", s)
55 gsub(/</, "<", s)
56 gsub(/>/, ">", s)
57 return s
58 }
59
60 function print_title(title, url, score, created_at, username, description) {
61 fmt_cmd = "par -d 0 -B=. -w 80"
62
63 gsub("\\|", "\\|", title)
64
65 print ""
66 print "Lobsters - Comments"
67 print ""
68 print "[h|" title "|" "URL:" url "|server|port]"
69 if (description) {
70 print description | fmt_cmd
71 close(fmt_cmd)
72 }
73 print "via " username " " created_at " " "(" score ")"
74 print ""
75 }
76
77 function print_comment(comment, i) {
78 fmt_cmd = "par -d 0 -B=. -w 80"
79 printf("[h|%s|URL:%s|server|port]\n",
80 comment[i, "username"] " writes:",
81 comment[i, "url"])
82 print comment[i, "comment"] | fmt_cmd
83 close(fmt_cmd)
84 print ""
85 }
86
87 {
88 # get rid of leading and trailing newlines
89 gsub(/^\n/, "")
90 gsub(/\n$/, "")
91
92 # XXX: get rid of all newlines otherwise the output is messed up
93 gsub(/\n/, "")
94 }
95
96 # Title, etc. TSV
97 NR == 1 {
98 title = $1
99 url = $2
100 score = $3
101 created_at = $4
102 username = $5
103 description = html2text($6)
104
105 print_title(title, url, score, created_at, username, description)
106 next
107 }
108
109 NR > 1 && NF == 4 {
110 n++
111 comment[n, "username"] = $1
112 comment[n, "indent_level"] = $2
113 comment[n, "comment"] = html2text($3)
114 comment[n, "url"] = $4
115 next
116 }
117
118 END {
119 for (i = 1; i <= n; i++) {
120 print_comment(comment, i)
121 }
122 }
123 ' | sed -e '/^\[[0137Ighi]\|/n' -e 's/^/t/'