open 0.1.30 → 0.2.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of open might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +73 -9
- data/bin/open +1 -1
- data/bin/open_in_browser +1 -1
- data/doc/README.gen +72 -8
- data/doc/deprecated_code/deprecated_code.md +26 -0
- data/img/open_logo.png +0 -0
- data/lib/open/base/base.rb +262 -122
- data/lib/open/books/README.md +8 -0
- data/lib/open/books/books.rb +99 -0
- data/lib/open/constants/constants.rb +85 -8
- data/lib/open/in_browser/in_browser.rb +476 -304
- data/lib/open/in_editor/in_editor.rb +348 -155
- data/lib/open/last/last.rb +14 -12
- data/lib/open/nano_open/nano_open.rb +4 -3
- data/lib/open/{open.rb → open/open.rb} +597 -653
- data/lib/open/project/project.rb +3 -2
- data/lib/open/requires/failsafe_require_of_beautiful_url.rb +9 -0
- data/lib/open/requires/require_the_project.rb +2 -2
- data/lib/open/requires/require_yaml.rb +13 -0
- data/lib/open/these_files/these_files.rb +1 -1
- data/lib/open/toplevel_methods/browser.rb +71 -0
- data/lib/open/toplevel_methods/delay.rb +23 -0
- data/lib/open/toplevel_methods/e.rb +14 -0
- data/lib/open/toplevel_methods/editor.rb +41 -0
- data/lib/open/toplevel_methods/host_os.rb +25 -0
- data/lib/open/toplevel_methods/is_on_roebe.rb +16 -0
- data/lib/open/toplevel_methods/is_on_windows.rb +26 -0
- data/lib/open/toplevel_methods/misc.rb +50 -0
- data/lib/open/version/version.rb +2 -2
- data/lib/open/with_delay/with_delay.rb +9 -11
- data/open.gemspec +1 -1
- data/test/testing_open.rb +1 -1
- data/test/testing_open_in_browser.rb +16 -0
- data/test/testing_open_via_launchy.rb +3 -0
- data/test/testing_shortcuts.rb +3 -0
- metadata +24 -8
- 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,
|
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
|
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
|
-
|
48
|
-
run_already
|
61
|
+
commandline_arguments = ARGV,
|
62
|
+
run_already = true,
|
63
|
+
&block
|
49
64
|
)
|
50
65
|
register_sigint
|
51
66
|
reset
|
52
|
-
|
53
|
-
|
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
|
-
|
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
|
-
|
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.
|
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] =
|
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
|
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
|
-
# === :
|
186
|
+
# === :work_on_these_URLs
|
187
|
+
#
|
188
|
+
# The following variable must be kept as Array at all times.
|
153
189
|
# ======================================================================= #
|
154
|
-
@internal_hash[:
|
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
|
-
# === :
|
196
|
+
# === :use_localhost
|
161
197
|
#
|
162
|
-
#
|
163
|
-
#
|
198
|
+
# If this variable is true then we will try to use 'localhost'
|
199
|
+
# entries, at the least on roebe-system.
|
200
|
+
#
|
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[:
|
205
|
+
@internal_hash[:use_localhost] = true
|
166
206
|
end
|
167
207
|
|
168
208
|
# ========================================================================= #
|
169
|
-
# ===
|
209
|
+
# === do_use_localhost
|
170
210
|
# ========================================================================= #
|
171
|
-
def
|
172
|
-
@internal_hash[:
|
211
|
+
def do_use_localhost
|
212
|
+
@internal_hash[:use_localhost] = true
|
173
213
|
end
|
174
214
|
|
175
215
|
# ========================================================================= #
|
176
|
-
# ===
|
216
|
+
# === do_not_use_localhost
|
177
217
|
# ========================================================================= #
|
178
|
-
def
|
179
|
-
@internal_hash[:
|
218
|
+
def do_not_use_localhost
|
219
|
+
@internal_hash[:use_localhost] = false
|
180
220
|
end
|
181
221
|
|
182
222
|
# ========================================================================= #
|
183
|
-
# ===
|
223
|
+
# === use_localhost?
|
184
224
|
# ========================================================================= #
|
185
|
-
def
|
186
|
-
@internal_hash[:
|
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,43 +247,56 @@ class InBrowser < Base # === Open::InBrowser
|
|
215
247
|
end
|
216
248
|
|
217
249
|
# ========================================================================= #
|
218
|
-
# ===
|
219
|
-
#
|
220
|
-
|
250
|
+
# === append_this_string?
|
251
|
+
# ========================================================================= #
|
252
|
+
def append_this_string?
|
253
|
+
@internal_hash[:append_this_string]
|
254
|
+
end
|
255
|
+
|
256
|
+
# ========================================================================= #
|
257
|
+
# === target_url?
|
258
|
+
# ========================================================================= #
|
259
|
+
def target_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
|
285
|
+
|
286
|
+
# ========================================================================= #
|
287
|
+
# === open_this_in_the_browser
|
221
288
|
#
|
222
289
|
# Usage example:
|
223
290
|
#
|
224
|
-
# Open
|
225
|
-
# Open
|
291
|
+
# Open.in_browser('https://blog.fefe.de/') { :use_launchy }
|
292
|
+
# Open.in_browser('blog.fefe.de') { :use_launchy }
|
226
293
|
#
|
227
294
|
# ========================================================================= #
|
228
|
-
def
|
229
|
-
|
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
|
295
|
+
def open_this_in_the_browser(i)
|
296
|
+
if i.is_a? Array
|
297
|
+
i.each {|entry| open_this_in_the_browser(entry) }
|
247
298
|
else
|
248
299
|
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
300
|
# =================================================================== #
|
256
301
|
# === :with_localhost
|
257
302
|
#
|
@@ -274,79 +319,151 @@ class InBrowser < Base # === Open::InBrowser
|
|
274
319
|
i = "http://localhost:#{i.delete(:port)}/"
|
275
320
|
end
|
276
321
|
end
|
277
|
-
|
278
|
-
|
279
|
-
|
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/'
|
322
|
+
i = i.dup if i.frozen?
|
323
|
+
if i and i.include?('→')
|
324
|
+
i.delete!('→')
|
325
|
+
i.strip!
|
287
326
|
end
|
288
|
-
if
|
289
|
-
|
290
|
-
|
327
|
+
if i.end_with?('.cgi')
|
328
|
+
if is_on_roebe? and
|
329
|
+
Dir.pwd.include?('/home/x/') and
|
330
|
+
!i.include?('/')
|
331
|
+
i = File.absolute_path(i)
|
332
|
+
end
|
333
|
+
i.delete_prefix!(HOME_DIRECTORY_OF_USER_X)
|
334
|
+
if use_localhost?
|
335
|
+
i.prepend('http://localhost/') unless i.start_with?('http://localhost/')
|
291
336
|
else
|
292
|
-
|
337
|
+
if beautiful_url_is_available? and is_on_roebe?
|
338
|
+
i.prepend(HOME_DIRECTORY_OF_USER_X) unless i.start_with?(HOME_DIRECTORY_OF_USER_X)
|
339
|
+
end
|
293
340
|
end
|
294
341
|
end
|
295
|
-
|
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)
|
342
|
+
unless i.empty?
|
319
343
|
# =================================================================== #
|
320
|
-
#
|
321
|
-
# "image3" towards "image3.jpg", if the latter file exists, and
|
322
|
-
# the former does NOT exist.
|
344
|
+
# Perform some sanitize-operations next.
|
323
345
|
# =================================================================== #
|
324
|
-
|
325
|
-
|
326
|
-
first = possible_matches.first
|
327
|
-
if File.exist? first
|
328
|
-
entry = first
|
329
|
-
end
|
346
|
+
if ARRAY_TOKENS_THAT_REQUIRE_PADDING.any? {|this_token| i.include?(this_token) }
|
347
|
+
i = '"'+i+'"' unless i.start_with?('"')
|
330
348
|
end
|
349
|
+
cmd_to_use = "#{use_this_browser?}".dup
|
350
|
+
# =================================================================== #
|
351
|
+
# Next, we must determine which options to pass to the browser at
|
352
|
+
# hand. Each browser may be different - for instance, firefox
|
353
|
+
# responds to a different set of commandline flags compared to
|
354
|
+
# chrome, and so forth.
|
355
|
+
# =================================================================== #
|
356
|
+
case cmd_to_use
|
357
|
+
# =================================================================== #
|
358
|
+
# This is for chromium-based browsers, including thorium and chrome.
|
359
|
+
# =================================================================== #
|
360
|
+
when 'chromium',
|
361
|
+
/thorium$/i,
|
362
|
+
/chrome$/i
|
363
|
+
cmd_to_use << ' --no-sandbox --app-url'
|
364
|
+
# =================================================================== #
|
365
|
+
# Handle firefox next:
|
366
|
+
# =================================================================== #
|
367
|
+
when 'firefox'
|
368
|
+
cmd_to_use << ' -new-tab'
|
369
|
+
# =================================================================== #
|
370
|
+
# Handle opera next:
|
371
|
+
# =================================================================== #
|
372
|
+
when 'opera' # Perform some sanitizing in this case.
|
373
|
+
cmd_to_use << ' -newtab'
|
374
|
+
# ================================================================= #
|
375
|
+
# Or an alternative to the above would be:
|
376
|
+
#
|
377
|
+
# opera --remote 'openURL(<url>, new-page)'
|
378
|
+
#
|
379
|
+
# I am not sure which variant to prefer, so I kind of chose one
|
380
|
+
# randomly for now.
|
381
|
+
# ================================================================= #
|
382
|
+
end
|
383
|
+
cmd_to_use << " #{i}"
|
384
|
+
# =================================================================== #
|
385
|
+
# Finally pass it into the designated browser.
|
386
|
+
# =================================================================== #
|
387
|
+
if open_in_background?
|
388
|
+
cmd_to_use << ' &'
|
389
|
+
end
|
390
|
+
# =================================================================== #
|
391
|
+
# Check for the launchy gem next.
|
392
|
+
# =================================================================== #
|
393
|
+
if use_launchy?
|
394
|
+
cmd_to_use.prepend('launchy ')
|
395
|
+
end
|
396
|
+
# =================================================================== #
|
397
|
+
# Handle windows next:
|
398
|
+
# =================================================================== #
|
399
|
+
if is_on_windows?
|
400
|
+
# ================================================================= #
|
401
|
+
# Usage example for windows:
|
402
|
+
#
|
403
|
+
# start firefox -new-tab www.howtogeek.com
|
404
|
+
#
|
405
|
+
# ================================================================= #
|
406
|
+
cmd_to_use = cmd_to_use.dup if cmd_to_use.frozen?
|
407
|
+
cmd_to_use.prepend('start ')
|
408
|
+
end
|
409
|
+
if show_the_sys_command_that_is_used?
|
410
|
+
e cmd_to_use
|
411
|
+
end
|
412
|
+
system "#{cmd_to_use}"
|
331
413
|
end
|
332
|
-
|
333
|
-
|
334
|
-
|
414
|
+
end
|
415
|
+
end; alias open_in_browser open_this_in_the_browser # === open_in_browser
|
416
|
+
|
417
|
+
# ========================================================================= #
|
418
|
+
# === first_remote_URL?
|
419
|
+
# ========================================================================= #
|
420
|
+
def first_remote_URL?
|
421
|
+
remote_URL?.first
|
335
422
|
end
|
336
423
|
|
337
424
|
# ========================================================================= #
|
338
|
-
# ===
|
425
|
+
# === open_in_background?
|
339
426
|
# ========================================================================= #
|
340
|
-
def
|
341
|
-
@internal_hash[:
|
342
|
-
end
|
343
|
-
alias remote_URL? target_url? # === remote_URL?
|
427
|
+
def open_in_background?
|
428
|
+
@internal_hash[:open_in_background]
|
429
|
+
end
|
344
430
|
|
345
431
|
# ========================================================================= #
|
346
|
-
# ===
|
432
|
+
# === set_open_in_background
|
347
433
|
# ========================================================================= #
|
348
|
-
def
|
349
|
-
|
434
|
+
def set_open_in_background(
|
435
|
+
i = true
|
436
|
+
)
|
437
|
+
@internal_hash[:open_in_background] = i
|
438
|
+
end
|
439
|
+
|
440
|
+
# ========================================================================= #
|
441
|
+
# === show_the_sys_command_that_is_used?
|
442
|
+
# ========================================================================= #
|
443
|
+
def show_the_sys_command_that_is_used?
|
444
|
+
@internal_hash[:show_the_sys_command_that_is_used]
|
445
|
+
end
|
446
|
+
|
447
|
+
# ========================================================================= #
|
448
|
+
# === determine_which_browser_to_use_then_return_the_result
|
449
|
+
# ========================================================================= #
|
450
|
+
def determine_which_browser_to_use_then_return_the_result
|
451
|
+
result = ::Open.use_which_browser? # Use the generic browser by default here.
|
452
|
+
if result.nil?
|
453
|
+
result = file_load_default_browser?
|
454
|
+
end
|
455
|
+
if is_on_windows?
|
456
|
+
case result
|
457
|
+
# ===================================================================== #
|
458
|
+
# === palemoon
|
459
|
+
# ===================================================================== #
|
460
|
+
when 'palemoon'
|
461
|
+
result.prepend(
|
462
|
+
"C:\\Program Files\\Pale Moon\\palemoon.exe"
|
463
|
+
)
|
464
|
+
end
|
465
|
+
end
|
466
|
+
return result
|
350
467
|
end
|
351
468
|
|
352
469
|
# ========================================================================= #
|
@@ -366,167 +483,203 @@ class InBrowser < Base # === Open::InBrowser
|
|
366
483
|
end
|
367
484
|
|
368
485
|
# ========================================================================= #
|
369
|
-
# ===
|
486
|
+
# === menu (menu tag)
|
370
487
|
# ========================================================================= #
|
371
|
-
def
|
372
|
-
|
488
|
+
def menu(i)
|
489
|
+
if i.is_a? Array
|
490
|
+
i.each {|entry| menu(entry) }
|
491
|
+
else
|
492
|
+
case i
|
493
|
+
# ===================================================================== #
|
494
|
+
# === rf --do-use-localhost
|
495
|
+
# ===================================================================== #
|
496
|
+
when /^-?-?do(-|_)?use(-|_)?localhost$/i,
|
497
|
+
/^-?-?localhost$/i
|
498
|
+
do_use_localhost
|
499
|
+
# ===================================================================== #
|
500
|
+
# === rf --do-not-use-localhost
|
501
|
+
# ===================================================================== #
|
502
|
+
when /^-?-?do(-|_)?not(-|_)?use(-|_)?localhost$/i,
|
503
|
+
/^-?-?no(-|_)?localhost$/i
|
504
|
+
do_not_use_localhost
|
505
|
+
# ===================================================================== #
|
506
|
+
# === rf HERE
|
507
|
+
#
|
508
|
+
# This entry point is mostly only useful on my home system, where
|
509
|
+
# the instruction "rf HERE" will delegate towards the Studium
|
510
|
+
# namespace.
|
511
|
+
# ===================================================================== #
|
512
|
+
when /HERE/
|
513
|
+
i = File.basename(return_pwd)
|
514
|
+
if is_on_roebe?
|
515
|
+
i = Studium.return_remote_homepage_of_this_lecture(i)
|
516
|
+
@internal_hash[:work_on_these_URLs].each_with_index {|inner_entry, index|
|
517
|
+
case inner_entry
|
518
|
+
when /^HERE$/
|
519
|
+
@internal_hash[:work_on_these_URLs][index] = i
|
520
|
+
end
|
521
|
+
}
|
522
|
+
end
|
523
|
+
end
|
524
|
+
end
|
373
525
|
end
|
374
526
|
|
375
527
|
# ========================================================================= #
|
376
|
-
# ===
|
528
|
+
# === could_there_be_a_potential_local_file_for?
|
377
529
|
# ========================================================================= #
|
378
|
-
def
|
379
|
-
|
530
|
+
def could_there_be_a_potential_local_file_for?(i)
|
531
|
+
# ======================================================================= #
|
532
|
+
# Try a trailing glob next. The idea here is to expand from e. g.
|
533
|
+
# "image3" towards "image3.jpg", if the latter file exists, and
|
534
|
+
# the former does NOT exist.
|
535
|
+
# ======================================================================= #
|
536
|
+
all_files = Dir["#{i}*"]
|
537
|
+
!all_files.empty?
|
380
538
|
end
|
381
539
|
|
382
540
|
# ========================================================================= #
|
383
|
-
# ===
|
541
|
+
# === work_through_all_designated_URLs
|
542
|
+
#
|
543
|
+
# This is the method that will batch-process the given input (as Array).
|
384
544
|
# ========================================================================= #
|
385
|
-
def
|
386
|
-
|
545
|
+
def work_through_all_designated_URLs(
|
546
|
+
i = @internal_hash[:work_on_these_URLs]
|
547
|
+
)
|
548
|
+
i.each {|this_argument|
|
549
|
+
do_process_this_target_URL(this_argument)
|
550
|
+
}
|
387
551
|
end
|
388
552
|
|
389
553
|
# ========================================================================= #
|
390
|
-
# ===
|
554
|
+
# === do_process_this_target_URL (URL tag, main tag)
|
391
555
|
#
|
392
|
-
# This is the
|
556
|
+
# This is the main method for this class - it will handle all given
|
557
|
+
# input properly.
|
393
558
|
# ========================================================================= #
|
394
|
-
def
|
395
|
-
|
559
|
+
def do_process_this_target_URL(
|
560
|
+
i,
|
561
|
+
optional_append_this_string = append_this_string?,
|
562
|
+
&block
|
396
563
|
)
|
397
|
-
|
398
|
-
|
564
|
+
i = i.first if i.is_a? Array # This is not necessary, but just in case.
|
565
|
+
if block_given?
|
566
|
+
yielded = yield
|
567
|
+
case yielded
|
568
|
+
# ===================================================================== #
|
569
|
+
# === :use_launchy
|
570
|
+
# ===================================================================== #
|
571
|
+
when :use_launchy
|
572
|
+
set_use_launchy(yielded.delete(:use_launchy))
|
573
|
+
end
|
574
|
+
end
|
575
|
+
if i.is_a? Symbol
|
576
|
+
case i
|
577
|
+
# ===================================================================== #
|
578
|
+
# === :default_sinatra_port
|
579
|
+
# ===================================================================== #
|
580
|
+
when :default_sinatra_port
|
581
|
+
i = 'http://localhost:4567/'
|
582
|
+
else
|
583
|
+
i = BeautifulUrl.menu(i)
|
584
|
+
end
|
585
|
+
end
|
586
|
+
if i.is_a? Array # This if-clause was added in May 2022.
|
587
|
+
i = i.join(' ').strip
|
588
|
+
end
|
399
589
|
# ======================================================================= #
|
400
|
-
#
|
590
|
+
# The next clause may remove '#'. However had, this is not always a
|
591
|
+
# good idea, as these are also linked for intralinks - so for now
|
592
|
+
# this was commented out in June 2023. But, if the file exists
|
593
|
+
# locally, then we will simply prepend "file://" instead.
|
401
594
|
# ======================================================================= #
|
402
|
-
if
|
403
|
-
|
404
|
-
if
|
405
|
-
|
406
|
-
|
407
|
-
# === palemoon
|
408
|
-
# =================================================================== #
|
409
|
-
when 'palemoon'
|
410
|
-
browser.prepend(
|
411
|
-
"C:\\Program Files\\Pale Moon\\palemoon.exe"
|
412
|
-
)
|
413
|
-
end
|
595
|
+
if i.include?('#')
|
596
|
+
raw_file = i[0 .. (i.index('#') - 1)]
|
597
|
+
if File.exist?(raw_file)
|
598
|
+
i = i.dup
|
599
|
+
i.prepend('file://')
|
414
600
|
end
|
601
|
+
end
|
602
|
+
if optional_append_this_string.size > 0
|
415
603
|
# ===================================================================== #
|
416
|
-
#
|
604
|
+
# We prepend the '#' character before appending it, unless
|
605
|
+
# it starts with the '?' character.
|
417
606
|
# ===================================================================== #
|
418
|
-
|
419
|
-
|
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 '
|
607
|
+
unless optional_append_this_string.start_with? '?'
|
608
|
+
optional_append_this_string.prepend '#'
|
435
609
|
end
|
436
|
-
|
437
|
-
|
438
|
-
|
439
|
-
|
440
|
-
|
441
|
-
|
442
|
-
|
443
|
-
|
444
|
-
|
445
|
-
|
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? '"'
|
610
|
+
i = i.dup if i.frozen?
|
611
|
+
i << optional_append_this_string
|
612
|
+
end if optional_append_this_string
|
613
|
+
# ======================================================================= #
|
614
|
+
# We must check first whether the target file at hand exists or not.
|
615
|
+
# ======================================================================= #
|
616
|
+
if i.is_a?(String) and File.exist?(i)
|
617
|
+
unless i.include?('/')
|
618
|
+
i = File.absolute_path(i) # Always use the absolute path in this case.
|
619
|
+
if use_localhost? and i.start_with?(HOME_DIRECTORY_OF_USER_X)
|
620
|
+
i.sub!(/#{HOME_DIRECTORY_OF_USER_X}/, 'http://localhost/')
|
493
621
|
end
|
622
|
+
end
|
623
|
+
open_this_in_the_browser(i) # If it exists then we can open it directly here.
|
624
|
+
else
|
625
|
+
# ===================================================================== #
|
626
|
+
# First find out whether there could be a local file for this input.
|
627
|
+
#
|
628
|
+
# For instance, the input "image3" could match towards "image3.jpg".
|
629
|
+
# So if a file called "image3.jpg" were to exist locally then we
|
630
|
+
# will prioritize it next - that is the main point for the
|
631
|
+
# following clause.
|
632
|
+
# ===================================================================== #
|
633
|
+
if could_there_be_a_potential_local_file_for?(i)
|
634
|
+
i = Dir["#{i}*"].first
|
635
|
+
elsif is_beautiful_url_available?
|
494
636
|
# =================================================================== #
|
495
|
-
#
|
637
|
+
# else we will simply try to fetch an entry from BeautifulUrl.
|
496
638
|
# =================================================================== #
|
497
|
-
|
498
|
-
|
499
|
-
|
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?
|
639
|
+
new_link = BeautifulUrl.menu(i)
|
640
|
+
case new_link
|
641
|
+
when i, nil
|
507
642
|
# ================================================================= #
|
508
|
-
#
|
509
|
-
# launchy gem.
|
643
|
+
# All is fine in this case.
|
510
644
|
# ================================================================= #
|
511
|
-
|
512
|
-
|
513
|
-
e cmd_to_use
|
514
|
-
system(cmd_to_use)
|
515
|
-
else
|
516
|
-
system(this_command) # Here we always output.
|
517
|
-
end
|
645
|
+
else
|
646
|
+
i = new_link
|
518
647
|
end
|
519
|
-
|
520
|
-
|
521
|
-
browser = 'firefox' # Else, default to this here as a generic fallback.
|
648
|
+
end
|
649
|
+
open_this_in_the_browser(i)
|
522
650
|
end
|
523
651
|
end
|
524
652
|
|
525
653
|
# ========================================================================= #
|
526
654
|
# === run (run tag)
|
527
655
|
# ========================================================================= #
|
528
|
-
def run
|
529
|
-
|
656
|
+
def run
|
657
|
+
@internal_hash[:work_on_these_URLs] = commandline_arguments?
|
658
|
+
menu(
|
659
|
+
@internal_hash[:work_on_these_URLs]
|
660
|
+
)
|
661
|
+
@internal_hash[:work_on_these_URLs].reject! {|entry|
|
662
|
+
entry.start_with?('--') # Reject entries that start with '--'.
|
663
|
+
}
|
664
|
+
sanitize_work_on_these_URLs
|
665
|
+
work_through_all_designated_URLs
|
666
|
+
end
|
667
|
+
|
668
|
+
# ========================================================================= #
|
669
|
+
# === sanitize_work_on_these_URLs
|
670
|
+
# ========================================================================= #
|
671
|
+
def sanitize_work_on_these_URLs
|
672
|
+
if @internal_hash[:work_on_these_URLs]
|
673
|
+
@internal_hash[:work_on_these_URLs].map! {|entry|
|
674
|
+
# =================================================================== #
|
675
|
+
# First handle the case where we have a local link.
|
676
|
+
# =================================================================== #
|
677
|
+
if BeautifulUrl.is_a_local_link?(entry) and !File.exist?(entry)
|
678
|
+
entry = BeautifulUrl.local_menu(entry)
|
679
|
+
end
|
680
|
+
entry
|
681
|
+
}
|
682
|
+
end
|
530
683
|
end
|
531
684
|
|
532
685
|
# ========================================================================= #
|
@@ -538,57 +691,75 @@ class InBrowser < Base # === Open::InBrowser
|
|
538
691
|
|
539
692
|
end
|
540
693
|
|
694
|
+
# =========================================================================== #
|
695
|
+
# === Open.in_browser
|
696
|
+
#
|
697
|
+
# This method will always join the input-argument before passing it
|
698
|
+
# to the class.
|
699
|
+
#
|
700
|
+
# Note that the behaviour of Open.in_browser() is in-sync with the
|
701
|
+
# method Open.in_editor(), in regards to joinable arguments
|
702
|
+
# (typically an Array as input). If you do not want this behaviour
|
703
|
+
# then you may have to prepare for this in your own code, such
|
704
|
+
# as by having an Array and iterating over it before passing
|
705
|
+
# the individual entries into Open.in_browser().
|
706
|
+
#
|
707
|
+
# Note that this method needs to be somewhat flexible, as we can provide
|
708
|
+
# varied input such as shown in the following examples:
|
709
|
+
#
|
710
|
+
# Open.open_in_browser(port: 8080)
|
711
|
+
# Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
|
712
|
+
# Open.in_browser(:default_sinatra_port)
|
713
|
+
#
|
714
|
+
# =========================================================================== #
|
715
|
+
def self.in_browser(
|
716
|
+
i = ARGV,
|
717
|
+
&block
|
718
|
+
)
|
719
|
+
if i.respond_to? :join
|
720
|
+
# ======================================================================= #
|
721
|
+
# This will only join entries that do not start with '--'.
|
722
|
+
# ======================================================================= #
|
723
|
+
entries_that_start_with_hyphens = i.select {|entry| entry.start_with?('--') }
|
724
|
+
i.reject! {|entry| entry.start_with?('--') }
|
725
|
+
new_array = []
|
726
|
+
new_array << i.join(' ').strip
|
727
|
+
new_array << entries_that_start_with_hyphens unless entries_that_start_with_hyphens.empty?
|
728
|
+
i = new_array
|
729
|
+
end
|
541
730
|
# ========================================================================= #
|
542
|
-
# ===
|
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
|
-
#
|
731
|
+
# === Handle blocks next
|
550
732
|
# ========================================================================= #
|
551
|
-
|
552
|
-
|
553
|
-
&block
|
554
|
-
)
|
733
|
+
if block_given?
|
734
|
+
yielded = yield
|
555
735
|
# ======================================================================= #
|
556
|
-
# === Handle
|
736
|
+
# === Handle Hashes next
|
557
737
|
# ======================================================================= #
|
558
|
-
if
|
559
|
-
yielded = yield
|
738
|
+
if yielded.is_a? Hash
|
560
739
|
# ===================================================================== #
|
561
|
-
# ===
|
740
|
+
# === :delay
|
562
741
|
# ===================================================================== #
|
563
|
-
if yielded.
|
564
|
-
|
565
|
-
|
566
|
-
|
567
|
-
|
568
|
-
|
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(_)
|
742
|
+
if yielded.has_key? :delay
|
743
|
+
_ = yielded.delete(:delay)
|
744
|
+
if _.is_a? String
|
745
|
+
_ = _.sub(/ seconds/,'')
|
746
|
+
_ = _.sub(/ second/,'').to_f if _.include? ' second'
|
747
|
+
_ = _.to_f # Need a Float.
|
578
748
|
end
|
749
|
+
sleep(_)
|
579
750
|
end
|
580
|
-
# else
|
581
751
|
end
|
582
|
-
|
583
|
-
|
584
|
-
|
752
|
+
end
|
753
|
+
return ::Open::InBrowser.new(i, &block)
|
754
|
+
end; self.instance_eval { alias open_in_browser in_browser } # === Open.open_in_browser
|
755
|
+
self.instance_eval { alias open_browser in_browser } # === Open.open_browser
|
585
756
|
|
586
757
|
end
|
587
758
|
|
588
759
|
# =========================================================================== #
|
589
760
|
# === open_in_browser
|
590
761
|
#
|
591
|
-
# This method will open an URL
|
762
|
+
# This method will open an URL via the browser.
|
592
763
|
#
|
593
764
|
# Usage example in pure Ruby:
|
594
765
|
#
|
@@ -603,12 +774,13 @@ def open_in_browser(
|
|
603
774
|
# ========================================================================= #
|
604
775
|
# Next, open the class that we defined above in this file here.
|
605
776
|
# ========================================================================= #
|
606
|
-
_ = Open
|
777
|
+
_ = Open.in_browser(url, :do_not_run_yet, &block)
|
607
778
|
_.check_for_intralink(optional_intralink)
|
608
779
|
_.run
|
609
780
|
end; alias oib open_in_browser # oib shortcut.
|
610
781
|
|
611
782
|
if __FILE__ == $PROGRAM_NAME
|
612
|
-
|
783
|
+
Open.in_browser(ARGV)
|
613
784
|
end # inbrowser
|
614
|
-
# inbrowser https://distrowatch.com/dwres.php?resource=ratings&distro=antix
|
785
|
+
# inbrowser "https://distrowatch.com/dwres.php?resource=ratings&distro=antix"
|
786
|
+
# inbrowser /home/x/Temp/studium/html/timetable.html#today
|