open 0.1.30 → 0.2.22

Sign up to get free protection for your applications and to get access to all the features.
Files changed (39) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +110 -31
  3. data/bin/open +1 -1
  4. data/bin/open_in_browser +1 -1
  5. data/doc/README.gen +72 -8
  6. data/doc/deprecated_code/deprecated_code.md +26 -0
  7. data/img/open_logo.png +0 -0
  8. data/lib/open/base/base.rb +265 -128
  9. data/lib/open/books/README.md +8 -0
  10. data/lib/open/books/books.rb +100 -0
  11. data/lib/open/constants/constants.rb +86 -8
  12. data/lib/open/in_browser/in_browser.rb +501 -319
  13. data/lib/open/in_editor/in_editor.rb +367 -162
  14. data/lib/open/last/last.rb +14 -12
  15. data/lib/open/last_url/last_url.rb +4 -3
  16. data/lib/open/nano_open/nano_open.rb +30 -5
  17. data/lib/open/{open.rb → open/open.rb} +596 -654
  18. data/lib/open/project/project.rb +3 -2
  19. data/lib/open/requires/failsafe_require_of_beautiful_url.rb +9 -0
  20. data/lib/open/requires/require_the_project.rb +2 -2
  21. data/lib/open/requires/require_yaml.rb +13 -0
  22. data/lib/open/these_files/these_files.rb +1 -1
  23. data/lib/open/toplevel_methods/browser.rb +71 -0
  24. data/lib/open/toplevel_methods/delay.rb +23 -0
  25. data/lib/open/toplevel_methods/e.rb +14 -0
  26. data/lib/open/toplevel_methods/editor.rb +41 -0
  27. data/lib/open/toplevel_methods/host_os.rb +25 -0
  28. data/lib/open/toplevel_methods/is_on_roebe.rb +16 -0
  29. data/lib/open/toplevel_methods/is_on_windows.rb +26 -0
  30. data/lib/open/toplevel_methods/misc.rb +50 -0
  31. data/lib/open/version/version.rb +2 -2
  32. data/lib/open/with_delay/with_delay.rb +10 -11
  33. data/open.gemspec +3 -3
  34. data/test/testing_open.rb +1 -1
  35. data/test/testing_open_in_browser.rb +16 -0
  36. data/test/testing_open_via_launchy.rb +3 -0
  37. data/test/testing_shortcuts.rb +3 -0
  38. metadata +25 -9
  39. data/lib/open/toplevel_code/toplevel_code.rb +0 -189
@@ -4,6 +4,8 @@
4
4
  # =========================================================================== #
5
5
  # === Open::InBrowser
6
6
  #
7
+ # This class has been re-written on the 16.06.2023.
8
+ #
7
9
  # This class can be used to open something in the browser.
8
10
  #
9
11
  # The correct browser to use is, by default, the one that is stored in
@@ -18,8 +20,12 @@
18
20
  # require 'open'
19
21
  # Open::InBrowser.new(ARGV)
20
22
  #
23
+ # Usage example:
24
+ #
25
+ # Open::InBrowser.new(ARGV)
26
+ #
21
27
  # =========================================================================== #
22
- # To open a remote webpage in firefox only, do:
28
+ # To open a remote webpage in firefox only, you could use this code:
23
29
  #
24
30
  # use_this_browser = 'firefox -new-tab'
25
31
  # _ = use_this_browser
@@ -27,30 +33,39 @@
27
33
  #
28
34
  # =========================================================================== #
29
35
  # require 'open/in_browser/in_browser.rb'
36
+ # Open.in_browser(ARGV)
37
+ # Open::InBrowser.new(ARGV)
30
38
  # =========================================================================== #
31
39
  require 'open/base/base.rb'
32
40
 
33
41
  module Open
34
42
 
35
- class InBrowser < Base # === Open::InBrowser
43
+ class InBrowser < ::Open::Base # === Open::InBrowser
36
44
 
37
45
  begin
38
46
  require 'studium/requires/return_remote_homepage_of_this_lecture.rb'
39
47
  rescue LoadError; end
40
48
 
49
+ # ========================================================================= #
50
+ # === ARRAY_TOKENS_THAT_REQUIRE_PADDING
51
+ # ========================================================================= #
52
+ ARRAY_TOKENS_THAT_REQUIRE_PADDING = ['#',';','(','&',')']
53
+
41
54
  # ========================================================================= #
42
55
  # === initialize
43
56
  #
44
- # The first argument becomes the target URL that will be opened.
57
+ # The target URLs to be opened will be stored in the first argument,
58
+ # called commandline_arguments.
45
59
  # ========================================================================= #
46
60
  def initialize(
47
- target_url = '',
48
- run_already = true
61
+ commandline_arguments = ARGV,
62
+ run_already = true,
63
+ &block
49
64
  )
50
65
  register_sigint
51
66
  reset
52
- set_target_url(
53
- target_url
67
+ set_commandline_arguments(
68
+ commandline_arguments
54
69
  )
55
70
  # ======================================================================= #
56
71
  # === Handle blocks given next
@@ -65,12 +80,21 @@ class InBrowser < Base # === Open::InBrowser
65
80
  # part of the opened URL.
66
81
  # ===================================================================== #
67
82
  when :no_localhost
68
- @internal_hash[:use_localhost_URLs] = false
83
+ do_not_use_localhost
69
84
  # ===================================================================== #
70
85
  # === :try_to_use_localhost_URLs
71
86
  # ===================================================================== #
72
87
  when :try_to_use_localhost_URLs
73
- @internal_hash[:use_localhost_URLs] = true
88
+ do_use_localhost
89
+ # ===================================================================== #
90
+ # === be_silent
91
+ #
92
+ # This entry point will make this class be quiet, that is, not display
93
+ # the actual command that is used.
94
+ # ===================================================================== #
95
+ when :be_silent,
96
+ :be_quiet
97
+ do_not_show_the_sys_command_that_is_used
74
98
  # ===================================================================== #
75
99
  # === :open_in_background
76
100
  # ===================================================================== #
@@ -81,20 +105,11 @@ class InBrowser < Base # === Open::InBrowser
81
105
  #
82
106
  # This clause can be entered by code such as the following:
83
107
  #
84
- # Open.open_in_browser(port: 8080,) { :use_launchy }
108
+ # Open.in_browser(port: 8080) { :use_launchy }
85
109
  #
86
110
  # ===================================================================== #
87
111
  when :use_launchy
88
112
  set_use_launchy(true)
89
- # ===================================================================== #
90
- # === be_silent
91
- #
92
- # This entry point will make this class be quiet, that is, not display
93
- # the actual command that is used.
94
- # ===================================================================== #
95
- when :be_silent,
96
- :be_quiet
97
- @internal_hash[:show_the_sys_command_that_is_used] = false
98
113
  else
99
114
  # =================================================================== #
100
115
  # Handle Hashes next:
@@ -111,9 +126,25 @@ class InBrowser < Base # === Open::InBrowser
111
126
  if yielded.has_key? :use_this_browser
112
127
  set_use_this_browser(yielded[:use_this_browser])
113
128
  end
129
+ # ================================================================= #
130
+ # === :remaining_commandline_arguments
131
+ # ================================================================= #
132
+ if yielded.has_key? :remaining_commandline_arguments
133
+ _ = yielded[:remaining_commandline_arguments]
134
+ if _
135
+ append_array_to_commandline_arguments(_)
136
+ end
137
+ end
114
138
  end
115
139
  end
116
140
  end
141
+ case run_already
142
+ # ======================================================================= #
143
+ # === :do_not_run_yet
144
+ # ======================================================================= #
145
+ when :do_not_run_yet
146
+ run_already = false
147
+ end
117
148
  run if run_already
118
149
  end
119
150
 
@@ -121,7 +152,6 @@ class InBrowser < Base # === Open::InBrowser
121
152
  # === reset (reset tag)
122
153
  # ========================================================================= #
123
154
  def reset
124
- super()
125
155
  # ======================================================================= #
126
156
  # === @internal_hash
127
157
  # ======================================================================= #
@@ -131,12 +161,16 @@ class InBrowser < Base # === Open::InBrowser
131
161
  #
132
162
  # This variable will keep track as to which browser will be used.
133
163
  # ======================================================================= #
134
- @internal_hash[:use_this_browser] = ::Open.use_which_browser?
164
+ @internal_hash[:use_this_browser] = determine_which_browser_to_use_then_return_the_result
135
165
  # ======================================================================= #
136
166
  # === :open_in_background
137
167
  # ======================================================================= #
138
168
  @internal_hash[:open_in_background] = false
139
169
  # ======================================================================= #
170
+ # === :show_the_sys_command_that_is_used
171
+ # ======================================================================= #
172
+ @internal_hash[:show_the_sys_command_that_is_used] = true
173
+ # ======================================================================= #
140
174
  # === :use_launchy
141
175
  #
142
176
  # This variable will make use of the launchy gem rather than the
@@ -144,54 +178,53 @@ class InBrowser < Base # === Open::InBrowser
144
178
  # way, in particular for windows, to open remote URLs. That way
145
179
  # we can query for the windows operating system and silently
146
180
  # delegate onto the launchy gem, if need be. The default value
147
- # is false, though, so most users of this class have to enable
148
- # this on their own.
181
+ # for this setting is false, though, so most users of this
182
+ # class have to enable this on their own.
149
183
  # ======================================================================= #
150
184
  @internal_hash[:use_launchy] = false
151
185
  # ======================================================================= #
152
- # === :show_the_sys_command_that_is_used
186
+ # === :work_on_these_URLs
187
+ #
188
+ # The following variable must be kept as Array at all times.
153
189
  # ======================================================================= #
154
- @internal_hash[:show_the_sys_command_that_is_used] = true
190
+ @internal_hash[:work_on_these_URLs] = []
155
191
  # ======================================================================= #
156
192
  # === :append_this_string
157
193
  # ======================================================================= #
158
194
  @internal_hash[:append_this_string] = ''.dup
159
195
  # ======================================================================= #
160
- # === :use_localhost_URLs
196
+ # === :use_localhost
197
+ #
198
+ # If this variable is true then we will try to use 'localhost'
199
+ # entries, at the least on roebe-system.
161
200
  #
162
- # By default we will not use localhost-URLs. If we are on a roebe-system,
163
- # however had, we will use localhost URLs.
201
+ # By default the class will try to use localhost-URLs. Use the
202
+ # commandline flag --do-not-use-localhost if you dislike or
203
+ # do not need this feature.
164
204
  # ======================================================================= #
165
- @internal_hash[:use_localhost_URLs] = false
205
+ @internal_hash[:use_localhost] = true
166
206
  end
167
207
 
168
208
  # ========================================================================= #
169
- # === set_open_in_background
209
+ # === do_use_localhost
170
210
  # ========================================================================= #
171
- def set_open_in_background(i = true)
172
- @internal_hash[:open_in_background] = i
211
+ def do_use_localhost
212
+ @internal_hash[:use_localhost] = true
173
213
  end
174
214
 
175
215
  # ========================================================================= #
176
- # === set_use_launchy
216
+ # === do_not_use_localhost
177
217
  # ========================================================================= #
178
- def set_use_launchy(i = false)
179
- @internal_hash[:use_launchy] = i
218
+ def do_not_use_localhost
219
+ @internal_hash[:use_localhost] = false
180
220
  end
181
221
 
182
222
  # ========================================================================= #
183
- # === show_the_sys_command_that_is_used?
223
+ # === use_localhost?
184
224
  # ========================================================================= #
185
- def show_the_sys_command_that_is_used?
186
- @internal_hash[:show_the_sys_command_that_is_used]
187
- end
188
-
189
- # ========================================================================= #
190
- # === use_launchy?
191
- # ========================================================================= #
192
- def use_launchy?
193
- @internal_hash[:use_launchy]
194
- end
225
+ def use_localhost?
226
+ @internal_hash[:use_localhost]
227
+ end; alias use_localhost_URLs? use_localhost? # === use_localhost_URLs?
195
228
 
196
229
  # ========================================================================= #
197
230
  # === check_for_intralink
@@ -204,10 +237,9 @@ class InBrowser < Base # === Open::InBrowser
204
237
  use_this_token_to_replace_empty_spaces = '_'
205
238
  )
206
239
  if i
207
- if i.is_a? Array
208
- i = i.join(' ').tr(' ', use_this_token_to_replace_empty_spaces)
209
- end
240
+ i = i.join(' ') if i.is_a? Array
210
241
  i = i.to_s # Ensure that we have a String at this point.
242
+ i.tr!(' ', use_this_token_to_replace_empty_spaces) if i.include?(' ')
211
243
  unless i.empty?
212
244
  @internal_hash[:append_this_string] << i
213
245
  end
@@ -215,132 +247,41 @@ class InBrowser < Base # === Open::InBrowser
215
247
  end
216
248
 
217
249
  # ========================================================================= #
218
- # === set_target_url (URL tag)
219
- #
220
- # We prefer this to be an Array.
221
- #
222
- # Usage example:
223
- #
224
- # Open::InBrowser.new('https://blog.fefe.de/') { :use_launchy }
225
- # Open::InBrowser.new('blog.fefe.de') { :use_launchy }
226
- #
250
+ # === append_this_string?
227
251
  # ========================================================================= #
228
- def set_target_url(
229
- i = ''
230
- )
231
- if i.is_a? Array # This if-clause was added in May 2022.
232
- i = i.join(' ').strip
233
- end
234
- possible_array = beautiful_url(i)
235
- case i
236
- # ======================================================================= #
237
- # === rf HERE
238
- # ======================================================================= #
239
- when /HERE/
240
- i = File.basename(return_pwd)
241
- if is_on_roebe?
242
- i = Studium.return_remote_homepage_of_this_lecture(i)
243
- end
244
- end
245
- if possible_array.is_a?(Array) and (possible_array.size > 1)
246
- i = possible_array
247
- else
248
- if i.is_a? Hash
249
- # =================================================================== #
250
- # === :use_launchy
251
- # =================================================================== #
252
- if i.has_key? :use_launchy
253
- set_use_launchy(i.delete(:use_launchy))
254
- end
255
- # =================================================================== #
256
- # === :with_localhost
257
- #
258
- # Example for this entry point:
259
- #
260
- # Open::InBrowser.new(with_localhost: PORT_TO_USE)
261
- #
262
- # =================================================================== #
263
- if i.has_key?(:with_localhost) and use_localhost_URLs?
264
- i = "http://localhost:#{i.delete(:with_localhost)}/"
265
- # =================================================================== #
266
- # === :port
267
- #
268
- # Example for this entry point:
269
- #
270
- # Open::InBrowser[port: 8080]
271
- #
272
- # =================================================================== #
273
- elsif i.has_key? :port
274
- i = "http://localhost:#{i.delete(:port)}/"
275
- end
276
- end
277
- end
278
- i = [i] unless i.is_a? Array
279
- i.flatten! if i.is_a? Array
280
- # ======================================================================= #
281
- # Work over all members of this Array next.
282
- # ======================================================================= #
283
- i.map! {|entry|
284
- case entry
285
- when :default_sinatra_port
286
- entry = 'http://localhost:4567/'
287
- end
288
- if Object.const_defined? :Studium
289
- possible_link = Studium.return_remote_homepage_of_this_lecture(entry)
290
- if possible_link == entry
291
- else
292
- entry = possible_link
293
- end
294
- end
295
- if ::BeautifulUrl.is_a_local_link?(entry)
296
- entry = ::BeautifulUrl.local_menu(entry)
297
- else
298
- entry = beautiful_url(entry).dup
299
- end
300
- entry = entry.first if entry.is_a? Array
301
- if entry and File.exist?(entry)
302
- entry = File.absolute_path(entry)
303
- end
304
- if entry and entry.start_with?('/home/x/') and !entry.include?('images/') and use_localhost_URLs?
305
- entry.sub!(/\/home\/x\//,'http://localhost/')
306
- end
307
- # ===================================================================== #
308
- # Perform some sanitize-operations next.
309
- # ===================================================================== #
310
- if entry and
311
- ((entry.include?("'") and !entry.include?('"')) or
312
- entry.include?(' ')
313
- )
314
- entry = '"'+entry+'"'
315
- elsif entry and (entry.include?(';') or entry.include?('('))
316
- entry = '"'+entry+'"'
317
- end
318
- unless entry and File.exist?(entry)
319
- # =================================================================== #
320
- # Try a trailing glob next. The idea here is to expand from e. g.
321
- # "image3" towards "image3.jpg", if the latter file exists, and
322
- # the former does NOT exist.
323
- # =================================================================== #
324
- possible_matches = Dir["#{entry}*"]
325
- unless possible_matches.empty?
326
- first = possible_matches.first
327
- if File.exist? first
328
- entry = first
329
- end
330
- end
331
- end
332
- entry
333
- }
334
- @internal_hash[:target_url] = i
252
+ def append_this_string?
253
+ @internal_hash[:append_this_string]
335
254
  end
336
255
 
337
256
  # ========================================================================= #
338
257
  # === target_url?
339
258
  # ========================================================================= #
340
259
  def target_url?
341
- @internal_hash[:target_url]
342
- end; alias target_URL? target_url? # === target_URL?
343
- alias remote_URL? target_url? # === remote_URL?
260
+ @internal_hash[:work_on_these_URLs]
261
+ end; alias target_URL? target_url? # === target_URL?
262
+ alias remote_URL? target_url? # === remote_URL?
263
+ alias work_on_these_URLs? target_url? # === work_on_these_URLs?
264
+
265
+ # ========================================================================= #
266
+ # === set_use_launchy
267
+ # ========================================================================= #
268
+ def set_use_launchy(i = false)
269
+ @internal_hash[:use_launchy] = i
270
+ end
271
+
272
+ # ========================================================================= #
273
+ # === use_launchy?
274
+ # ========================================================================= #
275
+ def use_launchy?
276
+ @internal_hash[:use_launchy]
277
+ end
278
+
279
+ # ========================================================================= #
280
+ # === do_not_show_the_sys_command_that_is_used
281
+ # ========================================================================= #
282
+ def do_not_show_the_sys_command_that_is_used
283
+ @internal_hash[:show_the_sys_command_that_is_used] = false
284
+ end
344
285
 
345
286
  # ========================================================================= #
346
287
  # === first_remote_URL?
@@ -349,6 +290,51 @@ class InBrowser < Base # === Open::InBrowser
349
290
  remote_URL?.first
350
291
  end
351
292
 
293
+ # ========================================================================= #
294
+ # === open_in_background?
295
+ # ========================================================================= #
296
+ def open_in_background?
297
+ @internal_hash[:open_in_background]
298
+ end
299
+
300
+ # ========================================================================= #
301
+ # === set_open_in_background
302
+ # ========================================================================= #
303
+ def set_open_in_background(
304
+ i = true
305
+ )
306
+ @internal_hash[:open_in_background] = i
307
+ end
308
+
309
+ # ========================================================================= #
310
+ # === show_the_sys_command_that_is_used?
311
+ # ========================================================================= #
312
+ def show_the_sys_command_that_is_used?
313
+ @internal_hash[:show_the_sys_command_that_is_used]
314
+ end
315
+
316
+ # ========================================================================= #
317
+ # === determine_which_browser_to_use_then_return_the_result
318
+ # ========================================================================= #
319
+ def determine_which_browser_to_use_then_return_the_result
320
+ result = ::Open.use_which_browser? # Use the generic browser by default here.
321
+ if result.nil?
322
+ result = file_load_default_browser?
323
+ end
324
+ if is_on_windows?
325
+ case result
326
+ # ===================================================================== #
327
+ # === palemoon
328
+ # ===================================================================== #
329
+ when 'palemoon'
330
+ result.prepend(
331
+ "C:\\Program Files\\Pale Moon\\palemoon.exe"
332
+ )
333
+ end
334
+ end
335
+ return result
336
+ end
337
+
352
338
  # ========================================================================= #
353
339
  # === set_use_this_browser
354
340
  # ========================================================================= #
@@ -366,167 +352,344 @@ class InBrowser < Base # === Open::InBrowser
366
352
  end
367
353
 
368
354
  # ========================================================================= #
369
- # === open_in_background?
355
+ # === menu (menu tag)
370
356
  # ========================================================================= #
371
- def open_in_background?
372
- @internal_hash[:open_in_background]
357
+ def menu(i)
358
+ if i.is_a? Array
359
+ i.each {|entry| menu(entry) }
360
+ else
361
+ case i
362
+ # ===================================================================== #
363
+ # === rf --do-use-localhost
364
+ # ===================================================================== #
365
+ when /^-?-?do(-|_)?use(-|_)?localhost$/i,
366
+ /^-?-?localhost$/i
367
+ do_use_localhost
368
+ # ===================================================================== #
369
+ # === rf --do-not-use-localhost
370
+ # ===================================================================== #
371
+ when /^-?-?do(-|_)?not(-|_)?use(-|_)?localhost$/i,
372
+ /^-?-?no(-|_)?localhost$/i
373
+ do_not_use_localhost
374
+ # ===================================================================== #
375
+ # === rf HERE
376
+ #
377
+ # This entry point is mostly only useful on my home system, where
378
+ # the instruction "rf HERE" will delegate towards the Studium
379
+ # namespace.
380
+ # ===================================================================== #
381
+ when /HERE/
382
+ i = File.basename(return_pwd)
383
+ if is_on_roebe?
384
+ i = Studium.return_remote_homepage_of_this_lecture(i)
385
+ @internal_hash[:work_on_these_URLs].each_with_index {|inner_entry, index|
386
+ case inner_entry
387
+ when /^HERE$/
388
+ @internal_hash[:work_on_these_URLs][index] = i
389
+ end
390
+ }
391
+ end
392
+ end
393
+ end
373
394
  end
374
395
 
375
396
  # ========================================================================= #
376
- # === append_this_string?
397
+ # === could_there_be_a_potential_local_file_for?
377
398
  # ========================================================================= #
378
- def append_this_string?
379
- @internal_hash[:append_this_string]
399
+ def could_there_be_a_potential_local_file_for?(i)
400
+ # ======================================================================= #
401
+ # Try a trailing glob next. The idea here is to expand from e. g.
402
+ # "image3" towards "image3.jpg", if the latter file exists, and
403
+ # the former does NOT exist.
404
+ # ======================================================================= #
405
+ all_files = Dir["#{i}*"]
406
+ !all_files.empty?
380
407
  end
381
408
 
382
409
  # ========================================================================= #
383
- # === use_localhost_URLs?
410
+ # === sanitize_work_on_these_URLs
384
411
  # ========================================================================= #
385
- def use_localhost_URLs?
386
- @internal_hash[:use_localhost_URLs]
412
+ def sanitize_work_on_these_URLs
413
+ if @internal_hash[:work_on_these_URLs]
414
+ @internal_hash[:work_on_these_URLs].map! {|entry|
415
+ # =================================================================== #
416
+ # First handle the case where we have a local link.
417
+ # =================================================================== #
418
+ if BeautifulUrl.is_a_local_link?(entry) and !File.exist?(entry)
419
+ entry = BeautifulUrl.local_menu(entry)
420
+ end
421
+ entry
422
+ }
423
+ end
387
424
  end
388
425
 
389
426
  # ========================================================================= #
390
- # === open_in_browser
427
+ # === work_through_all_designated_URLs
391
428
  #
392
- # This is the part that does the actual file-opening via the browser.
429
+ # This is the method that will batch-process the given input (as Array).
393
430
  # ========================================================================= #
394
- def open_in_browser(
395
- optional_append_this_string = append_this_string?
431
+ def work_through_all_designated_URLs(
432
+ i = @internal_hash[:work_on_these_URLs]
396
433
  )
397
- _ = LOCATION_OF_BROWSER_YAML_FILE
398
- browser = use_this_browser? # Use the generic browser by default.
434
+ i.each {|this_argument|
435
+ do_process_this_target_URL(this_argument)
436
+ }
437
+ end
438
+
439
+ # ========================================================================= #
440
+ # === do_process_this_target_URL (URL tag, main tag)
441
+ #
442
+ # This is the main method for this class - it will handle all given
443
+ # input properly.
444
+ # ========================================================================= #
445
+ def do_process_this_target_URL(
446
+ i,
447
+ optional_append_this_string = append_this_string?,
448
+ &block
449
+ )
450
+ i = i.first if i.is_a? Array # This is not necessary, but just in case.
451
+ if block_given?
452
+ yielded = yield
453
+ case yielded
454
+ # ===================================================================== #
455
+ # === :use_launchy
456
+ # ===================================================================== #
457
+ when :use_launchy
458
+ set_use_launchy(yielded.delete(:use_launchy))
459
+ end
460
+ end
461
+ if i.is_a? Symbol
462
+ case i
463
+ # ===================================================================== #
464
+ # === :default_sinatra_port
465
+ # ===================================================================== #
466
+ when :default_sinatra_port
467
+ i = 'http://localhost:4567/'
468
+ else
469
+ i = BeautifulUrl.menu(i)
470
+ end
471
+ end
472
+ if i.is_a? Array # This if-clause was added in May 2022.
473
+ i = i.join(' ').strip
474
+ end
475
+ if i.include?(' # ') # e. g. "https://moodle.univie.ac.at/course/view.php?id=415670 # 2023W"
476
+ i = i.split(' # ').first.to_s.strip
477
+ end
399
478
  # ======================================================================= #
400
- # We must check first whether the file exists or not.
479
+ # The next clause may remove '#'. However had, this is not always a
480
+ # good idea, as these are also linked for intralinks - so for now this
481
+ # was changed June 2023. But, if the file exists locally, then we will
482
+ # simply prepend "file://" instead.
401
483
  # ======================================================================= #
402
- if File.exist? _
403
- browser = YAML.load_file(_).strip.dup # This variable holds which browser to use.
404
- if is_on_windows?
405
- case browser
406
- # =================================================================== #
407
- # === palemoon
408
- # =================================================================== #
409
- when 'palemoon'
410
- browser.prepend(
411
- "C:\\Program Files\\Pale Moon\\palemoon.exe"
412
- )
484
+ if i.include?('#')
485
+ raw_file = i[0 .. (i.index('#') - 1)]
486
+ if File.exist?(raw_file)
487
+ i = i.dup
488
+ i.prepend('file://')
489
+ end
490
+ end
491
+ if optional_append_this_string.size > 0
492
+ # ===================================================================== #
493
+ # We prepend the '#' character before appending it, unless
494
+ # it starts with the '?' character.
495
+ # ===================================================================== #
496
+ unless optional_append_this_string.start_with? '?'
497
+ optional_append_this_string.prepend '#'
498
+ end
499
+ i = i.dup if i.frozen?
500
+ i << optional_append_this_string
501
+ end if optional_append_this_string
502
+ # ======================================================================= #
503
+ # We must check first whether the target file at hand exists or not.
504
+ # ======================================================================= #
505
+ if i.is_a?(String) and File.exist?(i)
506
+ unless i.include?('/')
507
+ i = File.absolute_path(i) # Always use the absolute path in this case.
508
+ if use_localhost? and
509
+ i.start_with?(HOME_DIRECTORY_OF_USER_X)
510
+ i.sub!(/#{HOME_DIRECTORY_OF_USER_X}/, 'http://localhost/')
413
511
  end
414
512
  end
513
+ open_this_in_the_browser(i) # If it exists then we can open it directly here.
514
+ else
415
515
  # ===================================================================== #
416
- # Different browsers have different ways to open a new tab.
516
+ # First find out whether there could be a local file for this input.
517
+ #
518
+ # For instance, the input "image3" could match towards "image3.jpg".
519
+ # So if a file called "image3.jpg" were to exist locally then we
520
+ # will prioritize it next - that is the main point for the
521
+ # following clause.
417
522
  # ===================================================================== #
418
- if browser.include? 'opera' # Perform some sanitizing in this case.
419
- browser << ' -newtab'
523
+ if could_there_be_a_potential_local_file_for?(i)
524
+ i = Dir["#{i}*"].first
525
+ elsif is_beautiful_url_available?
526
+ # =================================================================== #
527
+ # else we will simply try to fetch an entry from BeautifulUrl.
528
+ # =================================================================== #
529
+ new_link = BeautifulUrl.menu(i)
530
+ case new_link
531
+ when i, nil
532
+ # ================================================================= #
533
+ # All is fine in this case.
534
+ # ================================================================= #
535
+ else
536
+ i = new_link
537
+ end
538
+ end
539
+ open_this_in_the_browser(i)
540
+ end
541
+ end
542
+
543
+ # ========================================================================= #
544
+ # === open_this_in_the_browser
545
+ #
546
+ # Usage example:
547
+ #
548
+ # Open.in_browser('https://blog.fefe.de/') { :use_launchy }
549
+ # Open.in_browser('blog.fefe.de') { :use_launchy }
550
+ #
551
+ # ========================================================================= #
552
+ def open_this_in_the_browser(i)
553
+ if i.is_a? Array
554
+ i.each {|entry| open_this_in_the_browser(entry) }
555
+ else
556
+ if i.is_a? Hash
420
557
  # =================================================================== #
421
- # Or alternatively:
558
+ # === :with_localhost
559
+ #
560
+ # Example for this entry point:
422
561
  #
423
- # opera --remote 'openURL(<url>, new-page)'
562
+ # Open::InBrowser.new(with_localhost: PORT_TO_USE)
424
563
  #
425
564
  # =================================================================== #
426
- elsif browser.include? 'firefox'
427
- browser << ' -new-tab'
428
- elsif browser.include?('chromium') or
429
- browser.include?('thorium') or
430
- browser.include?('chrome')
565
+ if i.has_key?(:with_localhost) and use_localhost_URLs?
566
+ i = "http://localhost:#{i.delete(:with_localhost)}/"
431
567
  # =================================================================== #
432
- # This is for chromium-based browsers, including chrome.
568
+ # === :port
569
+ #
570
+ # Example for this entry point:
571
+ #
572
+ # Open::InBrowser[port: 8080]
573
+ #
433
574
  # =================================================================== #
434
- browser << ' --no-sandbox --app-url '
575
+ elsif i.has_key? :port
576
+ i = "http://localhost:#{i.delete(:port)}/"
577
+ end
578
+ end
579
+ i = i.dup if i.frozen?
580
+ if i and i.include?('→')
581
+ i.delete!('→')
582
+ i.strip!
435
583
  end
436
- [target_url?].flatten.each {|entry|
437
- entry = entry.to_s.dup # .dup to avoid that it may be frozen.
438
- real_filename = entry.dup
439
- if real_filename.include?('#')
440
- real_filename = real_filename[0 .. (real_filename.index('#')-1)]
584
+ if i.end_with?('.cgi')
585
+ if is_on_roebe? and
586
+ Dir.pwd.include?('/home/x/') and
587
+ !i.include?('/')
588
+ i = File.absolute_path(i)
441
589
  end
442
- if optional_append_this_string.size > 0
443
- # ================================================================= #
444
- # We prepend the '#' character, unless it starts with the
445
- # '?' character.
446
- # ================================================================= #
447
- unless optional_append_this_string.start_with? '?'
448
- optional_append_this_string.prepend '#'
590
+ i.delete_prefix!(HOME_DIRECTORY_OF_USER_X)
591
+ if use_localhost?
592
+ i.prepend('http://localhost/') unless i.start_with?('http://localhost/')
593
+ else
594
+ if beautiful_url_is_available? and is_on_roebe?
595
+ i.prepend(HOME_DIRECTORY_OF_USER_X) unless i.start_with?(HOME_DIRECTORY_OF_USER_X)
449
596
  end
450
- entry << optional_append_this_string
451
597
  end
598
+ end
599
+ unless i.empty?
600
+ # =================================================================== #
601
+ # Perform some sanitize-operations next.
602
+ # =================================================================== #
603
+ if ARRAY_TOKENS_THAT_REQUIRE_PADDING.any? {|this_token| i.include?(this_token) }
604
+ i = '"'+i+'"' unless i.start_with?('"')
605
+ end
606
+ cmd_to_use = "#{use_this_browser?}".dup
452
607
  # =================================================================== #
453
- # Next, specifically handle the situation on my home system, when
454
- # we are in a directory that includes a substring such as
455
- # '/home/x/data/' and if the target file name ends with '.cgi'.
608
+ # Next, we must determine which options to pass to the browser at
609
+ # hand. Each browser may be different - for instance, firefox
610
+ # responds to a different set of commandline flags compared to
611
+ # chrome, and so forth.
456
612
  # =================================================================== #
457
- if is_on_roebe? and
458
- entry.end_with?('.cgi') and # Modify .cgi files
459
- !entry.start_with?('http') and
460
- !entry.start_with?('http://localhost/')
461
- if return_pwd.include?(DATA_DIRECTORY_AT_HOME)
462
- # ================================================================= #
463
- # We need a target URL such as:
464
- # http://localhost/data/games/AITD/AITD.cgi
465
- # in this case.
466
- # ================================================================= #
467
- unless entry.include? DATA_DIRECTORY_AT_HOME
468
- entry.prepend(return_pwd)
469
- end
470
- entry.squeeze!('/')
471
- quoted_regexp = Regexp.quote(DATA_DIRECTORY_AT_HOME)
472
- entry.sub!(quoted_regexp, 'http://localhost/data/') if use_localhost_URLs?
473
- elsif return_pwd.include?(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
474
- unless entry.include? PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME
475
- entry.prepend(return_pwd)
476
- end
477
- entry.squeeze!('/')
478
- quoted_regexp = Regexp.quote(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
479
- entry.sub!(quoted_regexp, 'http://localhost/programming/') if use_localhost_URLs?
480
- end
613
+ case cmd_to_use
614
+ # =================================================================== #
615
+ # This is for chromium-based browsers, including thorium and chrome.
616
+ # =================================================================== #
617
+ when 'chromium',
618
+ /thorium$/i,
619
+ /chrome$/i
620
+ cmd_to_use << ' --no-sandbox --app-url'
621
+ # =================================================================== #
622
+ # Handle firefox next:
623
+ # =================================================================== #
624
+ when 'firefox'
625
+ cmd_to_use << ' -new-tab'
626
+ # =================================================================== #
627
+ # Handle opera next:
628
+ # =================================================================== #
629
+ when 'opera' # Perform some sanitizing in this case.
630
+ cmd_to_use << ' -newtab'
631
+ # ================================================================= #
632
+ # Or an alternative to the above would be:
633
+ #
634
+ # opera --remote 'openURL(<url>, new-page)'
635
+ #
636
+ # I am not sure which variant to prefer, so I kind of chose one
637
+ # randomly for now.
638
+ # ================================================================= #
481
639
  end
640
+ # if cmd_to_use.include?(' ')
641
+ # # ================================================================= #
642
+ # # Pad it in this case:
643
+ # # ================================================================= #
644
+ # i = '"'+i.to_s+'"'
645
+ # end
646
+ cmd_to_use << " #{i}"
647
+ # =================================================================== #
648
+ # Finally pass it into the designated browser.
649
+ # =================================================================== #
482
650
  if open_in_background?
483
- entry << '&'
484
- end
485
- if File.exist?(real_filename) and
486
- !real_filename.start_with?('file://') and
487
- !entry.include?('http://localhost/')
488
- entry = entry.dup
489
- entry.prepend('file://')
651
+ cmd_to_use << ' &'
490
652
  end
491
- if entry.include? '&'
492
- entry = '"'+entry+'"' unless entry.start_with? '"'
653
+ # =================================================================== #
654
+ # Check for the launchy gem next.
655
+ # =================================================================== #
656
+ if use_launchy?
657
+ cmd_to_use.prepend('launchy ')
493
658
  end
494
659
  # =================================================================== #
495
660
  # Handle windows next:
496
661
  # =================================================================== #
497
662
  if is_on_windows?
498
- # start firefox -new-tab www.howtogeek.com
499
- this_command = "start #{browser} -new-tab #{entry}"
500
- else
501
- this_command = "#{browser} #{entry}"
502
- end
503
- if show_the_sys_command_that_is_used?
504
- e this_command
505
- end
506
- unless entry.empty?
507
663
  # ================================================================= #
508
- # Finally run the system-command, unless we will use the
509
- # launchy gem.
664
+ # Usage example for windows:
665
+ #
666
+ # start firefox -new-tab www.howtogeek.com
667
+ #
510
668
  # ================================================================= #
511
- if use_launchy?
512
- cmd_to_use = 'launchy '+entry.to_s
513
- e cmd_to_use
514
- system(cmd_to_use)
515
- else
516
- system(this_command) # Here we always output.
517
- end
669
+ cmd_to_use = cmd_to_use.dup if cmd_to_use.frozen?
670
+ cmd_to_use.prepend('start ')
518
671
  end
519
- } unless target_url?.nil?
520
- else
521
- browser = 'firefox' # Else, default to this here as a generic fallback.
672
+ if show_the_sys_command_that_is_used?
673
+ e "#{rev}#{cmd_to_use}"
674
+ end
675
+ system "#{cmd_to_use}"
676
+ end
522
677
  end
523
- end
678
+ end; alias open_in_browser open_this_in_the_browser # === open_in_browser
524
679
 
525
680
  # ========================================================================= #
526
681
  # === run (run tag)
527
682
  # ========================================================================= #
528
- def run
529
- open_in_browser
683
+ def run
684
+ @internal_hash[:work_on_these_URLs] = commandline_arguments?
685
+ menu(
686
+ @internal_hash[:work_on_these_URLs]
687
+ )
688
+ @internal_hash[:work_on_these_URLs].reject! {|entry|
689
+ entry.start_with?('--') # Reject entries that start with '--'.
690
+ }
691
+ sanitize_work_on_these_URLs
692
+ work_through_all_designated_URLs
530
693
  end
531
694
 
532
695
  # ========================================================================= #
@@ -538,57 +701,75 @@ class InBrowser < Base # === Open::InBrowser
538
701
 
539
702
  end
540
703
 
704
+ # =========================================================================== #
705
+ # === Open.in_browser
706
+ #
707
+ # This method will always join the input-argument before passing it
708
+ # to the class.
709
+ #
710
+ # Note that the behaviour of Open.in_browser() is in-sync with the
711
+ # method Open.in_editor(), in regards to joinable arguments
712
+ # (typically an Array as input). If you do not want this behaviour
713
+ # then you may have to prepare for this in your own code, such
714
+ # as by having an Array and iterating over it before passing
715
+ # the individual entries into Open.in_browser().
716
+ #
717
+ # Note that this method needs to be somewhat flexible, as we can provide
718
+ # varied input such as shown in the following examples:
719
+ #
720
+ # Open.open_in_browser(port: 8080)
721
+ # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
722
+ # Open.in_browser(:default_sinatra_port)
723
+ #
724
+ # =========================================================================== #
725
+ def self.in_browser(
726
+ i = ARGV,
727
+ &block
728
+ )
729
+ if i.respond_to? :join
730
+ # ======================================================================= #
731
+ # This will only join entries that do not start with '--'.
732
+ # ======================================================================= #
733
+ entries_that_start_with_hyphens = i.select {|entry| entry.start_with?('--') }
734
+ i.reject! {|entry| entry.start_with?('--') }
735
+ new_array = []
736
+ new_array << i.join(' ').strip
737
+ new_array << entries_that_start_with_hyphens unless entries_that_start_with_hyphens.empty?
738
+ i = new_array
739
+ end
541
740
  # ========================================================================= #
542
- # === Open.open_in_browser
543
- #
544
- # Note that this method needs to be somewhat flexible, as we can provide
545
- # varied input such as:
546
- #
547
- # Open.open_in_browser(port: 8080)
548
- # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
549
- #
741
+ # === Handle blocks next
550
742
  # ========================================================================= #
551
- def self.open_in_browser(
552
- i = ARGV,
553
- &block
554
- )
743
+ if block_given?
744
+ yielded = yield
555
745
  # ======================================================================= #
556
- # === Handle blocks next
746
+ # === Handle Hashes next
557
747
  # ======================================================================= #
558
- if block_given?
559
- yielded = yield
748
+ if yielded.is_a? Hash
560
749
  # ===================================================================== #
561
- # === Handle Hashes next
750
+ # === :delay
562
751
  # ===================================================================== #
563
- if yielded.is_a? Hash
564
- # =================================================================== #
565
- # === Handle Hashes past this point
566
- # =================================================================== #
567
- # =================================================================== #
568
- # === :delay
569
- # =================================================================== #
570
- if yielded.has_key? :delay
571
- _ = yielded.delete(:delay)
572
- if _.is_a? String
573
- _ = _.sub(/ seconds/,'')
574
- _ = _.sub(/ second/,'').to_f if _.include? ' second'
575
- _ = _.to_f # Need a Float.
576
- end
577
- sleep(_)
752
+ if yielded.has_key? :delay
753
+ _ = yielded.delete(:delay)
754
+ if _.is_a? String
755
+ _ = _.sub(/ seconds/,'')
756
+ _ = _.sub(/ second/,'').to_f if _.include? ' second'
757
+ _ = _.to_f # Need a Float.
578
758
  end
759
+ sleep(_)
579
760
  end
580
- # else
581
761
  end
582
- ::Open::InBrowser.new(i, &block)
583
- end; self.instance_eval { alias in_browser open_in_browser } # === Open.in_browser
584
- self.instance_eval { alias open open_in_browser } # === Open.open
762
+ end
763
+ return ::Open::InBrowser.new(i, &block)
764
+ end; self.instance_eval { alias open_in_browser in_browser } # === Open.open_in_browser
765
+ self.instance_eval { alias open_browser in_browser } # === Open.open_browser
585
766
 
586
767
  end
587
768
 
588
769
  # =========================================================================== #
589
770
  # === open_in_browser
590
771
  #
591
- # This method will open an URL in firefox.
772
+ # This method will open an URL via the browser.
592
773
  #
593
774
  # Usage example in pure Ruby:
594
775
  #
@@ -603,12 +784,13 @@ def open_in_browser(
603
784
  # ========================================================================= #
604
785
  # Next, open the class that we defined above in this file here.
605
786
  # ========================================================================= #
606
- _ = Open::InBrowser.new(url, false, &block)
787
+ _ = Open.in_browser(url, :do_not_run_yet, &block)
607
788
  _.check_for_intralink(optional_intralink)
608
789
  _.run
609
790
  end; alias oib open_in_browser # oib shortcut.
610
791
 
611
792
  if __FILE__ == $PROGRAM_NAME
612
- open_in_browser(ARGV.first)
793
+ Open.in_browser(ARGV)
613
794
  end # inbrowser
614
- # inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix
795
+ # inbrowser "https://distrowatch.com/dwres.php?resource=ratings&distro=antix"
796
+ # inbrowser /home/x/Temp/studium/html/timetable.html#today