open 0.1.23

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of open might be problematic. Click here for more details.

@@ -0,0 +1,572 @@
1
+ #!/usr/bin/ruby -w
2
+ # Encoding: UTF-8
3
+ # frozen_string_literal: true
4
+ # =========================================================================== #
5
+ # === Open::InBrowser
6
+ #
7
+ # This class can be used to open something in the browser.
8
+ #
9
+ # The correct browser to use is, by default, the one that is stored in
10
+ # the yaml file called 'use_this_browser.yml'. This can, evidently,
11
+ # only work if this file exists and can be found.
12
+ #
13
+ # If it is a local file then the absolute path will be used, determined
14
+ # via File.absolute_path().
15
+ #
16
+ # Usage example for this class:
17
+ #
18
+ # require 'open'
19
+ # Open::InBrowser.new(ARGV)
20
+ #
21
+ # =========================================================================== #
22
+ # To open a remote webpage in firefox only, do:
23
+ #
24
+ # use_this_browser = 'firefox -new-tab'
25
+ # _ = use_this_browser
26
+ # _ << ' "'+url.to_s+'"'
27
+ #
28
+ # =========================================================================== #
29
+ # require 'open/in_browser/in_browser.rb'
30
+ # =========================================================================== #
31
+ require 'open/base/base.rb'
32
+
33
+ module Open
34
+
35
+ class InBrowser < Base # === Open::InBrowser
36
+
37
+ begin
38
+ require 'studium/requires/return_remote_homepage_of_this_lecture.rb'
39
+ rescue LoadError; end
40
+
41
+ # ========================================================================= #
42
+ # === initialize
43
+ #
44
+ # The first argument becomes the target URL that will be opened.
45
+ # ========================================================================= #
46
+ def initialize(
47
+ target_url = '',
48
+ run_already = true
49
+ )
50
+ register_sigint
51
+ reset
52
+ set_target_url(
53
+ target_url
54
+ )
55
+ # ======================================================================= #
56
+ # === Handle blocks given next
57
+ # ======================================================================= #
58
+ if block_given?
59
+ yielded = yield
60
+ case yielded
61
+ # ===================================================================== #
62
+ # === :open_in_background
63
+ # ===================================================================== #
64
+ when :open_in_background
65
+ set_open_in_background(true)
66
+ # ===================================================================== #
67
+ # === use_launchy
68
+ #
69
+ # This clause can be entered by code such as the following:
70
+ #
71
+ # Open.open_in_browser(port: 8080,) { :use_launchy }
72
+ #
73
+ # ===================================================================== #
74
+ when :use_launchy
75
+ set_use_launchy(true)
76
+ # ===================================================================== #
77
+ # === be_silent
78
+ # ===================================================================== #
79
+ when :be_silent,
80
+ :be_quiet
81
+ @internal_hash[:show_the_sys_command_that_is_used] = false
82
+ else
83
+ # =================================================================== #
84
+ # Handle Hashes next:
85
+ # =================================================================== #
86
+ if yielded.is_a? Hash
87
+ # ================================================================= #
88
+ # === :use_this_browser
89
+ #
90
+ # Usage example for this clause:
91
+ #
92
+ # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
93
+ #
94
+ # ================================================================= #
95
+ if yielded.has_key? :use_this_browser
96
+ set_use_this_browser(yielded[:use_this_browser])
97
+ end
98
+ end
99
+ end
100
+ end
101
+ run if run_already
102
+ end
103
+
104
+ # ========================================================================= #
105
+ # === reset (reset tag)
106
+ # ========================================================================= #
107
+ def reset
108
+ super()
109
+ # ======================================================================= #
110
+ # === @internal_hash
111
+ # ======================================================================= #
112
+ @internal_hash = {}
113
+ # ======================================================================= #
114
+ # === :use_this_browser
115
+ #
116
+ # This variable will keep track as to which browser will be used.
117
+ # ======================================================================= #
118
+ @internal_hash[:use_this_browser] = ::Open.use_which_browser?
119
+ # ======================================================================= #
120
+ # === :open_in_background
121
+ # ======================================================================= #
122
+ @internal_hash[:open_in_background] = false
123
+ # ======================================================================= #
124
+ # === :use_launchy
125
+ #
126
+ # This variable will make use of the launchy gem rather than the
127
+ # class here. It has been added in February 2022 as an ad-hoc
128
+ # way, in particular for windows, to open remote URLs. That way
129
+ # we can query for the windows operating system and silently
130
+ # delegate onto the launchy gem, if need be. The default value
131
+ # is false, though, so most users of this class have to enable
132
+ # this on their own.
133
+ # ======================================================================= #
134
+ @internal_hash[:use_launchy] = false
135
+ # ======================================================================= #
136
+ # === :show_the_sys_command_that_is_used
137
+ # ======================================================================= #
138
+ @internal_hash[:show_the_sys_command_that_is_used] = true
139
+ # ======================================================================= #
140
+ # === :append_this_string
141
+ # ======================================================================= #
142
+ @internal_hash[:append_this_string] = ''.dup
143
+ end
144
+
145
+ # ========================================================================= #
146
+ # === set_open_in_background
147
+ # ========================================================================= #
148
+ def set_open_in_background(i = true)
149
+ @internal_hash[:open_in_background] = i
150
+ end
151
+
152
+ # ========================================================================= #
153
+ # === set_use_launchy
154
+ # ========================================================================= #
155
+ def set_use_launchy(i = false)
156
+ @internal_hash[:use_launchy] = i
157
+ end
158
+
159
+ # ========================================================================= #
160
+ # === show_the_sys_command_that_is_used?
161
+ # ========================================================================= #
162
+ def show_the_sys_command_that_is_used?
163
+ @internal_hash[:show_the_sys_command_that_is_used]
164
+ end
165
+
166
+ # ========================================================================= #
167
+ # === use_launchy?
168
+ # ========================================================================= #
169
+ def use_launchy?
170
+ @internal_hash[:use_launchy]
171
+ end
172
+
173
+ # ========================================================================= #
174
+ # === check_for_intralink
175
+ #
176
+ # The second optional argument specifies the token to use for replacing
177
+ # empty spaces, if we have passed an Array as argument.
178
+ # ========================================================================= #
179
+ def check_for_intralink(
180
+ i = nil,
181
+ use_this_token_to_replace_empty_spaces = '_'
182
+ )
183
+ if i
184
+ if i.is_a? Array
185
+ i = i.join(' ').tr(' ', use_this_token_to_replace_empty_spaces)
186
+ end
187
+ i = i.to_s # Ensure that we have a String at this point.
188
+ unless i.empty?
189
+ @internal_hash[:append_this_string] << i
190
+ end
191
+ end
192
+ end
193
+
194
+ # ========================================================================= #
195
+ # === set_target_url (URL tag)
196
+ #
197
+ # We prefer this to be an Array.
198
+ #
199
+ # Usage example:
200
+ #
201
+ # Open::InBrowser.new('https://blog.fefe.de/') { :use_launchy }
202
+ # Open::InBrowser.new('blog.fefe.de') { :use_launchy }
203
+ #
204
+ # ========================================================================= #
205
+ def set_target_url(
206
+ i = ''
207
+ )
208
+ if i.is_a? Array # This if-clause was added in May 2022.
209
+ i = i.join(' ').strip
210
+ end
211
+ possible_array = beautiful_url(i)
212
+ case i
213
+ # ======================================================================= #
214
+ # === rf HERE
215
+ # ======================================================================= #
216
+ when /HERE/
217
+ i = File.basename(return_pwd)
218
+ if is_on_roebe?
219
+ i = Studium.return_remote_homepage_of_this_lecture(i)
220
+ end
221
+ end
222
+ if possible_array.is_a?(Array) and (possible_array.size > 1)
223
+ i = possible_array
224
+ else
225
+ if i.is_a? Hash
226
+ # =================================================================== #
227
+ # === :use_launchy
228
+ # =================================================================== #
229
+ if i.has_key? :use_launchy
230
+ set_use_launchy(i.delete(:use_launchy))
231
+ end
232
+ # =================================================================== #
233
+ # === :with_localhost
234
+ #
235
+ # Example for this entry point:
236
+ #
237
+ # Open::InBrowser.new(with_localhost: PORT_TO_USE)
238
+ #
239
+ # =================================================================== #
240
+ if i.has_key? :with_localhost
241
+ i = "http://localhost:#{i.delete(:with_localhost)}/"
242
+ # =================================================================== #
243
+ # === :port
244
+ #
245
+ # Example for this entry point:
246
+ #
247
+ # Open::InBrowser[port: 8080]
248
+ #
249
+ # =================================================================== #
250
+ elsif i.has_key? :port
251
+ i = "http://localhost:#{i.delete(:port)}/"
252
+ end
253
+ end
254
+ end
255
+ i = [i] unless i.is_a? Array
256
+ i.flatten! if i.is_a? Array
257
+ # ======================================================================= #
258
+ # Work over all members of this Array next.
259
+ # ======================================================================= #
260
+ i.map! {|entry|
261
+ case entry
262
+ when :default_sinatra_port
263
+ entry = 'http://localhost:4567/'
264
+ end
265
+ if Object.const_defined? :Studium
266
+ possible_link = Studium.return_remote_homepage_of_this_lecture(entry)
267
+ if possible_link == entry
268
+ else
269
+ entry = possible_link
270
+ end
271
+ end
272
+ if ::BeautifulUrl.is_a_local_link?(entry)
273
+ entry = ::BeautifulUrl.local_menu(entry)
274
+ else
275
+ entry = beautiful_url(entry).dup
276
+ end
277
+ entry = entry.first if entry.is_a? Array
278
+ if File.exist?(entry)
279
+ entry = File.absolute_path(entry)
280
+ end
281
+ if entry.start_with?('/home/x/') and !entry.include?('images/')
282
+ entry.sub!(/\/home\/x\//,'http://localhost/')
283
+ end
284
+ # ===================================================================== #
285
+ # Perform some sanitize-operations next.
286
+ # ===================================================================== #
287
+ if entry and
288
+ ((entry.include?("'") and !entry.include?('"')) or
289
+ entry.include?(' ')
290
+ )
291
+ entry = '"'+entry+'"'
292
+ elsif entry.include?(';') or entry.include?('(')
293
+ entry = '"'+entry+'"'
294
+ end
295
+ unless File.exist?(entry)
296
+ # =================================================================== #
297
+ # Try a trailing glob next. The idea here is to expand from e. g.
298
+ # "image3" towards "image3.jpg", if the latter file exists, and
299
+ # the former does NOT exist.
300
+ # =================================================================== #
301
+ possible_matches = Dir["#{entry}*"]
302
+ unless possible_matches.empty?
303
+ first = possible_matches.first
304
+ if File.exist? first
305
+ entry = first
306
+ end
307
+ end
308
+ end
309
+ entry
310
+ }
311
+ @internal_hash[:target_url] = i
312
+ end
313
+
314
+ # ========================================================================= #
315
+ # === target_url?
316
+ # ========================================================================= #
317
+ def target_url?
318
+ @internal_hash[:target_url]
319
+ end; alias remote_URL? target_url? # === remote_URL?
320
+
321
+ # ========================================================================= #
322
+ # === first_remote_URL?
323
+ # ========================================================================= #
324
+ def first_remote_URL?
325
+ remote_URL?.first
326
+ end
327
+
328
+ # ========================================================================= #
329
+ # === set_use_this_browser
330
+ # ========================================================================= #
331
+ def set_use_this_browser(
332
+ i = ::Open.use_which_browser?
333
+ )
334
+ @internal_hash[:use_this_browser] = i.to_s
335
+ end
336
+
337
+ # ========================================================================= #
338
+ # === use_this_browser?
339
+ # ========================================================================= #
340
+ def use_this_browser?
341
+ @internal_hash[:use_this_browser]
342
+ end
343
+
344
+ # ========================================================================= #
345
+ # === open_in_background?
346
+ # ========================================================================= #
347
+ def open_in_background?
348
+ @internal_hash[:open_in_background]
349
+ end
350
+
351
+ # ========================================================================= #
352
+ # === append_this_string?
353
+ # ========================================================================= #
354
+ def append_this_string?
355
+ @internal_hash[:append_this_string]
356
+ end
357
+
358
+ # ========================================================================= #
359
+ # === open_in_browser
360
+ #
361
+ # This is the part that does the actual file-opening via the browser.
362
+ # ========================================================================= #
363
+ def open_in_browser(
364
+ optional_append_this_string = append_this_string?
365
+ )
366
+ _ = LOCATION_OF_BROWSER_YAML_FILE
367
+ browser = use_this_browser? # Use the generic browser by default.
368
+ # ======================================================================= #
369
+ # We must check first whether the file exists or not.
370
+ # ======================================================================= #
371
+ if File.exist? _
372
+ browser = YAML.load_file(_).strip.dup # This variable holds which browser to use.
373
+ if is_on_windows?
374
+ case browser
375
+ # =================================================================== #
376
+ # === palemoon
377
+ # =================================================================== #
378
+ when 'palemoon'
379
+ browser.prepend(
380
+ "C:\\Program Files\\Pale Moon\\palemoon.exe"
381
+ )
382
+ end
383
+ end
384
+ # ===================================================================== #
385
+ # Different browsers have different ways to open a new tab.
386
+ # ===================================================================== #
387
+ if browser.include? 'opera' # Perform some sanitizing in this case.
388
+ browser << ' -newtab'
389
+ # =================================================================== #
390
+ # Or alternatively:
391
+ #
392
+ # opera --remote 'openURL(<url>, new-page)'
393
+ #
394
+ # =================================================================== #
395
+ elsif browser.include? 'firefox'
396
+ browser << ' -new-tab'
397
+ elsif browser.include?('chromium') or
398
+ browser.include?('thorium') or
399
+ browser.include?('chrome')
400
+ # =================================================================== #
401
+ # This is for chromium-based browsers, including chrome.
402
+ # =================================================================== #
403
+ browser << ' --no-sandbox --app-url '
404
+ end
405
+ [target_url?].flatten.each {|entry|
406
+ entry = entry.to_s.dup # .dup to avoid that it may be frozen.
407
+ if optional_append_this_string.size > 0
408
+ # ================================================================= #
409
+ # We prepend the '#' character, unless it starts with the
410
+ # '?' character.
411
+ # ================================================================= #
412
+ unless optional_append_this_string.start_with? '?'
413
+ optional_append_this_string.prepend '#'
414
+ end
415
+ entry << optional_append_this_string
416
+ end
417
+ # =================================================================== #
418
+ # Next, specifically handle the situation on my home system, when
419
+ # we are in a directory that includes a substring such as
420
+ # '/home/x/data/' and if the target file name ends with '.cgi'.
421
+ # =================================================================== #
422
+ if is_on_roebe? and
423
+ entry.end_with?('.cgi') and
424
+ !entry.start_with?('http') and
425
+ !entry.start_with?('http://localhost/')
426
+ if return_pwd.include?(DATA_DIRECTORY_AT_HOME)
427
+ # ================================================================= #
428
+ # We need a target URL such as:
429
+ # http://localhost/data/games/AITD/AITD.cgi
430
+ # in this case.
431
+ # ================================================================= #
432
+ unless entry.include? DATA_DIRECTORY_AT_HOME
433
+ entry.prepend(return_pwd)
434
+ end
435
+ entry.squeeze!('/')
436
+ quoted_regexp = Regexp.quote(DATA_DIRECTORY_AT_HOME)
437
+ entry.sub!(quoted_regexp, 'http://localhost/data/')
438
+ elsif return_pwd.include?(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
439
+ unless entry.include? PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME
440
+ entry.prepend(return_pwd)
441
+ end
442
+ entry.squeeze!('/')
443
+ quoted_regexp = Regexp.quote(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
444
+ entry.sub!(quoted_regexp, 'http://localhost/programming/')
445
+ end
446
+ end
447
+ if open_in_background?
448
+ entry << '&'
449
+ end
450
+ if entry.include? '&'
451
+ entry = '"'+entry+'"' unless entry.start_with? '"'
452
+ end
453
+ # =================================================================== #
454
+ # Handle windows next:
455
+ # =================================================================== #
456
+ if is_on_windows?
457
+ # start firefox -new-tab www.howtogeek.com
458
+ this_command = "start #{browser} -new-tab #{entry}"
459
+ else
460
+ this_command = "#{browser} #{entry}"
461
+ end
462
+ if show_the_sys_command_that_is_used?
463
+ e this_command
464
+ end
465
+ unless entry.empty?
466
+ # ================================================================= #
467
+ # Finally run the system-command, unless we will use the
468
+ # launchy gem.
469
+ # ================================================================= #
470
+ if use_launchy?
471
+ cmd_to_use = 'launchy '+entry.to_s
472
+ e cmd_to_use
473
+ system(cmd_to_use)
474
+ else
475
+ system(this_command) # Here we always output.
476
+ end
477
+ end
478
+ } unless target_url?.nil?
479
+ else
480
+ browser = 'firefox' # Else, default to this here as a generic fallback.
481
+ end
482
+ end
483
+
484
+ # ========================================================================= #
485
+ # === run (run tag)
486
+ # ========================================================================= #
487
+ def run
488
+ open_in_browser
489
+ end
490
+
491
+ # ========================================================================= #
492
+ # === Open::InBrowser[]
493
+ # ========================================================================= #
494
+ def self.[](i = ARGV)
495
+ new(i)
496
+ end; self.instance_eval { alias open_in_browser [] } # === Open::InBrowser.open_in_browser
497
+
498
+ end
499
+
500
+ # ========================================================================= #
501
+ # === Open.open_in_browser
502
+ #
503
+ # Note that this method needs to be somewhat flexible, as we can provide
504
+ # varied input such as:
505
+ #
506
+ # Open.open_in_browser(port: 8080)
507
+ # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
508
+ #
509
+ # ========================================================================= #
510
+ def self.open_in_browser(
511
+ i = ARGV, &block
512
+ )
513
+ # ======================================================================= #
514
+ # === Handle blocks next
515
+ # ======================================================================= #
516
+ if block_given?
517
+ yielded = yield
518
+ # ===================================================================== #
519
+ # === Handle Hashes next
520
+ # ===================================================================== #
521
+ if yielded.is_a? Hash
522
+ # =================================================================== #
523
+ # === Handle Hashes past this point
524
+ # =================================================================== #
525
+ # =================================================================== #
526
+ # === :delay
527
+ # =================================================================== #
528
+ if yielded.has_key? :delay
529
+ _ = yielded.delete(:delay)
530
+ if _.is_a? String
531
+ _ = _.sub(/ seconds/,'')
532
+ _ = _.sub(/ second/,'').to_f if _.include? ' second'
533
+ _ = _.to_f # Need a Float.
534
+ end
535
+ sleep(_)
536
+ end
537
+ end
538
+ # else
539
+ end
540
+ ::Open::InBrowser.new(i, &block)
541
+ end; self.instance_eval { alias in_browser open_in_browser } # === Open.in_browser
542
+ self.instance_eval { alias open open_in_browser } # === Open.open
543
+
544
+ end
545
+
546
+ # =========================================================================== #
547
+ # === open_in_browser
548
+ #
549
+ # This method will open an URL in firefox.
550
+ #
551
+ # Usage example in pure Ruby:
552
+ #
553
+ # require 'open'; open_in_browser 'google.at'
554
+ #
555
+ # =========================================================================== #
556
+ def open_in_browser(
557
+ url = ARGV,
558
+ optional_intralink = nil,
559
+ &block
560
+ )
561
+ # ========================================================================= #
562
+ # Next, open the class that we defined above in this file here.
563
+ # ========================================================================= #
564
+ _ = Open::InBrowser.new(url, false, &block)
565
+ _.check_for_intralink(optional_intralink)
566
+ _.run
567
+ end; alias oib open_in_browser # oib shortcut.
568
+
569
+ if __FILE__ == $PROGRAM_NAME
570
+ open_in_browser(ARGV.first)
571
+ end # inbrowser
572
+ # inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix