searchlink 2.3.68 → 2.3.70
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/searchlink/exceptions.rb +29 -0
- data/lib/searchlink/number.rb +13 -0
- data/lib/searchlink/output.rb +7 -4
- data/lib/searchlink/parse.rb +221 -197
- data/lib/searchlink/script_plugin.rb +84 -0
- data/lib/searchlink/searches/duckduckgo.rb +3 -3
- data/lib/searchlink/searches/google.rb +2 -2
- data/lib/searchlink/searches/helpers/chromium.rb +207 -50
- data/lib/searchlink/searches/helpers/safari.rb +1 -0
- data/lib/searchlink/searches.rb +32 -7
- data/lib/searchlink/semver.rb +7 -7
- data/lib/searchlink/string.rb +8 -0
- data/lib/searchlink/version.rb +1 -1
- data/lib/searchlink.rb +10 -0
- metadata +5 -2
data/lib/searchlink/parse.rb
CHANGED
@@ -20,14 +20,14 @@ module SL
|
|
20
20
|
|
21
21
|
input.parse_flags! unless skip_flags
|
22
22
|
|
23
|
-
options = %w[debug country_code inline prefix_random include_titles remove_seo validate_links]
|
23
|
+
options = %w[debug country_code inline prefix_random include_titles remove_seo validate_links complete_bare]
|
24
24
|
options.each do |o|
|
25
25
|
if input =~ /^ *#{o}:\s+(\S+)$/
|
26
26
|
val = Regexp.last_match(1).strip
|
27
27
|
val = true if val =~ /true/i
|
28
28
|
val = false if val =~ /false/i
|
29
29
|
SL.config[o] = val
|
30
|
-
|
30
|
+
warn "\r\033[0KGlobal config: #{o} = #{SL.config[o]}\n" unless SILENT
|
31
31
|
end
|
32
32
|
|
33
33
|
next if skip_flags
|
@@ -57,7 +57,7 @@ module SL
|
|
57
57
|
case input.strip
|
58
58
|
when /^!?help$/i
|
59
59
|
if SILENT
|
60
|
-
help_dialog
|
60
|
+
help_dialog
|
61
61
|
else
|
62
62
|
$stdout.puts SL.version_check.to_s
|
63
63
|
$stdout.puts 'See https://github.com/ttscoff/searchlink/wiki for help'
|
@@ -75,6 +75,117 @@ module SL
|
|
75
75
|
Process.exit 0
|
76
76
|
end
|
77
77
|
|
78
|
+
def create_footnote(mtch)
|
79
|
+
if mtch[1].nil? || mtch[1] == ''
|
80
|
+
match
|
81
|
+
else
|
82
|
+
note = mtch[1].strip
|
83
|
+
@footnote_counter += 1
|
84
|
+
ref = if !@link_text.empty? && @link_text.scan(/\s/).empty?
|
85
|
+
@link_text
|
86
|
+
else
|
87
|
+
format('%<p>sfn%<c>04d', p: @prefix, c: @footnote_counter)
|
88
|
+
end
|
89
|
+
SL.add_footer "[^#{ref}]: #{note}"
|
90
|
+
res = "[^#{ref}]"
|
91
|
+
@cursor_difference += (SL.match_length - res.length)
|
92
|
+
SL.match_length = res.length
|
93
|
+
SL.add_report("#{@match_string} => Footnote #{ref}")
|
94
|
+
res
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
def add_title(link_info)
|
99
|
+
@url = link_info
|
100
|
+
title = SL::URL.title(@url)
|
101
|
+
@link_text = title
|
102
|
+
|
103
|
+
if @ref_title
|
104
|
+
unless @links.key? @url
|
105
|
+
@links[@url] = @link_text
|
106
|
+
SL.add_footer SL.make_link(:ref_title, @link_text, @url, title: title, force_title: false)
|
107
|
+
end
|
108
|
+
@delete_line = true
|
109
|
+
elsif SL.config['inline']
|
110
|
+
res = SL.make_link(:inline, @link_text, @url, title: title, force_title: false)
|
111
|
+
@cursor_difference += SL.match_length - res.length
|
112
|
+
SL.match_length = res.length
|
113
|
+
SL.add_report("#{@match_string} => #{@url}")
|
114
|
+
res
|
115
|
+
else
|
116
|
+
unless @links.key? @url
|
117
|
+
@highest_marker += 1
|
118
|
+
@links[@url] = format('%<pre>s%<m>04d', pre: @prefix, m: @highest_marker)
|
119
|
+
SL.add_footer SL.make_link(:ref_title, @links[@url], @url, title: title, force_title: false)
|
120
|
+
end
|
121
|
+
|
122
|
+
type = SL.config['inline'] ? :inline : :ref_link
|
123
|
+
res = SL.make_link(type, @link_text, @links[@url], title: false, force_title: false)
|
124
|
+
@cursor_difference += SL.match_length - res.length
|
125
|
+
SL.match_length = res.length
|
126
|
+
SL.add_report("#{@match_string} => #{@url}")
|
127
|
+
res
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def custom_search(search_type, search_terms)
|
132
|
+
SL.config['custom_site_searches'].each do |k, v|
|
133
|
+
next unless search_type == k
|
134
|
+
|
135
|
+
@link_text = search_terms if !SL.titleize && @link_text == ''
|
136
|
+
v = parse_arguments(v, { no_restore: true })
|
137
|
+
if v =~ %r{^(/|http)}i
|
138
|
+
search_type = 'r'
|
139
|
+
tokens = v.scan(/\$term\d+[ds]?/).sort.uniq
|
140
|
+
|
141
|
+
if !tokens.empty?
|
142
|
+
highest_token = 0
|
143
|
+
tokens.each do |token|
|
144
|
+
if token =~ /(\d+)[ds]?$/ && Regexp.last_match(1).to_i > highest_token
|
145
|
+
highest_token = Regexp.last_match(1).to_i
|
146
|
+
end
|
147
|
+
end
|
148
|
+
terms_p = search_terms.split(/ +/)
|
149
|
+
if terms_p.length > highest_token
|
150
|
+
remainder = terms_p[highest_token - 1..-1].join(' ')
|
151
|
+
terms_p = terms_p[0..highest_token - 2]
|
152
|
+
terms_p.push(remainder)
|
153
|
+
end
|
154
|
+
tokens.each do |t|
|
155
|
+
next unless t =~ /(\d+)[ds]?$/
|
156
|
+
|
157
|
+
int = Regexp.last_match(1).to_i - 1
|
158
|
+
replacement = terms_p[int]
|
159
|
+
case t
|
160
|
+
when /d$/
|
161
|
+
replacement.downcase!
|
162
|
+
re_down = ''
|
163
|
+
when /s$/
|
164
|
+
replacement.slugify!
|
165
|
+
re_down = ''
|
166
|
+
else
|
167
|
+
re_down = '(?!d|s)'
|
168
|
+
end
|
169
|
+
v.gsub!(/#{Regexp.escape(t) + re_down}/, replacement.url_encode)
|
170
|
+
end
|
171
|
+
search_terms = v
|
172
|
+
else
|
173
|
+
search_terms = v.gsub(/\$term[ds]?/i) do |mtch|
|
174
|
+
search_terms.downcase! if mtch =~ /d$/i
|
175
|
+
search_terms.slugify! if mtch =~ /s$/i
|
176
|
+
search_terms.url_encode
|
177
|
+
end
|
178
|
+
end
|
179
|
+
else
|
180
|
+
search_type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
181
|
+
search_terms = "site:#{v} #{search_terms}"
|
182
|
+
end
|
183
|
+
|
184
|
+
break
|
185
|
+
end
|
186
|
+
[search_type, search_terms]
|
187
|
+
end
|
188
|
+
|
78
189
|
def parse(input)
|
79
190
|
SL.output = []
|
80
191
|
return false if input.empty?
|
@@ -94,7 +205,7 @@ module SL
|
|
94
205
|
SL.add_output("<!-- v#{latest_version} available, run SearchLink on the word 'update' to install. -->")
|
95
206
|
end
|
96
207
|
|
97
|
-
links = {}
|
208
|
+
@links = {}
|
98
209
|
SL.footer = []
|
99
210
|
counter_links = 0
|
100
211
|
counter_errors = 0
|
@@ -102,9 +213,9 @@ module SL
|
|
102
213
|
input.sub!(/\n?<!-- Report:.*?-->\n?/m, '')
|
103
214
|
input.sub!(/\n?<!-- Errors:.*?-->\n?/m, '')
|
104
215
|
|
105
|
-
input.scan(/\[(.*?)\]:\s+(.*?)\n/).each { |match| links[match[1].strip] = match[0] }
|
216
|
+
input.scan(/\[(.*?)\]:\s+(.*?)\n/).each { |match| @links[match[1].strip] = match[0] }
|
106
217
|
|
107
|
-
prefix = if SL.config['prefix_random']
|
218
|
+
@prefix = if SL.config['prefix_random']
|
108
219
|
if input =~ /\[(\d{4}-)\d+\]: \S+/
|
109
220
|
Regexp.last_match(1)
|
110
221
|
else
|
@@ -114,16 +225,27 @@ module SL
|
|
114
225
|
''
|
115
226
|
end
|
116
227
|
|
117
|
-
highest_marker = 0
|
118
|
-
input.scan(/^\s{,3}\[(?:#{prefix})?(\d+)\]: /).each do
|
228
|
+
@highest_marker = 0
|
229
|
+
input.scan(/^\s{,3}\[(?:#{@prefix})?(\d+)\]: /).each do
|
119
230
|
m = Regexp.last_match
|
120
|
-
highest_marker = m[1].to_i if m[1].to_i > highest_marker
|
231
|
+
@highest_marker = m[1].to_i if m[1].to_i > @highest_marker
|
121
232
|
end
|
122
233
|
|
123
|
-
footnote_counter = 0
|
124
|
-
input.scan(/^\s{,3}\[\^(?:#{prefix})?fn(\d+)\]: /).each do
|
234
|
+
@footnote_counter = 0
|
235
|
+
input.scan(/^\s{,3}\[\^(?:#{@prefix})?fn(\d+)\]: /).each do
|
125
236
|
m = Regexp.last_match
|
126
|
-
footnote_counter = m[1].to_i if m[1].to_i > footnote_counter
|
237
|
+
@footnote_counter = m[1].to_i if m[1].to_i > @footnote_counter
|
238
|
+
end
|
239
|
+
|
240
|
+
if SL.config['complete_bare']
|
241
|
+
rx = %r{(?ix-m)(?<!\(|:\s|<)(?:
|
242
|
+
(?:https?://)(?:[\da-z.-]+)\.(?:[a-z.]{2,6})
|
243
|
+
(?:[/\w\d.\-()_/+=?&%]*?(?=[\s\n]|$))
|
244
|
+
)}
|
245
|
+
input.gsub!(rx) do
|
246
|
+
url_match = Regexp.last_match
|
247
|
+
url_match.pre_match =~ /!\S+ +$/ ? url_match[0] : "[%](#{url_match[0]})"
|
248
|
+
end
|
127
249
|
end
|
128
250
|
|
129
251
|
if input =~ /\[(.*?)\]\((.*?)\)/
|
@@ -135,7 +257,7 @@ module SL
|
|
135
257
|
line_difference = 0
|
136
258
|
lines.each_with_index do |line, num|
|
137
259
|
SL.line_num = num - line_difference
|
138
|
-
cursor_difference = 0
|
260
|
+
@cursor_difference = 0
|
139
261
|
# ignore links in code blocks
|
140
262
|
if line =~ /^( {4,}|\t+)[^*+\-]/
|
141
263
|
out.push(line)
|
@@ -155,22 +277,22 @@ module SL
|
|
155
277
|
next
|
156
278
|
end
|
157
279
|
|
158
|
-
delete_line = false
|
280
|
+
@delete_line = false
|
159
281
|
|
160
|
-
search_count = 0
|
282
|
+
@search_count = 0
|
161
283
|
|
162
284
|
line.gsub!(/\[(.*?)\]\((.*?)\)/) do |match|
|
163
285
|
this_match = Regexp.last_match
|
164
|
-
SL.match_column = this_match.begin(0) - cursor_difference
|
165
|
-
match_string = this_match.to_s
|
166
|
-
SL.match_length = match_string.length
|
286
|
+
SL.match_column = this_match.begin(0) - @cursor_difference
|
287
|
+
@match_string = this_match.to_s
|
288
|
+
SL.match_length = @match_string.length
|
167
289
|
match_before = this_match.pre_match
|
168
290
|
|
169
291
|
invalid_search = false
|
170
|
-
ref_title = false
|
292
|
+
@ref_title = false
|
171
293
|
|
172
294
|
if match_before.scan(/(^|[^\\])`/).length.odd?
|
173
|
-
SL.add_report("Match '#{match_string}' within an inline code block")
|
295
|
+
SL.add_report("Match '#{@match_string}' within an inline code block")
|
174
296
|
invalid_search = true
|
175
297
|
end
|
176
298
|
|
@@ -179,14 +301,14 @@ module SL
|
|
179
301
|
$stderr.print("\033[0K\rProcessed: #{counter_links} of #{total_links}, #{counter_errors} errors. ")
|
180
302
|
end
|
181
303
|
|
182
|
-
link_text = this_match[1] || ''
|
304
|
+
@link_text = this_match[1] || ''
|
183
305
|
link_info = parse_arguments(this_match[2].strip).strip || ''
|
184
306
|
|
185
|
-
if link_text.strip == '' && link_info =~ /".*?"/
|
307
|
+
if @link_text.strip == '' && link_info =~ /".*?"/
|
186
308
|
link_info.gsub!(/"(.*?)"/) do
|
187
|
-
m = Regexp.last_match
|
188
|
-
link_text = m if link_text == ''
|
189
|
-
|
309
|
+
m = Regexp.last_match
|
310
|
+
@link_text = m[1] if @link_text == ''
|
311
|
+
m[0]
|
190
312
|
end
|
191
313
|
end
|
192
314
|
|
@@ -195,11 +317,11 @@ module SL
|
|
195
317
|
end
|
196
318
|
|
197
319
|
if link_info.strip =~ /:$/ && line.strip == match
|
198
|
-
ref_title = true
|
320
|
+
@ref_title = true
|
199
321
|
link_info.sub!(/\s*:\s*$/, '')
|
200
322
|
end
|
201
323
|
|
202
|
-
unless
|
324
|
+
unless !@link_text.empty? || !link_info.sub(/^[!\^]\S+/, '').strip.empty?
|
203
325
|
SL.add_error('No input', match)
|
204
326
|
counter_errors += 1
|
205
327
|
invalid_search = true
|
@@ -217,60 +339,15 @@ module SL
|
|
217
339
|
match
|
218
340
|
elsif link_info =~ /^\^(.+)/
|
219
341
|
m = Regexp.last_match
|
220
|
-
|
221
|
-
match
|
222
|
-
else
|
223
|
-
note = m[1].strip
|
224
|
-
footnote_counter += 1
|
225
|
-
ref = if !link_text.empty? && link_text.scan(/\s/).empty?
|
226
|
-
link_text
|
227
|
-
else
|
228
|
-
format('%<p>sfn%<c>04d', p: prefix, c: footnote_counter)
|
229
|
-
end
|
230
|
-
SL.add_footer "[^#{ref}]: #{note}"
|
231
|
-
res = "[^#{ref}]"
|
232
|
-
cursor_difference += (SL.match_length - res.length)
|
233
|
-
SL.match_length = res.length
|
234
|
-
SL.add_report("#{match_string} => Footnote #{ref}")
|
235
|
-
res
|
236
|
-
end
|
342
|
+
create_footnote(m)
|
237
343
|
# Handle [](URL) and [%](URL), filling in title
|
238
|
-
elsif (link_text == '' || link_text == '%') && SL::URL.url?(link_info)
|
239
|
-
|
240
|
-
|
241
|
-
link_text = title
|
242
|
-
|
243
|
-
if ref_title
|
244
|
-
unless links.key? url
|
245
|
-
links[url] = link_text
|
246
|
-
SL.add_footer SL.make_link(:ref_title, link_text, url, title: title, force_title: false)
|
247
|
-
end
|
248
|
-
delete_line = true
|
249
|
-
elsif SL.config['inline']
|
250
|
-
res = SL.make_link(:inline, link_text, url, title: title, force_title: false)
|
251
|
-
cursor_difference += SL.match_length - res.length
|
252
|
-
SL.match_length = res.length
|
253
|
-
SL.add_report("#{match_string} => #{url}")
|
254
|
-
res
|
255
|
-
else
|
256
|
-
unless links.key? url
|
257
|
-
highest_marker += 1
|
258
|
-
links[url] = format('%<pre>s%<m>04d', pre: prefix, m: highest_marker)
|
259
|
-
SL.add_footer SL.make_link(:ref_title, links[url], url, title: title, force_title: false)
|
260
|
-
end
|
261
|
-
|
262
|
-
type = SL.config['inline'] ? :inline : :ref_link
|
263
|
-
res = SL.make_link(type, link_text, links[url], title: false, force_title: false)
|
264
|
-
cursor_difference += SL.match_length - res.length
|
265
|
-
SL.match_length = res.length
|
266
|
-
SL.add_report("#{match_string} => #{url}")
|
267
|
-
res
|
268
|
-
end
|
269
|
-
elsif (link_text == '' && link_info == '') || SL::URL.url?(link_info)
|
344
|
+
elsif (@link_text == '' || @link_text == '%') && SL::URL.url?(link_info)
|
345
|
+
add_title(link_info)
|
346
|
+
elsif (@link_text == '' && link_info == '') || SL::URL.url?(link_info)
|
270
347
|
SL.add_error('Invalid search', match) unless SL::URL.url?(link_info)
|
271
348
|
match
|
272
349
|
else
|
273
|
-
link_info = link_text if
|
350
|
+
link_info = @link_text if !@link_text.empty? && link_info == ''
|
274
351
|
|
275
352
|
search_type = ''
|
276
353
|
search_terms = ''
|
@@ -282,7 +359,7 @@ module SL
|
|
282
359
|
m = Regexp.last_match
|
283
360
|
|
284
361
|
search_type = if m[1].nil?
|
285
|
-
SL::GoogleSearch.
|
362
|
+
SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
286
363
|
else
|
287
364
|
m[1]
|
288
365
|
end
|
@@ -291,15 +368,15 @@ module SL
|
|
291
368
|
search_terms.strip!
|
292
369
|
|
293
370
|
# if the link text is just '%' replace with title regardless of config settings
|
294
|
-
if link_text == '%' && search_terms && !search_terms.empty?
|
371
|
+
if @link_text == '%' && search_terms && !search_terms.empty?
|
295
372
|
SL.titleize = true
|
296
|
-
link_text = ''
|
373
|
+
@link_text = ''
|
297
374
|
end
|
298
375
|
|
299
|
-
search_terms = link_text if search_terms == ''
|
376
|
+
search_terms = @link_text if search_terms == ''
|
300
377
|
|
301
378
|
# if the input starts with a +, append it to the link text as the search terms
|
302
|
-
search_terms = "#{link_text} #{search_terms.strip.sub(/^\+\s*/, '')}" if search_terms.strip =~ /^\+[^+]/
|
379
|
+
search_terms = "#{@link_text} #{search_terms.strip.sub(/^\+\s*/, '')}" if search_terms.strip =~ /^\+[^+]/
|
303
380
|
|
304
381
|
# if the end of input contain "^", copy to clipboard instead of STDOUT
|
305
382
|
SL.clipboard = true if search_terms =~ /(!!)?\^(!!)?$/
|
@@ -314,21 +391,21 @@ module SL
|
|
314
391
|
|
315
392
|
if search_word && SL::Searches.valid_search?(search_word[1])
|
316
393
|
search_type = search_word[1] unless search_word.nil?
|
317
|
-
search_terms = link_text
|
394
|
+
search_terms = @link_text
|
318
395
|
elsif search_word && search_word[1] =~ /^(\S+\.)+\S+$/
|
319
|
-
search_type = SL::GoogleSearch.
|
320
|
-
puts SL::GoogleSearch.
|
321
|
-
search_terms = "site:#{search_word[1]} #{link_text}"
|
396
|
+
search_type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
397
|
+
puts SL::GoogleSearch.api_key?
|
398
|
+
search_terms = "site:#{search_word[1]} #{@link_text}"
|
322
399
|
else
|
323
400
|
SL.add_error("Invalid search#{SL::Searches.did_you_mean(search_word[1])}", match)
|
324
401
|
search_type = false
|
325
402
|
search_terms = false
|
326
403
|
end
|
327
|
-
elsif link_text &&
|
328
|
-
search_type = SL::GoogleSearch.
|
329
|
-
search_terms = link_text
|
404
|
+
elsif @link_text && !@link_text.empty? && (!link_info || link_info.empty?)
|
405
|
+
search_type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
406
|
+
search_terms = @link_text
|
330
407
|
elsif link_info && !link_info.empty?
|
331
|
-
search_type = SL::GoogleSearch.
|
408
|
+
search_type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
332
409
|
search_terms = link_info
|
333
410
|
else
|
334
411
|
SL.add_error('Invalid search', match)
|
@@ -337,109 +414,56 @@ module SL
|
|
337
414
|
end
|
338
415
|
|
339
416
|
if search_type && !search_terms.empty?
|
340
|
-
|
341
|
-
next unless search_type == k
|
342
|
-
|
343
|
-
link_text = search_terms if !SL.titleize && link_text == ''
|
344
|
-
v = parse_arguments(v, { no_restore: true })
|
345
|
-
if v =~ %r{^(/|http)}i
|
346
|
-
search_type = 'r'
|
347
|
-
tokens = v.scan(/\$term\d+[ds]?/).sort.uniq
|
348
|
-
|
349
|
-
if !tokens.empty?
|
350
|
-
highest_token = 0
|
351
|
-
tokens.each do |token|
|
352
|
-
if token =~ /(\d+)[ds]?$/ && Regexp.last_match(1).to_i > highest_token
|
353
|
-
highest_token = Regexp.last_match(1).to_i
|
354
|
-
end
|
355
|
-
end
|
356
|
-
terms_p = search_terms.split(/ +/)
|
357
|
-
if terms_p.length > highest_token
|
358
|
-
remainder = terms_p[highest_token - 1..-1].join(' ')
|
359
|
-
terms_p = terms_p[0..highest_token - 2]
|
360
|
-
terms_p.push(remainder)
|
361
|
-
end
|
362
|
-
tokens.each do |t|
|
363
|
-
next unless t =~ /(\d+)[ds]?$/
|
364
|
-
|
365
|
-
int = Regexp.last_match(1).to_i - 1
|
366
|
-
replacement = terms_p[int]
|
367
|
-
case t
|
368
|
-
when /d$/
|
369
|
-
replacement.downcase!
|
370
|
-
re_down = ''
|
371
|
-
when /s$/
|
372
|
-
replacement.slugify!
|
373
|
-
re_down = ''
|
374
|
-
else
|
375
|
-
re_down = '(?!d|s)'
|
376
|
-
end
|
377
|
-
v.gsub!(/#{Regexp.escape(t) + re_down}/, replacement.url_encode)
|
378
|
-
end
|
379
|
-
search_terms = v
|
380
|
-
else
|
381
|
-
search_terms = v.gsub(/\$term[ds]?/i) do |mtch|
|
382
|
-
search_terms.downcase! if mtch =~ /d$/i
|
383
|
-
search_terms.slugify! if mtch =~ /s$/i
|
384
|
-
search_terms.url_encode
|
385
|
-
end
|
386
|
-
end
|
387
|
-
else
|
388
|
-
search_type = SL::GoogleSearch.test_for_key ? 'gg' : 'g'
|
389
|
-
search_terms = "site:#{v} #{search_terms}"
|
390
|
-
end
|
391
|
-
|
392
|
-
break
|
393
|
-
end
|
417
|
+
search_type, search_terms = custom_search(search_type, search_terms)
|
394
418
|
end
|
395
419
|
|
396
|
-
if (search_type && search_terms) || url
|
420
|
+
if (search_type && search_terms) || @url
|
397
421
|
# warn "Searching #{search_type} for #{search_terms}"
|
398
|
-
unless url
|
399
|
-
search_count += 1
|
400
|
-
url, title, link_text = do_search(search_type, search_terms, link_text, search_count)
|
422
|
+
unless @url
|
423
|
+
@search_count += 1
|
424
|
+
@url, title, @link_text = do_search(search_type, search_terms, @link_text, @search_count)
|
401
425
|
end
|
402
426
|
|
403
|
-
if url
|
404
|
-
title = SL::URL.title(url) if SL.titleize && title == ''
|
427
|
+
if @url
|
428
|
+
title = SL::URL.title(@url) if SL.titleize && title == ''
|
405
429
|
|
406
|
-
link_text = title if link_text == '' && title
|
430
|
+
@link_text = title if @link_text == '' && title
|
407
431
|
force_title = search_type =~ /def/ ? true : false
|
408
432
|
|
409
|
-
if link_only || search_type =~ /sp(ell)?/ || url == 'embed'
|
410
|
-
url = title if url == 'embed'
|
411
|
-
cursor_difference += SL.match_length - url.length
|
412
|
-
SL.match_length = url.length
|
413
|
-
SL.add_report("#{match_string} => #{url}")
|
414
|
-
url
|
415
|
-
elsif ref_title
|
416
|
-
unless links.key? url
|
417
|
-
links[url] = link_text
|
418
|
-
SL.add_footer SL.make_link(:ref_title, link_text, url, title: title, force_title: force_title)
|
433
|
+
if link_only || search_type =~ /sp(ell)?/ || @url == 'embed'
|
434
|
+
@url = title if @url == 'embed'
|
435
|
+
@cursor_difference += SL.match_length - @url.length
|
436
|
+
SL.match_length = @url.length
|
437
|
+
SL.add_report("#{@match_string} => #{@url}")
|
438
|
+
@url
|
439
|
+
elsif @ref_title
|
440
|
+
unless @links.key? @url
|
441
|
+
@links[@url] = @link_text
|
442
|
+
SL.add_footer SL.make_link(:ref_title, @link_text, @url, title: title, force_title: force_title)
|
419
443
|
end
|
420
|
-
delete_line = true
|
444
|
+
@delete_line = true
|
421
445
|
elsif SL.config['inline']
|
422
|
-
res = SL.make_link(:inline, link_text, url, title: title, force_title: force_title)
|
423
|
-
cursor_difference += SL.match_length - res.length
|
446
|
+
res = SL.make_link(:inline, @link_text, @url, title: title, force_title: force_title)
|
447
|
+
@cursor_difference += SL.match_length - res.length
|
424
448
|
SL.match_length = res.length
|
425
|
-
SL.add_report("#{match_string} => #{url}")
|
449
|
+
SL.add_report("#{@match_string} => #{@url}")
|
426
450
|
res
|
427
451
|
else
|
428
|
-
unless links.key? url
|
429
|
-
highest_marker += 1
|
430
|
-
links[url] = format('%<pre>s%<m>04d', pre: prefix, m: highest_marker)
|
431
|
-
SL.add_footer SL.make_link(:ref_title, links[url], url, title: title, force_title: force_title)
|
452
|
+
unless @links.key? @url
|
453
|
+
@highest_marker += 1
|
454
|
+
@links[@url] = format('%<pre>s%<m>04d', pre: @prefix, m: @highest_marker)
|
455
|
+
SL.add_footer SL.make_link(:ref_title, @links[@url], @url, title: title, force_title: force_title)
|
432
456
|
end
|
433
457
|
|
434
458
|
type = SL.config['inline'] ? :inline : :ref_link
|
435
|
-
res = SL.make_link(type, link_text, links[url], title: false, force_title: force_title)
|
436
|
-
cursor_difference += SL.match_length - res.length
|
459
|
+
res = SL.make_link(type, @link_text, @links[@url], title: false, force_title: force_title)
|
460
|
+
@cursor_difference += SL.match_length - res.length
|
437
461
|
SL.match_length = res.length
|
438
|
-
SL.add_report("#{match_string} => #{url}")
|
462
|
+
SL.add_report("#{@match_string} => #{@url}")
|
439
463
|
res
|
440
464
|
end
|
441
465
|
else
|
442
|
-
SL.add_error('No results', "#{search_terms} (#{match_string})")
|
466
|
+
SL.add_error('No results', "#{search_terms} (#{@match_string})")
|
443
467
|
counter_errors += 1
|
444
468
|
match
|
445
469
|
end
|
@@ -450,9 +474,9 @@ module SL
|
|
450
474
|
end
|
451
475
|
end
|
452
476
|
end
|
453
|
-
line_difference += 1 if delete_line
|
454
|
-
out.push(line) unless delete_line
|
455
|
-
delete_line = false
|
477
|
+
line_difference += 1 if @delete_line
|
478
|
+
out.push(line) unless @delete_line
|
479
|
+
@delete_line = false
|
456
480
|
end
|
457
481
|
warn "\n" unless SILENT
|
458
482
|
|
@@ -514,8 +538,8 @@ module SL
|
|
514
538
|
## using hostname as text without doing search
|
515
539
|
if SL::URL.only_url?(input.strip)
|
516
540
|
type = reference_link ? :ref_title : :inline
|
517
|
-
url, title = SL::URL.url_to_link(input.strip, type)
|
518
|
-
print SL.make_link(type, title, url, title: false, force_title: false)
|
541
|
+
@url, title = SL::URL.url_to_link(input.strip, type)
|
542
|
+
print SL.make_link(type, title, @url, title: false, force_title: false)
|
519
543
|
Process.exit
|
520
544
|
end
|
521
545
|
|
@@ -532,10 +556,10 @@ module SL
|
|
532
556
|
# input.sub!(/\+.*?$/, '').strip!
|
533
557
|
# end
|
534
558
|
|
535
|
-
link_text = false
|
559
|
+
@link_text = false
|
536
560
|
|
537
561
|
if input =~ /"(.*?)"/
|
538
|
-
link_text = Regexp.last_match(1)
|
562
|
+
@link_text = Regexp.last_match(1)
|
539
563
|
input.gsub!(/"(.*?)"/, '\1')
|
540
564
|
end
|
541
565
|
|
@@ -546,7 +570,7 @@ module SL
|
|
546
570
|
when /^!(\S+)\s+(.*)$/
|
547
571
|
type = Regexp.last_match(1)
|
548
572
|
link_info = Regexp.last_match(2).strip
|
549
|
-
link_text ||= link_info
|
573
|
+
@link_text ||= link_info
|
550
574
|
terms = link_info + additional_terms
|
551
575
|
terms.strip!
|
552
576
|
|
@@ -556,7 +580,7 @@ module SL
|
|
556
580
|
SL.config['custom_site_searches'].each do |k, v|
|
557
581
|
next unless type == k
|
558
582
|
|
559
|
-
link_text = terms if link_text == ''
|
583
|
+
@link_text = terms if @link_text == ''
|
560
584
|
v = parse_arguments(v, { no_restore: true })
|
561
585
|
if v =~ %r{^(/|http)}i
|
562
586
|
type = 'r'
|
@@ -601,7 +625,7 @@ module SL
|
|
601
625
|
end
|
602
626
|
end
|
603
627
|
else
|
604
|
-
type = SL::GoogleSearch.
|
628
|
+
type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
605
629
|
terms = "site:#{v} #{terms}"
|
606
630
|
end
|
607
631
|
|
@@ -612,12 +636,12 @@ module SL
|
|
612
636
|
# if contains TLD, use site-specific search
|
613
637
|
if type =~ /^(\S+\.)+\S+$/
|
614
638
|
terms = "site:#{type} #{terms}"
|
615
|
-
type = SL::GoogleSearch.
|
639
|
+
type = SL::GoogleSearch.api_key? ? 'gg' : 'g'
|
616
640
|
end
|
617
|
-
search_count ||= 0
|
618
|
-
search_count += 1
|
641
|
+
@search_count ||= 0
|
642
|
+
@search_count += 1
|
619
643
|
|
620
|
-
url, title, link_text = do_search(type, terms, link_text, search_count)
|
644
|
+
@url, title, @link_text = do_search(type, terms, @link_text, @search_count)
|
621
645
|
else
|
622
646
|
SL.add_error("Invalid search#{SL::Searches.did_you_mean(type)}", input)
|
623
647
|
counter_errors += 1
|
@@ -630,25 +654,25 @@ module SL
|
|
630
654
|
else
|
631
655
|
't'
|
632
656
|
end
|
633
|
-
link_text = input.sub(/^[tfilm]/, '')
|
634
|
-
url, title = SL::SocialSearch.social_handle(type, link_text)
|
635
|
-
link_text = title
|
657
|
+
@link_text = input.sub(/^[tfilm]/, '')
|
658
|
+
@url, title = SL::SocialSearch.social_handle(type, @link_text)
|
659
|
+
@link_text = title
|
636
660
|
else
|
637
|
-
link_text ||= input
|
638
|
-
url, title, link_text = SL.ddg(input, link_text)
|
661
|
+
@link_text ||= input
|
662
|
+
@url, title, @link_text = SL.ddg(input, @link_text)
|
639
663
|
end
|
640
664
|
|
641
|
-
if url
|
665
|
+
if @url
|
642
666
|
if type =~ /sp(ell)?/
|
643
|
-
SL.add_output(url)
|
667
|
+
SL.add_output(@url)
|
644
668
|
elsif link_only
|
645
|
-
SL.add_output(url)
|
646
|
-
elsif url == 'embed'
|
669
|
+
SL.add_output(@url)
|
670
|
+
elsif @url == 'embed'
|
647
671
|
SL.add_output(title)
|
648
672
|
else
|
649
673
|
type = reference_link ? :ref_title : :inline
|
650
674
|
|
651
|
-
SL.add_output SL.make_link(type, link_text, url, title: title, force_title: false)
|
675
|
+
SL.add_output SL.make_link(type, @link_text, @url, title: title, force_title: false)
|
652
676
|
SL.print_errors
|
653
677
|
end
|
654
678
|
else
|