Class: Roebe::Browser

Inherits:
Base show all
Defined in:
lib/roebe/browser/menu.rb,
lib/roebe/browser/misc.rb,
lib/roebe/browser/reset.rb,
lib/roebe/browser/constants.rb

Overview

Roebe::Browser

Direct Known Subclasses

Firefox, OutputUrlThenOpenInBrowser, Palemoon

Constant Summary collapse

NAMESPACE =
#

NAMESPACE

#
inspect
FILE_BROWSER =
#

FILE_BROWSER

#
"#{Roebe.project_yaml_dir?}browser.yml"
_

Constants inherited from Base

Roebe::Base::COLOURS, Roebe::Base::HOME_DIRECTORY_OF_USER_X, Roebe::Base::N

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#actions, #append_this_onto_that_file, #append_what_into, #be_silent, #be_verbose?, #beautiful_url, #burlywood, #call_next, #chdir, #cheerful_guy, #cliner, #complex_esystem, #convert_global_env, #copy_directory, #copy_file, #cornflowerblue, #create_directory, #crimson, #current_month?, #current_time?, #current_year?, #cyan, #darkcyan, #darkgreen, #darkkhaki, #darkslateblue, #deeppink, #delete_symlink, #do_not_use_the_base_colours, #do_use_the_base_colours, #e, #ecomment, #editor_to_use?, #efancy, #eimp, #emphasis, #ensure_main_encoding, #ensure_utf_encoding, #eparse, #erev, #esteelblue, #etomato, #ewarn, #exit_program, #firebrick, #get_current_day, #get_current_month, #get_files_and_directories, #get_files_from, #get_german_name_for_this_weekday, #gold, #green, #grey, #hh_mm_ss, #hh_mm_ss_day_month_year, #home_dir?, #home_directory_of_user_x?, #infer_the_namespace, #internal_hash?, #is_a_directory?, #is_a_file?, #is_a_jpg_file?, #is_an_image_file?, #is_archive?, #is_audio_file?, #is_in_studium_dir?, #is_multimedia_file?, #is_on_roebe?, #is_on_windows?, #is_studium_available?, #is_symlink?, #is_this_a_ruby_file?, #is_video_file?, #iso_encoding?, #le, #left_colour, #lightblue, #lightgreen, #lightseagreen, #lightsteelblue, #lime, #limegreen, #localhost_to_data, #log_directory?, #main_encoding?, #mediumorchid, #mediumpurple, #mediumseagreen, #mediumslateblue, #mediumspringgreen, #mediumturquoise, #mkdir_p, #mv, #n_days_in_this_month, #n_pages_in_this_pdf_file?, #namespace?, #no_file_exists_at, #ogrey, #olive, #olivedrab, #open_in_browser, #opne, #opnesystem, #opnn, #orange, #orchid, #orev, #palegoldenrod, #palevioletred, #pink, #powderblue, #programs_dir?, #project_base_dir?, #random_html_colour, #rds, #read_file_in_iso_encoding, #read_file_via_the_default_encoding, #read_lines_via_iso_encoding, #readlines_with_main_encoding, #red, #register_sigint, #remove, #remove_directory, #remove_file, #rename_kde_konsole_tab, #replace_localhost_with_data, #report_pwd, #require_rescue, #reset_the_internal_hash, #return_all_directories_from_this_directory, #return_all_files_from_this_directory, #return_current_directory, #return_dd_mm_yyyy, #return_file_or_directory_of, #return_files_from_pwd, #return_last_part_of_the_current_directory, #return_utc, #rev, #right_arrow?, #right_colour, #roebe_log_directory?, #rosybrown, #royalblue, #ruby_base_directory?, #run_in_background, #run_rcfiles_then_run_ata_via_qdbus, #sandybrown, #sdir, #seagreen, #set_be_verbose, #set_xorg_buffer, #sfancy, #sfile, #silent_redirection?, #simp, #simple_esystem, #skyblue, #slateblue, #slategray, #springgreen, #steelblue, #string_italic, #swarn, #symlink, #teal, #temp_dir?, #to_camelcase, #to_counted_hash, #tomato, #touch, #try_to_require_the_beautiful_url_gem, #try_to_require_the_html_template, #try_to_require_the_open_gem, #try_to_require_the_program_information_gem, #try_to_require_the_xorg_buffer, #use_colours?, #verbose_truth, #weekday?, #word_wrap, #write_what_into, #yellow

Methods included from Roebe::Base::CommandlineArguments

#append_onto_the_commandline_arguments, #clear_commandline_arguments, #commandline_arguments?, #commandline_arguments_as_string?, #commandline_arguments_without_leading_hyphens?, #first_argument?, #first_argument_without_leading_hyphens?, #has_an_argument_been_passed?, #remove_hyphened_arguments_from_the_commandline_arguments, #return_commandline_arguments_with_leading_hyphens, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(commandline_arguments = nil, run_already = true) ⇒ Browser

#

initialize

#


14
15
16
17
18
19
20
21
22
23
# File 'lib/roebe/browser/misc.rb', line 14

def initialize(
    commandline_arguments = nil,
    run_already           = true
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  run if run_already
end

Class Method Details

.[](i = '') ⇒ Object

#

Roebe::Browser[]

#


243
244
245
# File 'lib/roebe/browser/misc.rb', line 243

def self.[](i = '')
  new(i)
end

.e(i = '') ⇒ Object

#

Roebe::Browser.e

#


236
237
238
# File 'lib/roebe/browser/misc.rb', line 236

def self.e(i = '')
  puts i
end

.permanently_use_this_browser(location_to_the_binary = '/usr/bin/firefox', into = FILE_BROWSER) ⇒ Object

#

Roebe::Browser.permanently_use_this_browser

This method can be used to permanently set the location to a browser.

#


292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/roebe/browser/misc.rb', line 292

def self.permanently_use_this_browser(
    location_to_the_binary = '/usr/bin/firefox',
    into = FILE_BROWSER
  )
  case into
  when :default
    into = FILE_BROWSER
  end
  e rev+'Storing into '+sfile(into)+' next.'
  ::Roebe.write_what_into(location_to_the_binary, into)
  if ::Roebe.is_on_roebe?
    into = RUBY_SRC_DIR+'roebe/lib/roebe/yaml/'+
           File.basename(into)
    e rev+'Storing into '+sfile(into)+' as well.'
    ::Roebe.write_what_into(location_to_the_binary, into)
  end
end

.return_remote_homepage_of_this_lecture(i) ⇒ Object

#

Roebe::Browser.return_remote_homepage_of_this_lecture

#


313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/roebe/browser/misc.rb', line 313

def self.return_remote_homepage_of_this_lecture(i)
  if i.is_a? Array
    i = i.first
  end
  if Object.const_defined?(:Studium) and
     Studium.respond_to?(:return_homepage_for)
    result = Studium.return_homepage_for(i)
    if i == result
      # =================================================================== #
      # === Delegate towards BeautifulUrl next
      #
      # In this case no change was made, so we assume we did not find
      # a remote homepage for this entry. Thus, delegate towards
      # BeautifulUrl.
      # =================================================================== #
      if Object.const_defined? :BeautifulUrl
        i = BeautifulUrl[i] { :array_if_possible }
      end
    else
      i = result
    end
    i = i.first if i.is_a? Array
  end
  i
end

.revObject

#

Roebe::Browser.rev

#


229
230
231
# File 'lib/roebe/browser/misc.rb', line 229

def self.rev
  ::Roebe.rev
end

.sfile(i) ⇒ Object

#

Roebe::Browser.sfile

#


222
223
224
# File 'lib/roebe/browser/misc.rb', line 222

def self.sfile(i)
  ::Roebe.sfile(i)
end

Instance Method Details

#considering_opening_this_remote_url(i) ⇒ Object Also known as: consider_opening_url_in_the_browser

#

considering_opening_this_remote_url

This is the method that will set the URL to use; and then open this in the browser as well.

#


273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/roebe/browser/misc.rb', line 273

def considering_opening_this_remote_url(i)
  set_url(i)
  _ = url?
  if @be_verbose
    e "#{rev}Next opening #{steelblue(_)} in the browser."
  end
  # ======================================================================= #
  # Delegate towards the method open_in_browser() next.
  # ======================================================================= #
  cmd = @path_to_the_browser+' '+url?.to_s+' '+@optional_argument.to_s
  esystem cmd.strip
end

#generate_tab_completionsObject

#

generate_tab_completions

To trigger this method, do:

browser.rb tabcompletions
#


160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/roebe/browser/misc.rb', line 160

def generate_tab_completions
  require 'case_parser'
  begin
    require 'generate_shell_completion'
  rescue LoadError; end
  array = CaseParser.parse(MENU)
  _ = GenerateShellCompletion::GenerateShellCompletion.new(nil, :dont_run_yet)
  _.set_name_of_the_shell_function 'beautiful_menu'
  if array.empty?
    opne 'The array is empty at line `'+__LINE__.to_s+'`.'
  end
  _.set_dataset(array) # Which dataset to use.
  _.set_name "#{HOME_DIRECTORY_OF_USER_X}DATA/PROGRAMMING_LANGUAGES/RUBY/src/"\
             "roebe/lib/roebe/shell_scripts/completion_for_rf.sh"
  _.set_aliases 'rf'
  _.run
end
#

menu (menu tag)

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/roebe/browser/menu.rb', line 14

def menu(
    i = commandline_arguments?
  )
  if i.is_a? Array
    i.each {|entry| menu(entry) }
  else
    case i
    # ===================================================================== #
    # === rf --browser?
    # ===================================================================== #
    when /^-?-?browser\??$/i
      show_which_browser_is_in_use
    # ===================================================================== #
    # === rf --browser=/usr/bin/firefox
    #
    # Or use the alias "--set=".
    # ===================================================================== #
    when /^-?-?browser=(.+)$/i,
         /^-?-?set=(.+)$/i
      permanently_use_this_browser($1.to_s.dup)
    # ===================================================================== #
    # === rf --help
    # ===================================================================== #
    when /^-?-?help$/i,
         '--help?'
      show_help
      exit
    # ===================================================================== #
    # === LAST
    # ===================================================================== #
    when 'LAST'
      i = `xsel -o`.to_s.chomp
    # ===================================================================== #
    # === rf file?
    #
    # This entry point will show where the .yml file resides.
    #
    # Note that the leading ^ is required in the following regex, so that
    # links such as "https://sourceforge.net/projects/porg/files/?source=navbar"
    # do not trigger this entry point here.
    #
    # Invocation example:
    #
    #   rf --file?
    #
    # ===================================================================== #
    when /^-?-?file\??$/
      e sfile(FILE_BROWSER)
      exit
    # ===================================================================== #
    # === rf HERE
    # ===================================================================== #
    when /^HERE$/i,
         /^-?-?pwd$/i # === rf PWD
      if return_pwd.include? 'studium'
        i = File.basename(return_pwd)
        if i.include?('_') and (i =~ /^\d+/)
          i.tr!('_',' ')
          i.delete!('.')
          # =============================================================== #
          # Handle entries such as:
          #
          #   300665-1 Protein Transport und Sortierung in Säugetierzellen
          #
          # =============================================================== #
          regex_for_numbers = /^(\d+-?\d*)/ # See: https://rubular.com/r/idmnZlKA0S
          i.gsub!(regex_for_numbers, '')
          i.strip!
          considering_opening_this_remote_url(i)
        end
      end
    # ===================================================================== #
    # === rf --completions
    # ===================================================================== #
    when /^-?-?completion$/i,
         /^-?-?complete$/i,
         /^-?-?gentab$/i,
         /^-?-?generate(_|-)?tab(_|-)?completions$/i,
         /^-?-?generate(_|-)?tab$/i,
         /^-?-?generate(_|-)?completions?$/i,
         /^-?-?tabcompletions$/i,
         /^-?-?TAB$/i
      generate_tab_completions
    else
      considering_opening_this_remote_url(i)
    end
  end
end

#permanently_use_this_browser(i) ⇒ Object

#

permanently_use_this_browser

#


53
54
55
56
57
58
# File 'lib/roebe/browser/misc.rb', line 53

def permanently_use_this_browser(i)
  if File.exist?(i) and !i.include?('/')
    i = File.absolute_path(i)
  end
  ::Roebe::Browser.permanently_use_this_browser(i)
end

#resetObject

#

reset (reset tag)

#


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/roebe/browser/reset.rb', line 14

def reset
  super()
  infer_the_namespace
  # ======================================================================= #
  # === @path_to_the_browser
  # ======================================================================= #
  @path_to_the_browser = File.read(FILE_BROWSER)
  # ======================================================================= #
  # === @name_of_the_browser
  #
  # This variable keeps track of the name of the browser, as a Symbol.
  #
  # Permissive values include :palemoon, :firefox, :chrome and so
  # forth. Evidently this can be changed at a later time.
  # ======================================================================= #
  @name_of_the_browser = nil
  # ======================================================================= #
  # === @optional_argument
  # ======================================================================= #
  @optional_argument = nil
  # ======================================================================= #
  # === @use_this_url
  # ======================================================================= #
  @use_this_url = nil
  # ======================================================================= #
  # === @be_verbose
  #
  # If the following variable is true then the class will be more
  # verbose about which URL is about to be opened.
  # ======================================================================= #
  @be_verbose = false
end

#return_remote_homepage_of_this_lecture(i) ⇒ Object

#

return_remote_homepage_of_this_lecture

#


215
216
217
# File 'lib/roebe/browser/misc.rb', line 215

def return_remote_homepage_of_this_lecture(i)
  ::Roebe::Browser.return_remote_homepage_of_this_lecture(i)
end

#runObject

#

run (run tag)

#


208
209
210
# File 'lib/roebe/browser/misc.rb', line 208

def run
  menu
end

#set_name(i = :palemoon) ⇒ Object

#

set_name

#


46
47
48
# File 'lib/roebe/browser/misc.rb', line 46

def set_name(i = :palemoon)
  @name_of_the_browser = i.to_sym
end

#set_path_to_the_browser(i) ⇒ Object

#

set_path_to_the_browser

#


263
264
265
# File 'lib/roebe/browser/misc.rb', line 263

def set_path_to_the_browser(i)
  @path_to_the_browser = i
end

#set_use_this_url(i = '') ⇒ Object Also known as: set_url

#

set_use_this_url

#


63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/roebe/browser/misc.rb', line 63

def set_use_this_url(i = '')
  if i.is_a?(Symbol) and is_on_roebe?
    i = BeautifulUrl.sanitize_this_input(i)
  end
  if i.is_a? Array
    if i[1]
      # =================================================================== #
      # Obtain the second argument if available, like in:
      #
      #   rf diacgi history
      #
      # This will be used as intralink. If that intralink starts with
      # a '?' character then we will not use a '#' character before it.
      #
      # Since as of Oct 2016, we will use all the remaining arguments
      # here.
      # =================================================================== #
      @optional_argument = i[1..-1] 
    end
    i = i[0]
  end
  i = i.dup if i.frozen?
  # ======================================================================= #
  # The following is tried since March 2016. We will replace all ' '
  # tokens with '_' tokens. But in January 2021 this was considered
  # problematic for localhost entries; at the least for .cgi files.
  # So .cgi files require a slightly different approach.
  # ======================================================================= #
  if i.respond_to?(:include?)
    if i.include?('.cgi')
      i.tr!(' ','#')     if i.include? ' '
    else
      i.tr!(' ','_')     if i.include? ' '
    end
  else
    i = i.to_s
  end
  i.gsub!(/_-_/,'_') if i.include? '_-_'
  i.gsub!(/-_/,'_')  if i.include? '-_'
  i.delete!(N)
  i.rstrip!
  i.chop! if i.end_with? '~'
  i.rstrip!
  i.chop! if i.end_with? '_' and i.count('_') > 1
  i.delete!('()') if i.include? '('
  # ===================================================================== #
  # Check for .cgi files next. Invocation example:
  #
  #   browser.rb kontakt.cgi
  #
  # ===================================================================== #
  if i.end_with?('.cgi') and
     i.include?(HOME_DIRECTORY_OF_USER_X)
    use_this_regex = /#{Regexp.quote("#{HOME_DIRECTORY_OF_USER_X}")}/
    i.sub!(use_this_regex, 'http://localhost/')
  end
  # ======================================================================= #
  # In the past, we used to downcase everything. This is not
  # always useful, such as for URLs. Hence, we do not downcase
  # since as of April 2016 if the input contains a 'htt' substring.
  # ======================================================================= #
  unless i.include?('htt') or i.include?(HOME_DIRECTORY_OF_USER_X)
    i.downcase! # This is not good because
  end
  old_input = i.dup
  i = return_remote_homepage_of_this_lecture(i)
  if is_on_roebe? and (old_input == i)
    i = BeautifulUrl[i]
  end
  if is_on_roebe? and i.end_with?('.cgi') and i.include?(HOME_DIRECTORY_OF_USER_X)
    use_this_regex = /#{Regexp.quote("#{HOME_DIRECTORY_OF_USER_X}")}/
    i.sub!(use_this_regex, 'http://localhost/')
  end
  unless i.start_with? '"'
    # ===================================================================== #
    # This will also handle URLs such as 'sources/?C=M;O=D'
    # ===================================================================== #
    i = '"'+i+'"'
  end
  @use_this_url = i
end

#show_helpObject

#

show_help (help tag)

Invocation example:

browser --help
#


33
34
35
36
37
38
39
40
41
# File 'lib/roebe/browser/misc.rb', line 33

def show_help
  # ======================================================================= #
  # This is a stub - subclasses may have to define this for the particular
  # browser at hand.
  # ======================================================================= #
  e
  e '  browser --set=firefox # set the path to firefox'
  e
end

#show_which_browser_is_in_use(_ = @path_to_the_browser) ⇒ Object

#

show_which_browser_is_in_use

This method will show which browser is in use.

Normally we will store which browser is in use in this file:

roebe/yaml/browser.yml
#


188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/roebe/browser/misc.rb', line 188

def show_which_browser_is_in_use(
    _ = @path_to_the_browser
  )
  if File.exist? _
    e
    opnn
    e "The browser that will be used is:#{N}#{N}"
    if _.end_with? '.yml'
      _ = File.read(_)
    end
    e sfile(_)
    e
  else
    opnn; no_file_exists_at(_)
  end
end

#use_this_url?Boolean Also known as: url?

#

use_this_url?

#

Returns:

  • (Boolean)


148
149
150
# File 'lib/roebe/browser/misc.rb', line 148

def use_this_url?
  @use_this_url
end