open 0.1.30

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,614 @@
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
+ # === :no_localhost
63
+ #
64
+ # This entry point ensures that there will not be a "localhost"
65
+ # part of the opened URL.
66
+ # ===================================================================== #
67
+ when :no_localhost
68
+ @internal_hash[:use_localhost_URLs] = false
69
+ # ===================================================================== #
70
+ # === :try_to_use_localhost_URLs
71
+ # ===================================================================== #
72
+ when :try_to_use_localhost_URLs
73
+ @internal_hash[:use_localhost_URLs] = true
74
+ # ===================================================================== #
75
+ # === :open_in_background
76
+ # ===================================================================== #
77
+ when :open_in_background
78
+ set_open_in_background(true)
79
+ # ===================================================================== #
80
+ # === use_launchy
81
+ #
82
+ # This clause can be entered by code such as the following:
83
+ #
84
+ # Open.open_in_browser(port: 8080,) { :use_launchy }
85
+ #
86
+ # ===================================================================== #
87
+ when :use_launchy
88
+ 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
+ else
99
+ # =================================================================== #
100
+ # Handle Hashes next:
101
+ # =================================================================== #
102
+ if yielded.is_a? Hash
103
+ # ================================================================= #
104
+ # === :use_this_browser
105
+ #
106
+ # Usage example for this clause:
107
+ #
108
+ # Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
109
+ #
110
+ # ================================================================= #
111
+ if yielded.has_key? :use_this_browser
112
+ set_use_this_browser(yielded[:use_this_browser])
113
+ end
114
+ end
115
+ end
116
+ end
117
+ run if run_already
118
+ end
119
+
120
+ # ========================================================================= #
121
+ # === reset (reset tag)
122
+ # ========================================================================= #
123
+ def reset
124
+ super()
125
+ # ======================================================================= #
126
+ # === @internal_hash
127
+ # ======================================================================= #
128
+ @internal_hash = {}
129
+ # ======================================================================= #
130
+ # === :use_this_browser
131
+ #
132
+ # This variable will keep track as to which browser will be used.
133
+ # ======================================================================= #
134
+ @internal_hash[:use_this_browser] = ::Open.use_which_browser?
135
+ # ======================================================================= #
136
+ # === :open_in_background
137
+ # ======================================================================= #
138
+ @internal_hash[:open_in_background] = false
139
+ # ======================================================================= #
140
+ # === :use_launchy
141
+ #
142
+ # This variable will make use of the launchy gem rather than the
143
+ # class here. It has been added in February 2022 as an ad-hoc
144
+ # way, in particular for windows, to open remote URLs. That way
145
+ # we can query for the windows operating system and silently
146
+ # 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.
149
+ # ======================================================================= #
150
+ @internal_hash[:use_launchy] = false
151
+ # ======================================================================= #
152
+ # === :show_the_sys_command_that_is_used
153
+ # ======================================================================= #
154
+ @internal_hash[:show_the_sys_command_that_is_used] = true
155
+ # ======================================================================= #
156
+ # === :append_this_string
157
+ # ======================================================================= #
158
+ @internal_hash[:append_this_string] = ''.dup
159
+ # ======================================================================= #
160
+ # === :use_localhost_URLs
161
+ #
162
+ # By default we will not use localhost-URLs. If we are on a roebe-system,
163
+ # however had, we will use localhost URLs.
164
+ # ======================================================================= #
165
+ @internal_hash[:use_localhost_URLs] = false
166
+ end
167
+
168
+ # ========================================================================= #
169
+ # === set_open_in_background
170
+ # ========================================================================= #
171
+ def set_open_in_background(i = true)
172
+ @internal_hash[:open_in_background] = i
173
+ end
174
+
175
+ # ========================================================================= #
176
+ # === set_use_launchy
177
+ # ========================================================================= #
178
+ def set_use_launchy(i = false)
179
+ @internal_hash[:use_launchy] = i
180
+ end
181
+
182
+ # ========================================================================= #
183
+ # === show_the_sys_command_that_is_used?
184
+ # ========================================================================= #
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
195
+
196
+ # ========================================================================= #
197
+ # === check_for_intralink
198
+ #
199
+ # The second optional argument specifies the token to use for replacing
200
+ # empty spaces, if we have passed an Array as argument.
201
+ # ========================================================================= #
202
+ def check_for_intralink(
203
+ i = nil,
204
+ use_this_token_to_replace_empty_spaces = '_'
205
+ )
206
+ if i
207
+ if i.is_a? Array
208
+ i = i.join(' ').tr(' ', use_this_token_to_replace_empty_spaces)
209
+ end
210
+ i = i.to_s # Ensure that we have a String at this point.
211
+ unless i.empty?
212
+ @internal_hash[:append_this_string] << i
213
+ end
214
+ end
215
+ end
216
+
217
+ # ========================================================================= #
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
+ #
227
+ # ========================================================================= #
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
335
+ end
336
+
337
+ # ========================================================================= #
338
+ # === target_url?
339
+ # ========================================================================= #
340
+ 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?
344
+
345
+ # ========================================================================= #
346
+ # === first_remote_URL?
347
+ # ========================================================================= #
348
+ def first_remote_URL?
349
+ remote_URL?.first
350
+ end
351
+
352
+ # ========================================================================= #
353
+ # === set_use_this_browser
354
+ # ========================================================================= #
355
+ def set_use_this_browser(
356
+ i = ::Open.use_which_browser?
357
+ )
358
+ @internal_hash[:use_this_browser] = i.to_s
359
+ end
360
+
361
+ # ========================================================================= #
362
+ # === use_this_browser?
363
+ # ========================================================================= #
364
+ def use_this_browser?
365
+ @internal_hash[:use_this_browser]
366
+ end
367
+
368
+ # ========================================================================= #
369
+ # === open_in_background?
370
+ # ========================================================================= #
371
+ def open_in_background?
372
+ @internal_hash[:open_in_background]
373
+ end
374
+
375
+ # ========================================================================= #
376
+ # === append_this_string?
377
+ # ========================================================================= #
378
+ def append_this_string?
379
+ @internal_hash[:append_this_string]
380
+ end
381
+
382
+ # ========================================================================= #
383
+ # === use_localhost_URLs?
384
+ # ========================================================================= #
385
+ def use_localhost_URLs?
386
+ @internal_hash[:use_localhost_URLs]
387
+ end
388
+
389
+ # ========================================================================= #
390
+ # === open_in_browser
391
+ #
392
+ # This is the part that does the actual file-opening via the browser.
393
+ # ========================================================================= #
394
+ def open_in_browser(
395
+ optional_append_this_string = append_this_string?
396
+ )
397
+ _ = LOCATION_OF_BROWSER_YAML_FILE
398
+ browser = use_this_browser? # Use the generic browser by default.
399
+ # ======================================================================= #
400
+ # We must check first whether the file exists or not.
401
+ # ======================================================================= #
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
+ )
413
+ end
414
+ end
415
+ # ===================================================================== #
416
+ # Different browsers have different ways to open a new tab.
417
+ # ===================================================================== #
418
+ if browser.include? 'opera' # Perform some sanitizing in this case.
419
+ browser << ' -newtab'
420
+ # =================================================================== #
421
+ # Or alternatively:
422
+ #
423
+ # opera --remote 'openURL(<url>, new-page)'
424
+ #
425
+ # =================================================================== #
426
+ elsif browser.include? 'firefox'
427
+ browser << ' -new-tab'
428
+ elsif browser.include?('chromium') or
429
+ browser.include?('thorium') or
430
+ browser.include?('chrome')
431
+ # =================================================================== #
432
+ # This is for chromium-based browsers, including chrome.
433
+ # =================================================================== #
434
+ browser << ' --no-sandbox --app-url '
435
+ 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)]
441
+ 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 '#'
449
+ end
450
+ entry << optional_append_this_string
451
+ end
452
+ # =================================================================== #
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'.
456
+ # =================================================================== #
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
481
+ end
482
+ 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://')
490
+ end
491
+ if entry.include? '&'
492
+ entry = '"'+entry+'"' unless entry.start_with? '"'
493
+ end
494
+ # =================================================================== #
495
+ # Handle windows next:
496
+ # =================================================================== #
497
+ 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
+ # ================================================================= #
508
+ # Finally run the system-command, unless we will use the
509
+ # launchy gem.
510
+ # ================================================================= #
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
518
+ end
519
+ } unless target_url?.nil?
520
+ else
521
+ browser = 'firefox' # Else, default to this here as a generic fallback.
522
+ end
523
+ end
524
+
525
+ # ========================================================================= #
526
+ # === run (run tag)
527
+ # ========================================================================= #
528
+ def run
529
+ open_in_browser
530
+ end
531
+
532
+ # ========================================================================= #
533
+ # === Open::InBrowser[]
534
+ # ========================================================================= #
535
+ def self.[](i = ARGV)
536
+ new(i)
537
+ end; self.instance_eval { alias open_in_browser [] } # === Open::InBrowser.open_in_browser
538
+
539
+ end
540
+
541
+ # ========================================================================= #
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
+ #
550
+ # ========================================================================= #
551
+ def self.open_in_browser(
552
+ i = ARGV,
553
+ &block
554
+ )
555
+ # ======================================================================= #
556
+ # === Handle blocks next
557
+ # ======================================================================= #
558
+ if block_given?
559
+ yielded = yield
560
+ # ===================================================================== #
561
+ # === Handle Hashes next
562
+ # ===================================================================== #
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(_)
578
+ end
579
+ end
580
+ # else
581
+ 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
585
+
586
+ end
587
+
588
+ # =========================================================================== #
589
+ # === open_in_browser
590
+ #
591
+ # This method will open an URL in firefox.
592
+ #
593
+ # Usage example in pure Ruby:
594
+ #
595
+ # require 'open'; open_in_browser 'google.at'
596
+ #
597
+ # =========================================================================== #
598
+ def open_in_browser(
599
+ url = ARGV,
600
+ optional_intralink = nil,
601
+ &block
602
+ )
603
+ # ========================================================================= #
604
+ # Next, open the class that we defined above in this file here.
605
+ # ========================================================================= #
606
+ _ = Open::InBrowser.new(url, false, &block)
607
+ _.check_for_intralink(optional_intralink)
608
+ _.run
609
+ end; alias oib open_in_browser # oib shortcut.
610
+
611
+ if __FILE__ == $PROGRAM_NAME
612
+ open_in_browser(ARGV.first)
613
+ end # inbrowser
614
+ # inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix