-
Notifications
You must be signed in to change notification settings - Fork 7
/
za-readurl-preinit-handler
executable file
·179 lines (178 loc) · 6.91 KB
/
za-readurl-preinit-handler
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
#!/usr/bin/env zsh
#
# -*- mode: sh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*-
# vim:ft=zsh:sw=2:sts=2:et:foldmarker=[[[,]]]:foldmethod=marker
#
# Copyright (c) 2016-2021 Sebastian Gniazdowski and contributors
# Copyright (c) 2021-2022 zdharma-continuum and contributors
#
# FUNCTION: za-readurl-preinit-handler [[[
#
za-readurl-preinit-handler() {
builtin emulate -LR zsh
builtin setopt extendedglob warncreateglobal typesetsilent noshortloops rcquotes
if [[ $ICE[as] != (readurl*|*readurl) && $ICE[.readurl] != (readurl*|*readurl) ]]; then
return 0
fi
[[ $1 == plugin ]] && {
+zinit-message "{pre}readurl annex: {error}ERROR: as'$ICE[as]'" "ice can be used only with snippets.{rst}"
return 7
}
local __type=$1 __url=$2 __id_as=$3 __dir=$4 __hook=$5 __subtype=$6
ICE[.readurl]=${ICE[.readurl]:-$ICE[as]}
if [[ $ICE[as] = ((readurl\||)(command|program)|(command|program)(\|readurl|)) ]]; then
ICE[as]=command
elif [[ $ICE[as] = ((readurl\||)completion|completion(\|readurl|)) ]]; then
ICE[as]=completion
elif [[ $ICE[as] = ((readurl\||)null|null(\|readurl|)) ]]; then
ICE[as]=null
ICE[pick]=${ICE[pick]:-/dev/null}
else
if [[ $ICE[as] != readurl ]]; then
+zinit-message "{pre}readurl annex: {msg2}" "Warning: Unrecognized as'readurl|{data2}...{msg2}' variant ({data2}$ICE[as]{msg2})." "Falling back to {meta}as{hi}'{data}readurl{hi}'{msg2}.{rst}"
fi
unset 'ICE[as]'
fi
if [[ -d $__dir && $__subtype != update(|:*) ]]; then
return 0
fi
local -a match mbegin mend
local MATCH
integer MBEGIN MEND
(( ${+functions[.zinit-setup-plugin-dir]} != 1 )) || builtin source $ZINIT[BIN_DIR]/zinit-install.zsh
match=()
local dlpage=${__url%(#b)([^+])++*}
dlpage=$dlpage$match[1]
if [[ $ICE[dl] == (*\;)(#c0,1)ink(0)(#c0,1)[=:](#c0,1)[-_/[:alnum:]]* ]]; then
ICE[dlink]=${(j.;.)${${(@M)${(@s.;.)ICE[dl]}:#ink([=:]|)*}##ink([=:]|)}}
ICE[dl]=${(j.;.)${(@s.;.)ICE[dl]}:#ink([=:]|)*}
[[ -z $ICE[dlink] ]] && unset 'ICE[dlink]'
[[ -z $ICE[dl] ]] && unset 'ICE[dl]'
fi
if [[ -z $ICE[dlink] ]]; then
local plus=${(MS)__url%%[^+]++##} pattern_url=$dlpage
plus=${plus#?++}
while [[ -n $plus ]]; do
pattern_url=${pattern_url:h}
plus=${plus%+}
done
local pattern_url=${pattern_url}/${__url##*++}
else
local -a urls sorts filters
urls=(${(s.;.)ICE[dlink]})
if ((${#urls} == 2)); then
local pattern_url=${urls[@]:#0*} pattern_url0=${${(M)urls[@]:#0*}##0(:|=)(#c0,1)}
if [[ $pattern_url == \!* ]]; then
sorts[2]=1
pattern_url=${pattern_url#\!}
fi
if [[ $pattern_url == *[^\\]([\\][\\])#\~%[^%]##%* ]]; then
if [[ $pattern_url == (#b)(*)\~%([^%]##)%(*) ]]; then
pattern_url=$match[1]$match[3]
filters[2]=$match[2]
fi
fi
if [[ $pattern_url0 == \!* ]]; then
sorts[1]=1
pattern_url0=${pattern_url0#\!}
fi
if [[ $pattern_url0 == *[^\\]([\\][\\])#\~%[^%]##%* ]]; then
if [[ $pattern_url0 == (#b)(*)\~%([^%]##)%(*) ]]; then
pattern_url0=$match[1]$match[3]
filters[1]=$match[2]
fi
fi
elif ((${#urls} == 1)); then
local pattern_url=${urls[@]:#0*} pattern_url0=${${(M)urls[@]:#0*}##0(:|=)(#c0,1)}
if [[ -n $pattern_url0 ]]; then
+zinit-message "{pre}readurl annex: {error}ERROR:{rst}" "The {meta}dlink0''{rst} ice cannot be used alone," "{meta}dlink''{rst} is always required."
return 9
fi
if [[ $pattern_url == \!* ]]; then
sorts[1]=1
pattern_url=${pattern_url#\!}
fi
if [[ $pattern_url == *[^\\]([\\][\\])#\~%[^%]##%* ]]; then
if [[ $pattern_url == (#b)(*)\~%([^%]##)%(*) ]]; then
pattern_url=$match[1]$match[3]
filters[1]=$match[2]
fi
fi
elif ((${#urls} > 2)); then
+zinit-message "{pre}readurl annex: {error}ERROR: {msg2}The ice {meta}dlink''{msg2}" "has been used too many times: {meta}#{obj}${#urls}{msg2}, while it can be used at most" "two times, as {meta2}dlink0''{msg2} and then {meta2}dlink''{msg2}, aborting.{rst}"
return 9
else
local pattern_url=$ICE[dlink]
fi
fi
local -A map
map=(".*" "*" ".+" "?##" "+" "##" "*" "#")
local -a filters
filters=("${filters[@]//(#b)(${(~kj.|.)map})/$map[$match[1]]}")
local tmpfile="$(mktemp)"
pattern_url=${pattern_url//\%VERSION\%/[.,a-zA-Z0-9_-]+}
local pattern_url0=${pattern_url0//\%VERSION\%/[.,a-zA-Z0-9_-]+}
integer index
local cur_paturl
for cur_paturl in $pattern_url0 $pattern_url; do
index+=1
.zinit-download-file-stdout $dlpage >| $tmpfile || {
.zinit-download-file-stdout $dlpage 1 >| $tmpfile || {
+zinit-message "{pre}readurl annex: {error}ERROR: couldn't" "fetch the download page ({url}${dlpage//\%/%%}{error}){rst}"
return 9
}
}
local -a list
list=(${(@f)"$(noglob command grep -E -io "href=.?$cur_paturl" $tmpfile)"})
if ((sorts[index])); then
list=(${list[@]:#[^[:digit:]]##})
list=(${(On)list[@]})
fi
if [[ -n $filters[index] ]]; then
list=(${list[@]:#href=(?|)$~filters[index]})
fi
local selected=${list[1]#href=}
selected=${selected#[\"\']}
if [[ -z $selected ]]; then
+zinit-message "{pre}readurl annex: {error}ERROR:{rst}" "couldn't match the URL${${(M)cur_paturl:#$pattern_url0}:+-0} at the download page" "(which is {url}${dlpage//\%/%%}{rst}${${__id_as:#$__url}:+; the snippet is being identified as" "{meta}${__id_as}{rst}}; was matching {data2}${cur_paturl}{rst})."
return 9
fi
if [[ $selected == /* ]]; then
local domain protocol
if [[ $dlpage = (#b)(#i)((http(s|)|ftp(s|)|ssh|scp)://|)([^/]##)(*) ]]; then
protocol=$match[1] domain=$match[5]
fi
if [[ -z $domain ]]; then
+zinit-message "{pre}readurl annex: {ehi}ERROR:{error} couldn't establish the domain name (unsupported" "protocol? supported are: {data}http(s){error},{data}ftp(s){error},{data}ssh{error},{data}scp{error}).{rst}"
return 9
fi
local new_url=${protocol:-http://}$domain$selected
elif [[ $selected = (#i)(http(s|)|ftp(s|)|ssh|scp)://* ]]; then
local new_url=$selected
else
local -a exts
exts=(xhtml htmls html htm php php3 php4 phtml pl asp aspx ece js jsp jspx jhtml cfm py rb rhtml shtml cgi)
if [[ $dlpage = *.(${(~j:|:)exts}) ]]; then
local new_url=${dlpage:h}/$selected
else
local new_url=$dlpage/$selected
fi
fi
dlpage=$new_url
done
if [[ $__subtype == update(|:*) && -z $opts[(r)-f] ]]; then
{
local old_url="$(< $__dir/._zinit/url_rsvd)"
} 2> /dev/null
[[ $old_url == $new_url ]] && return 8
fi
url=$new_url
ICE[url_rsvd]=$new_url
ZINIT[annex-multi-flag:pull-active]=2
if [[ $__subtype == update(|:*) ]]; then
typeset -gA OPTS
OPTS[opt_-q,--quiet]=0
fi
+zinit-message "{pre}readurl annex: {msg}Matched the following URL: {url}${new_url}{rst}"
return 0
} # ]]]