-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathdevdocs-local
More file actions
executable file
·118 lines (98 loc) · 3.1 KB
/
devdocs-local
File metadata and controls
executable file
·118 lines (98 loc) · 3.1 KB
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
#!/usr/bin/env sh
# Select docs from devdocs.io using a fuzzy-finder for display at the CLI
#
# Deps: tidy, xmlstarlet, jq, lynx, fzy, curl, POSIX
# (This uses a few shell scripts from $HOME/bin. Look there for reference.)
#
# Usage:
#
# devdocs-local download
# devdocs-local
NAME=$(basename "$0")
TEMP="${TMPDIR-/tmp}/${NAME}.${$}.$(awk \
'BEGIN {srand(); printf "%d\n", rand() * 10^10}')"
DEVDOCS_DIR="${DEVDOCS_DIR-"${HOME}/tmp/docs/devdocs.io"}"
mkdir -p "$DEVDOCS_DIR"
list () {
# Find the docs JavaScript and try to extract the slug and mtime values.
curl -sS https://devdocs.io |
htmlstar sel -t -v '//_:script[contains(@src, "docs-")]/@src' \
2>/dev/null |
xargs printf 'http:%s\n' |
xargs curl -sS |
awk '
/^slug: / {
match($2, /".*"/)
slug = substr($2, RSTART + 1, RLENGTH - 2)
}
/^mtime: / {
mtime = substr($2, 1, length($2) - 1)
print slug, mtime
}
'
}
download () {
# Download the docsets to a deterministic location.
# TODO: add multiple selection
list |
fzy -p 'Choose a docset > ' | {
while read -r slug mtime; do
test -n "$slug" || return
ddir="${DEVDOCS_DIR}/${slug}"
mkdir -p "$ddir"
cd "$ddir"
if [ -r "${ddir}/mtime" ]; then
old_mtime=$(< "${ddir}/mtime")
if [ "$old_mtime" -ge "$mtime" ]; then
printf '%s already installed.\n' "$slug" 1>&2
return
fi
fi
printf '%s\n' "$mtime" > mtime
curl -sS -o index.json "https://devdocs.io/docs/${slug}/index.json?${mtime}"
curl -sS -o db.json "https://docs.devdocs.io/${slug}/db.json?${mtime}"
done;
}
}
doc () {
find "$DEVDOCS_DIR" -mindepth 1 -type d -printf '%f\n' |
fzy -p 'Choose docs > ' | {
read -r slug
test -n "$slug" || return
jq -r '.entries[] | "\(.name) --- \(.path) --- (\(.type))"' \
"${DEVDOCS_DIR}/${slug}/index.json" |
fzy -p 'Choose a topic > ' |
awk -F " --- " '{
h = index($2, "#")
if (h == 0) {
print $2
} else {
print substr($2, 1, h - 1), substr($2, h)
}
}' | {
read -r dpath frag
test -n "$dpath" || return
jq -r ".\"${dpath}\"" \
< "${DEVDOCS_DIR}/${slug}/db.json" \
> "${TEMP}/index.html"
lynx -assume_charset=utf-8 \
"file://localhost/${TEMP}/index.html${frag}"
};
}
}
main () {
trap '
excode=$?; trap - EXIT;
rm -rf '"$TEMP"'
exit $excode
' INT TERM EXIT
mkdir -p -m 700 "$TEMP"
if [ $# -eq 0 ]; then
doc
else
cmd=$1
shift
"$cmd" "$@"
fi
}
main "$@"