Module: SL

Includes:
URL
Defined in:
lib/searchlink/output.rb,
lib/searchlink/url.rb,
lib/searchlink/help.rb,
lib/searchlink/util.rb,
lib/searchlink/parse.rb,
lib/searchlink/config.rb,
lib/searchlink/config.rb,
lib/searchlink/output.rb,
lib/searchlink/search.rb,
lib/searchlink/semver.rb,
lib/searchlink/version.rb,
lib/searchlink/version.rb,
lib/searchlink/searches.rb,
lib/searchlink/searches/hook.rb,
lib/searchlink/searches/tmdb.rb,
lib/searchlink/searches/bitly.rb,
lib/searchlink/searches/amazon.rb,
lib/searchlink/searches/github.rb,
lib/searchlink/searches/google.rb,
lib/searchlink/searches/itunes.rb,
lib/searchlink/searches/lastfm.rb,
lib/searchlink/searches/lyrics.rb,
lib/searchlink/searches/social.rb,
lib/searchlink/searches/history.rb,
lib/searchlink/searches/twitter.rb,
lib/searchlink/searches/youtube.rb,
lib/searchlink/searches/pinboard.rb,
lib/searchlink/searches/software.rb,
lib/searchlink/searches/spelling.rb,
lib/searchlink/searches/spotlight.rb,
lib/searchlink/searches/wikipedia.rb,
lib/searchlink/searches/applemusic.rb,
lib/searchlink/searches/definition.rb,
lib/searchlink/searches/duckduckgo.rb,
lib/searchlink/searches/duckduckgo.rb,
lib/searchlink/searches/stackoverflow.rb,
lib/searchlink/searches/helpers/safari.rb,
lib/searchlink/searches/helpers/firefox.rb,
lib/searchlink/searches/helpers/chromium.rb

Overview

Chromium (Chrome, Arc, Brave, Edge) search methods

Defined Under Namespace

Modules: Searches, URL, Util Classes: AmazonSearch, AppleMusicSearch, BitlySearch, DefinitionSearch, DuckDuckGoSearch, GitHubSearch, GoogleSearch, HistorySearch, HookSearch, ITunesSearch, LastFMSearch, LyricsSearch, PinboardSearch, SearchLink, SemVer, SocialSearch, SoftwareSearch, SpellSearch, SpotlightSearch, StackOverflowSearch, TMDBSearch, TwitterSearch, WikipediaSearch, YouTubeSearch

Constant Summary collapse

VERSION =
'2.3.63'

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from URL

amazon_affiliatize, only_url?, ref_title_for_url, title, url?, url_to_link, valid_link?

Class Attribute Details

.clipboardObject

Whether or not to copy results to clipbpard



12
13
14
# File 'lib/searchlink/output.rb', line 12

def clipboard
  @clipboard ||= false
end

.configObject



5
6
7
# File 'lib/searchlink/config.rb', line 5

def config
  @config ||= SL::SearchLink.new({ echo: true })
end

.errorsObject

Stores generated errors



57
58
59
# File 'lib/searchlink/output.rb', line 57

def errors
  @errors ||= {}
end

Stores the footer with reference links and footnotes



32
33
34
# File 'lib/searchlink/output.rb', line 32

def footer
  @footer ||= []
end

.line_numObject

Tracks the line number of each link match for debug output



37
38
39
# File 'lib/searchlink/output.rb', line 37

def line_num
  @line_num ||= 0
end

.match_columnObject

Tracks the column of each link match for debug output



42
43
44
# File 'lib/searchlink/output.rb', line 42

def match_column
  @match_column ||= 0
end

.match_lengthObject

Tracks the length of each link match for debug output



47
48
49
# File 'lib/searchlink/output.rb', line 47

def match_length
  @match_length ||= 0
end

.originputObject

Stores the original input



52
53
54
# File 'lib/searchlink/output.rb', line 52

def originput
  @originput ||= ''
end

.outputObject

Stores the generated output



22
23
24
# File 'lib/searchlink/output.rb', line 22

def output
  @output ||= []
end

.prev_configObject



9
10
11
# File 'lib/searchlink/config.rb', line 9

def prev_config
  @prev_config ||= {}
end

.printoutObject

Whether or not to echo results to STDOUT as they’re created



17
18
19
# File 'lib/searchlink/output.rb', line 17

def printout
  @printout ||= false
end

.reportObject

Stores the generated debug report



27
28
29
# File 'lib/searchlink/output.rb', line 27

def report
  @report ||= []
end

.titleizeObject

Whether or not to add a title to the output



7
8
9
# File 'lib/searchlink/output.rb', line 7

def titleize
  @titleize ||= false
end

Class Method Details

.add_error(type, str) ⇒ nil

Adds the given string to the errors.

Parameters:

  • type (Symbol)

    The type of error.

  • str (String)

    The string to add.

Returns:

  • (nil)


188
189
190
191
192
193
194
195
196
197
198
# File 'lib/searchlink/output.rb', line 188

def add_error(type, str)
  return unless SL.config['debug']

  unless SL.line_num.nil?
    position = "#{SL.line_num}:"
    position += SL.match_column.nil? ? '0:' : "#{SL.match_column}:"
    position += SL.match_length.nil? ? '0' : SL.match_length.to_s
  end
  SL.errors[type] ||= []
  SL.errors[type].push("(#{position}): #{str}")
end

Adds the given string to the footer.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


128
129
130
131
# File 'lib/searchlink/output.rb', line 128

def add_footer(str)
  SL.footer ||= []
  SL.footer.push(str.strip)
end

.add_output(str) ⇒ nil

Adds the given string to the output.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


117
118
119
120
# File 'lib/searchlink/output.rb', line 117

def add_output(str)
  print str if SL.printout && !SL.clipboard
  SL.output << str
end

.add_report(str) ⇒ nil

Adds the given string to the report.

Parameters:

  • str (String)

    The string to add.

Returns:

  • (nil)


169
170
171
172
173
174
175
176
177
178
179
# File 'lib/searchlink/output.rb', line 169

def add_report(str)
  return unless SL.config['report']

  unless SL.line_num.nil?
    position = "#{SL.line_num}:"
    position += SL.match_column.nil? ? '0:' : "#{SL.match_column}:"
    position += SL.match_length.nil? ? '0' : SL.match_length.to_s
  end
  SL.report.push("(#{position}): #{str}")
  warn "(#{position}): #{str}" unless SILENT
end

.ddg(search_terms, link_text = nil, timeout: , google: true, image: false) ⇒ SL::Searches::Result

Performs a DuckDuckGo search with the given search terms and link text. If link text is not provided, the first result will be returned. The search will timeout after the given number of seconds.

Parameters:

  • search_terms (String)

    The search terms to use

  • link_text (String) (defaults to: nil)

    The text of the link to search for

  • timeout (Integer) (defaults to: )

    The timeout for the search in seconds

Returns:

  • (SL::Searches::Result)

    The search result



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/searchlink/searches/duckduckgo.rb', line 149

def ddg(search_terms, link_text = nil, timeout: SL.config['timeout'], google: true, image: false)
  if google && SL::GoogleSearch.test_for_key
    s_class = 'google'
    s_type = image ? 'img' : 'gg'
  else
    s_class = 'duckduckgo'
    s_type = image ? 'ddgimg' : 'g'
  end

  search = proc { SL::Searches.plugins[:search][s_class][:class].search(s_type, search_terms, link_text) }
  SL::Util.search_with_timeout(search, timeout)
end

.first_image(url) ⇒ Object



162
163
164
165
# File 'lib/searchlink/searches/duckduckgo.rb', line 162

def first_image(url)
  images = Curl::Html.new(url).images
  images.filter { |img| img[:type] == 'img' }.first[:src]
end

.google(search_terms, link_text = nil, timeout: , image: false) ⇒ Object

Performs a Google search if API key is available, otherwise defaults to DuckDuckGo

Parameters:

  • search_terms (String)

    The search terms

  • link_text (String) (defaults to: nil)

    The link text

  • timeout (Integer) (defaults to: )

    The timeout



124
125
126
127
128
129
130
131
132
133
134
# File 'lib/searchlink/searches/duckduckgo.rb', line 124

def google(search_terms, link_text = nil, timeout: SL.config['timeout'], image: false)
  if SL::GoogleSearch.test_for_key
    s_class = 'google'
    s_type = image ? 'img' : 'gg'
  else
    s_class = 'duckduckgo'
    s_type = image ? 'ddgimg' : 'g'
  end
  search = proc { SL::Searches.plugins[:search][s_class][:class].search(s_type, search_terms, link_text) }
  SL::Util.search_with_timeout(search, timeout)
end

Creates a link of the specified type with the given text, url, and title.

Parameters:

  • type (Symbol)

    The type of link to create.

  • text (String)

    The text of the link.

  • url (String)

    The URL of the link.

  • title (String) (defaults to: false)

    The title of the link.

  • force_title (Boolean) (defaults to: false)

    Whether to force the title to be included.

Returns:



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/searchlink/output.rb', line 92

def make_link(type, text, url, title: false, force_title: false)
  title = title.gsub(/\P{Print}|\p{Cf}/, '') if title
  text = title || SL::URL.title(url) if SL.titleize && (!text || text.strip.empty?)
  text = text ? text.strip : title
  title = title && (SL.config['include_titles'] || force_title) ? %( "#{title.clean}") : ''

  title.gsub!(/[ \t]+/, ' ')

  case type.to_sym
  when :ref_title
    %(\n[#{text}]: #{url}#{title})
  when :ref_link
    %([#{text}][#{url}])
  when :inline
    image = url =~ /\.(gif|jpe?g|png|webp)$/ ? '!' : ''
    %(#{image}[#{text}](#{url}#{title}))
  end
end

.new_version?Boolean

Check for a newer version than local copy using GitHub release tag

Returns:

  • (Boolean)

    false if no new version, or semantic version of latest release



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
# File 'lib/searchlink/version.rb', line 36

def new_version?
  headers = {
    'Accept' => 'application/vnd.github+json',
    'X-GitHub-Api-Version' => '2022-11-28'
  }
  headers['Authorization'] = "Bearer #{Secrets::GH_AUTH_TOKEN}" if defined? Secrets::GH_AUTH_TOKEN

  url = 'https://api.github.com/repos/ttscoff/searchlink/releases/latest'
  page = Curl::Json.new(url, headers: headers)
  result = page.json

  if result
    latest_tag = result['tag_name']

    return false unless latest_tag

    return false if latest_tag =~ /^#{Regexp.escape(SL::VERSION)}$/

    latest = SemVer.new(latest_tag)
    current = SemVer.new(SL::VERSION)

    return latest_tag if current.older_than(latest)
  else
    warn 'Check for new version failed.'
  end

  false
end

.notify(str, sub) ⇒ Object

Posts macOS notifications

Parameters:

  • str

    The title of the notification

  • sub

    The text of the notification



66
67
68
69
70
# File 'lib/searchlink/output.rb', line 66

def notify(str, sub)
  return unless SL.config['notifications']

  `osascript -e 'display notification "SearchLink" with title "#{str}" subtitle "#{sub}"'`
end

Prints the errors.

Parameters:

  • type (String) (defaults to: 'Errors')

    The type of errors.

Returns:



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/searchlink/output.rb', line 219

def print_errors(type = 'Errors')
  return if SL.errors.empty?

  out = ''
  inline = if SL.originput.split(/\n/).length > 1
             false
           else
             SL.config['inline'] || SL.originput.split(/\n/).length == 1
           end

  SL.errors.each do |k, v|
    next if v.empty?

    v.each_with_index do |err, i|
      out += "(#{k}) #{err}"
      out += if inline
               i == v.length - 1 ? ' | ' : ', '
             else
               "\n"
             end
    end
  end

  unless out == ''
    sep = inline ? ' ' : "\n"
    out.sub!(/\| /, '')
    out = "#{sep}<!-- #{type}:#{sep}#{out}-->#{sep}"
  end
  if SL.clipboard
    warn out
  else
    add_output out
  end
end

Prints the footer.

Returns:



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
# File 'lib/searchlink/output.rb', line 137

def print_footer
  unless SL.footer.empty?

    footnotes = []
    SL.footer.delete_if do |note|
      note.strip!
      case note
      when /^\[\^.+?\]/
        footnotes.push(note)
        true
      when /^\s*$/
        true
      else
        false
      end
    end

    output = SL.footer.sort.join("\n").strip
    output += "\n\n" if !output.empty? && !footnotes.empty?
    output += footnotes.join("\n\n") unless footnotes.empty?
    return output.gsub(/\n{3,}/, "\n\n")
  end

  ''
end

Prints or copies the given text.

Parameters:

  • text (String)

    The text to print or copy.

Returns:

  • (nil)


260
261
262
263
264
265
266
267
268
# File 'lib/searchlink/output.rb', line 260

def print_or_copy(text)
  # Process.exit unless text
  if SL.clipboard
    `echo #{Shellwords.escape(text)}|tr -d "\n"|pbcopy`
    print SL.originput
  else
    print text
  end
end

Prints the report.

Returns:



204
205
206
207
208
209
210
211
# File 'lib/searchlink/output.rb', line 204

def print_report
  return if (SL.config['inline'] && SL.originput.split(/\n/).length == 1) || SL.clipboard

  return if SL.report.empty?

  out = "\n<!-- Report:\n#{SL.report.join("\n")}\n-->\n"
  add_output out
end

.spell(phrase) ⇒ Object



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
# File 'lib/searchlink/searches/spelling.rb', line 26

def spell(phrase)
  aspell = if File.exist?('/usr/local/bin/aspell')
             '/usr/local/bin/aspell'
           elsif File.exist?('/opt/homebrew/bin/aspell')
             '/opt/homebrew/bin/aspell'
           else
             `which aspell`.strip
           end

  if aspell.nil? || aspell.empty?
    SL.add_error('Missing aspell', 'Install aspell in to allow spelling corrections')
    return false
  end

  words = phrase.split(/\b/)
  output = ''
  words.each do |w|
    if w =~ /[A-Za-z]+/
      spell_res = `echo "#{w}" | #{aspell} --sug-mode=bad-spellers -C pipe | head -n 2 | tail -n 1`
      if spell_res.strip == "\*"
        output += w
      else
        spell_res.sub!(/.*?: /, '')
        results = spell_res.split(/, /).delete_if { |word| phrase =~ /^[a-z]/ && word =~ /[A-Z]/ }
        output += results[0]
      end
    else
      output += w
    end
  end
  output
end


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
# File 'lib/searchlink/version.rb', line 65

def update_searchlink
  new_version = SL.new_version?
  if new_version
    folder = File.expand_path('~/Downloads')
    services = File.expand_path('~/Library/Services')
    dl = File.join(folder, 'SearchLink.zip')
    curl = TTY::Which.which('curl')
    `#{curl} -SsL -o "#{dl}" https://github.com/ttscoff/searchlink/releases/latest/download/SearchLink.zip`
    Dir.chdir(folder)
    `unzip -qo #{dl} -d #{folder}`
    FileUtils.rm(dl)

    ['SearchLink', 'SearchLink File', 'Jump to SearchLink Error'].each do |workflow|
      wflow = "#{workflow}.workflow"
      src = File.join(folder, 'SearchLink Services', wflow)
      dest = File.join(services, wflow)
      if File.exist?(src) && File.exist?(dest)
        FileUtils.rm_rf(dest)
        FileUtils.mv(src, dest, force: true)
      end
    end
    add_output("Installed SearchLink #{new_version}")
    FileUtils.rm_rf('SearchLink Services')
  else
    add_output('Already up to date.')
  end
end

.version_checkObject



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
# File 'lib/searchlink/version.rb', line 7

def version_check
  cachefile = File.expand_path('~/.searchlink_update_check')
  if File.exist?(cachefile)
    last_check, latest_tag = IO.read(cachefile).strip.split(/\|/)
    last_time = Time.parse(last_check)
  else
    latest_tag = new_version?
    last_time = Time.now
  end

  if last_time + (24 * 60 * 60) < Time.now
    latest_tag = new_version?
    last_time = Time.now
  end

  latest_tag ||= SL::VERSION
  latest = SemVer.new(latest_tag)
  current = SemVer.new(SL::VERSION)
  
  File.open(cachefile, 'w') { |f| f.puts("#{last_time.strftime('%c')}|#{latest.to_s}") }

  return "SearchLink v#{current.to_s}, #{latest.to_s} available. Run 'update' to download." if latest_tag && current.older_than(latest)

  "SearchLink v#{current.to_s}"
end