-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathgenerate-bookmarks-html
executable file
·228 lines (202 loc) · 6.42 KB
/
generate-bookmarks-html
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
#!/usr/bin/env bash
# Generate the bookmarks.html file based on the JavaScript files.
# Escape special HTML chars.
escape_html () {
regex='s/&/\&/g; s/"/\"/g; s/</\</g; s/>/\>/g;';
if [ $# -eq 0 ]; then
sed "$regex";
else
sed "$regex" <<< "$@";
fi;
}
# URL-encode the given string.
url_encode () {
if [ $# -eq 0 ]; then
cat;
else
echo "$@";
fi | perl -pe "s/([^A-Za-z0-9\-_~.])/ sprintf '%%%02X', ord \$1 /eg";
}
# Loop through *.js in the given directory and recurse for its subdirectories.
process_dir () {
# Skip certain directories.
if [ "$(basename "$1")" = 'tests' ]; then
return;
fi;
echo "Now processing directory \"$1\"…" 1>&2;
# Start the bookmarks folder.
if [ -f "$1/README" ]; then
title="$(escape_html <<< "$(head -n 1 "$1/README")")";
description="$(escape_html <<< "$(tail -n +3 "$1/README")")";
else
title="$(escape_html "$(basename "$1")")";
description="$(escape_html "${1:2}")";
[ "$title" = "$description" ] && description=;
fi;
if [ "$1" = '.' ]; then
description="$description"$'\n\n'"These bookmarks were exported on $(date +'%a, %Y-%m-%d %T %z').";
fi;
cat <<-EOD
<DT><H3>$title</H3>
<DD><P>$description</P>
<DL><P>
EOD
shopt -s nullglob;
for file in "$1"/*/ "$1"/*.js; do
# Recurse for directories, and indent the output.
if [ -d "$file" ]; then
[ -L "${file%/}" ] || process_dir "${file%/}" | sed $'s/^\([[:space:]]*\)</\\1\t</';
continue;
fi;
original_file="$file";
# Use git log to find the timestamps for the initial and last commits.
unset add_date last_modified;
while read timestamp subject; do
[ -z "$last_modified" -a "${subject:0:1}" != '*' ] && last_modified="$timestamp";
add_date="$timestamp";
done < <(git log --follow --pretty='tformat:%at %s' "$file");
# Get the bookmarklet's metadata.
unset description title keyword transclude;
is_in_docblock=0;
while read -r line; do
if [ "$line" = '/**' ]; then
is_in_docblock=1;
continue;
elif [ "$line" = '*/' ]; then
is_in_docblock=0;
elif [[ "$line" =~ ^\(function\ (.+)\( ]]; then
[ -z "$keyword" ] && keyword="${BASH_REMATCH[1]}";
break;
fi;
if [[ "$line" =~ ^\*\ @([a-z]*)\ (.*) ]]; then
tag="${BASH_REMATCH[1]}";
content="${BASH_REMATCH[2]}";
case "$tag" in
'title')
title="$content";;
'keyword')
keyword="$content";;
'transclude')
transclude="$content";;
esac;
continue;
fi;
if [ $is_in_docblock -eq 1 ]; then
description+=("${line:2}");
fi;
done < "$file";
# Avoid duplicate keywords for the bookmarklets in the Bookmarks
# toolbar, which are just symlinks to the "real" bookmarklets.
if [ -L "$file" ]; then
keyword='';
fi;
# Trim blank lines from the start and end of the description.
if [ "${description[0]}" = '' ]; then
unset description[0];
fi;
for ((i = ${#description[@]} - 1; i >= 0; i--)); do
if [ "${description[$i]}" = '' ]; then
unset description[$i];
fi;
done;
description="$(IFS=$'\n'; echo "${description[*]}")";
# Transclude files, similar to `/etc/alternatives` and symlinking
# `/usr/bin/foo` to `/usr/bin/gfoo` to allow running `gfoo` as `foo`.
# (This is not a very advanced system, but Good Enough™.)
if [ -n "$transclude" ]; then
transclude="$(dirname "$file")/$transclude";
if ! [ -e "$transclude" ]; then
echo "WARNING! Cannot transclude “${transclude}” from “${file}”. Skipping." 1>&2;
continue;
fi;
file="$transclude";
fi;
# Output the current bookmarklet.
cat <<-EOD
<DT><A HREF="javascript:$(
# Clean up whitespace.
< "$file" \
perl -p -e 's/\\$//g; s/\n/ /g; s/\t//g' |
perl -p -e 's/\s\s+/ /g; s/^\s+//; s/\s+$//g' |
# Remove "/* */"-style comments.
perl -p -e 's@/\*\*? .*? \*/@@g' |
# Trim now that the comments are gone.
perl -p -e 's/^\s+//; s/\s+$//' |
# URL-encode the special characters, but make sure to
# restore "%s" for the bookmarklet parameter.
url_encode |
sed 's/%25s/%s/g'
)$(escape_html "/* $original_file */")" ADD_DATE="$(escape_html "$add_date")" LAST_MODIFIED="$(escape_html "$last_modified")" SHORTCUTURL="$(escape_html "$keyword")">$(escape_html "$title")</A>
<DD><P>$(escape_html "$description")</P></DD>
EOD
done;
# End the bookmarks folder.
cat <<-EOD
</DL>
EOD
}
# Export our bookmarks.html.
{
# Output the preamble.
cat <<EOD
<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
DO NOT EDIT! -->
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<TITLE>Bookmarks</TITLE>
<STYLE>
html {
max-width: 60em;
margin: auto;
font-family: "Calibri", sans-serif;
}
dd > p {
margin-top: 0;
white-space: pre-wrap;
line-height: 1.5;
}
a[shortcuturl]:not([shortcuturl=""])::after {
content: " (keyword: " attr(shortcuturl) ")";
display: inline-block;
margin-left: 1em;
font-family: "Consolas", monospace;
}
</STYLE>
<H1>Bookmarks Menu</H1>
<DL>
EOD
# Recursively process this directory and its subdirectories.
process_dir .;
# Wrap it up for the common good.
cat <<EOD
</DD>
</DL>
EOD
} > bookmarks.html;
# Create a separate version of the bookmarks HTML file without support for
# parameters (`%s`) for inferior browsers.
#
# Safari can’t handle `%s` in bookmarklets, and completely refuses to execute
# them. My guess it does not see `%s` as a placeholder, but as an invalid
# (non-URL-encoded) part of a URL.
#
# Chrome automatically converts bookmarklets with a `%s` into custom search
# engines, but they are inactive by default. You need to activate them in the
# settings. Even then, they are not as useful as in Firefox: when you use the
# keyword without a parameter (so not `vidspeed 1.33` but just `vidspeed`), it
# refuses to execute, instead of letting the prompt fallback code ask for a
# parameter. Sigh.
#
# Bookmarklets without a `%s` get added as normal bookmarks in Chrome, but you
# cannot execute them using the keyword. So no `read` or `nocookie` or `2comm`,
# for instance. Sigh-the-sequel.
sed 's/%s//g' bookmarks.html > bookmarks-without-parameters.html;
# I keep a symlink to bookmarks.html in my Downloads directory so I can quickly
# import it from Firefox's bookmarks manager. My Downloads directory is set to
# display the most recently modified files first, so if I update the symlink's
# timestamp, it will be at the top of the list.
if [ -h ~/Downloads/_bookmarks.html ]; then
touch -h ~/Downloads/_bookmarks.html;
fi;
exit 0;