-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxkcd2js.sh
109 lines (102 loc) · 3.29 KB
/
xkcd2js.sh
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
# $MirOS: src/share/misc/licence.template,v 1.28 2008/11/14 15:33:44 tg Rel $
#-
# Copyright © 2014, 2020
# mirabilos <m@mirbsd.org>
#
# Provided that these terms and disclaimer and all copyright notices
# are retained or reproduced in an accompanying document, permission
# is granted to deal in this work without restriction, including un‐
# limited rights to use, publicly perform, distribute, sell, modify,
# merge, give away, or sublicence.
#
# This work is provided “AS IS” and WITHOUT WARRANTY of any kind, to
# the utmost extent permitted by applicable law, neither express nor
# implied; without malicious intent or gross negligence. In no event
# may a licensor, author or contributor be held liable for indirect,
# direct, other damage, loss, or other issues arising in any way out
# of dealing in the work, even if advised of the possibility of such
# damage or existence of a defect, except proven that it results out
# of said person’s immediate fault when using the work as intended.
#-
# Helper script to download and convert data
unset LANGUAGE; export LC_ALL=C
# escape XHTML characters (three mandatory XML ones plus double quotes,
# the latter in an XML safe fashion numerically though)
function xhtml_escape {
if (( $# )); then
print -nr -- "$@"
else
cat
fi | sed \
-e 's&\&g' \
-e 's<\<g' \
-e 's>\>g' \
-e 's"\"g'
}
# escape string into JSON string (with surrounding quotes)
function json_escape {
[[ -o utf8-mode ]]; local u=$?
set -U
local o=\" s
if (( $# )); then
read -raN-1 s <<<"$*"
unset s[${#s[*]}-1]
else
read -raN-1 s
fi
local -i i=0 n=${#s[*]} wc
local -Uui16 -Z7 x
local -i1 ch
while (( i < n )); do
(( ch = x = wc = s[i++] ))
case $wc {
(8) o+=\\b ;;
(9) o+=\\t ;;
(10) o+=\\n ;;
(12) o+=\\f ;;
(13) o+=\\r ;;
(34) o+=\\\" ;;
(92) o+=\\\\ ;;
(*)
if (( wc < 0x20 || wc > 0xFFFD || \
(wc >= 0xD800 && wc <= 0xDFFF) || \
(wc > 0x7E && wc < 0xA0) )); then
o+=\\u${x#16#}
else
o+=${ch#1#}
fi
;;
}
done
(( u )) && set +U
print -nr -- "$o\""
}
exec >tpl/2-gh-graticules.js
cat <<\EOF
/* BEGIN generated file from xkcd2js.sh {{{ */
/*
* we always apply the 30W rule, so the code would need to be
* changed, both here and in the CGI, to go further west… but
* that would not scale either, anyway, plus we calculate all
* points using CET/CEST, so we don’t do that
*/
var graticules = [
EOF
set -A fetch -- ftp -H "User-Agent: MirKarte/0.2 (Beta; +https://evolvis.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=useful-scripts/mirkarte.git using MirBSD ftp)" -o -
whence -p wget >/dev/null 2>&1 && \
set -A fetch -- wget -e robots=off -U "MirKarte/0.2 (Beta; +https://evolvis.org/plugins/scmgit/cgi-bin/gitweb.cgi?p=useful-scripts/mirkarte.git using GNU wget)" -qO- -T3
for lat in 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56; do
for lon in -6 -5 -4 -3 -2 -1 -0 0 1 2 3 4 5 6 7 8 9 10 11 12; do
x=$("${fetch[@]}" http://carabiner.peeron.com/xkcd/map/data/loc/$lat,$lon)
x=$(xhtml_escape "$x")
[[ $x = *'!/'* ]] && x="<a href=\"https://geohashing.site/${x##*'!/'}\">${x%'!/'*}</a>"
print " [\"$lat\", \"$lon\", $(json_escape "$x")],"
done
done
cat <<\EOF
["666", "666", ""]
];
/* END generated file from xkcd2js.sh }}} */
EOF
exec >&2
ls -la tpl/2-gh-graticules.js >&2