open 0.1.22

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,567 @@
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 browser.include?('thorium')
398
+ browser << ' --no-sandbox --app-url '
399
+ end
400
+ [target_url?].flatten.each {|entry|
401
+ entry = entry.to_s.dup # .dup to avoid that it may be frozen.
402
+ if optional_append_this_string.size > 0
403
+ # ================================================================= #
404
+ # We prepend the '#' character, unless it starts with the
405
+ # '?' character.
406
+ # ================================================================= #
407
+ unless optional_append_this_string.start_with? '?'
408
+ optional_append_this_string.prepend '#'
409
+ end
410
+ entry << optional_append_this_string
411
+ end
412
+ # =================================================================== #
413
+ # Next, specifically handle the situation on my home system, when
414
+ # we are in a directory that includes a substring such as
415
+ # '/home/x/data/' and if the target file name ends with '.cgi'.
416
+ # =================================================================== #
417
+ if is_on_roebe? and
418
+ entry.end_with?('.cgi') and
419
+ !entry.start_with?('http') and
420
+ !entry.start_with?('http://localhost/')
421
+ if return_pwd.include?(DATA_DIRECTORY_AT_HOME)
422
+ # ================================================================= #
423
+ # We need a target URL such as:
424
+ # http://localhost/data/games/AITD/AITD.cgi
425
+ # in this case.
426
+ # ================================================================= #
427
+ unless entry.include? DATA_DIRECTORY_AT_HOME
428
+ entry.prepend(return_pwd)
429
+ end
430
+ entry.squeeze!('/')
431
+ quoted_regexp = Regexp.quote(DATA_DIRECTORY_AT_HOME)
432
+ entry.sub!(quoted_regexp, 'http://localhost/data/')
433
+ elsif return_pwd.include?(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
434
+ unless entry.include? PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME
435
+ entry.prepend(return_pwd)
436
+ end
437
+ entry.squeeze!('/')
438
+ quoted_regexp = Regexp.quote(PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME)
439
+ entry.sub!(quoted_regexp, 'http://localhost/programming/')
440
+ end
441
+ end
442
+ if open_in_background?
443
+ entry << '&'
444
+ end
445
+ if entry.include? '&'
446
+ entry = '"'+entry+'"' unless entry.start_with? '"'
447
+ end
448
+ # =================================================================== #
449
+ # Handle windows next:
450
+ # =================================================================== #
451
+ if is_on_windows?
452
+ # start firefox -new-tab www.howtogeek.com
453
+ this_command = "start #{browser} -new-tab #{entry}"
454
+ else
455
+ this_command = "#{browser} #{entry}"
456
+ end
457
+ if show_the_sys_command_that_is_used?
458
+ e this_command
459
+ end
460
+ unless entry.empty?
461
+ # ================================================================= #
462
+ # Finally run the system-command, unless we will use the
463
+ # launchy gem.
464
+ # ================================================================= #
465
+ if use_launchy?
466
+ cmd_to_use = 'launchy '+entry.to_s
467
+ e cmd_to_use
468
+ system(cmd_to_use)
469
+ else
470
+ system(this_command) # Here we always output.
471
+ end
472
+ end
473
+ } unless target_url?.nil?
474
+ else
475
+ browser = 'firefox' # Else, default to this here as a generic fallback.
476
+ end
477
+ end
478
+
479
+ # ========================================================================= #
480
+ # === run (run tag)
481
+ # ========================================================================= #
482
+ def run
483
+ open_in_browser
484
+ end
485
+
486
+ # ========================================================================= #
487
+ # === Open::InBrowser[]
488
+ # ========================================================================= #
489
+ def self.[](i = ARGV)
490
+ new(i)
491
+ end; self.instance_eval { alias open_in_browser [] } # === Open::InBrowser.open_in_browser
492
+
493
+ end
494
+
495
+ # ========================================================================= #
496
+ # === Open.open_in_browser
497
+ #
498
+ # Note that this method needs to be somewhat flexible, as we can provide
499
+ # varied input such as:
500
+ #
501
+ # Open.open_in_browser(port: 8080)
502
+ # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
503
+ #
504
+ # ========================================================================= #
505
+ def self.open_in_browser(
506
+ i = ARGV, &block
507
+ )
508
+ # ======================================================================= #
509
+ # === Handle blocks next
510
+ # ======================================================================= #
511
+ if block_given?
512
+ yielded = yield
513
+ # ===================================================================== #
514
+ # === Handle Hashes next
515
+ # ===================================================================== #
516
+ if yielded.is_a? Hash
517
+ # =================================================================== #
518
+ # === Handle Hashes past this point
519
+ # =================================================================== #
520
+ # =================================================================== #
521
+ # === :delay
522
+ # =================================================================== #
523
+ if yielded.has_key? :delay
524
+ _ = yielded.delete(:delay)
525
+ if _.is_a? String
526
+ _ = _.sub(/ seconds/,'')
527
+ _ = _.sub(/ second/,'').to_f if _.include? ' second'
528
+ _ = _.to_f # Need a Float.
529
+ end
530
+ sleep(_)
531
+ end
532
+ end
533
+ # else
534
+ end
535
+ ::Open::InBrowser.new(i, &block)
536
+ end; self.instance_eval { alias in_browser open_in_browser } # === Open.in_browser
537
+ self.instance_eval { alias open open_in_browser } # === Open.open
538
+
539
+ end
540
+
541
+ # =========================================================================== #
542
+ # === open_in_browser
543
+ #
544
+ # This method will open an URL in firefox.
545
+ #
546
+ # Usage example in pure Ruby:
547
+ #
548
+ # require 'open'; open_in_browser 'google.at'
549
+ #
550
+ # =========================================================================== #
551
+ def open_in_browser(
552
+ url = ARGV,
553
+ optional_intralink = nil,
554
+ &block
555
+ )
556
+ # ========================================================================= #
557
+ # Next, open the class that we defined above in this file here.
558
+ # ========================================================================= #
559
+ _ = Open::InBrowser.new(url, false, &block)
560
+ _.check_for_intralink(optional_intralink)
561
+ _.run
562
+ end; alias oib open_in_browser # oib shortcut.
563
+
564
+ if __FILE__ == $PROGRAM_NAME
565
+ open_in_browser(ARGV.first)
566
+ end # inbrowser
567
+ # inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix