Class: Studium::GenerateRegex

Inherits:
Base
  • Object
show all
Defined in:
lib/studium/utility_scripts/regexes/generate_regex.rb

Overview

Studium::GenerateRegex

Constant Summary

Constants inherited from Base

Base::ARRAY_HTML_COLOURS_IN_USE, Base::CHECK_FOR_INCORRECT_ENTRIES_IN_THE_QUESTION_ANSWER_FILE_THAT_SHOULD_BE_COMMENTS, Base::COLOURS, Base::COMMON_YEAR_DAYS_IN_MONTH, Base::GRAY, Base::LEFT_PADDING, Base::NAMESPACE, Base::N_CHARACTERS_PER_LINE, Base::UNICODE_RIGHT_ARROW

Constants included from Colours

Colours::COLOURS

Instance Method Summary collapse

Methods inherited from Base

#add_proper_year_entry_to_this_line, #already_solved_this_lva_number?, #append_what_into, #available_topics_as_short_name, #average_grade, #bachelor_master_or_diploma, #base_dir?, #be_silent, #be_verbose?, #beautiful_url, #change_directory, #cheering_person, #cliner, #colour_for_ects_points, #colourize_percentage, #commandline_arguments?, #commandline_arguments_joined, #commandline_arguments_with_hyphens?, #convert_this_array_of_lectures_into_the_full_dataset, #copy, #cpruby, #create_directory, #create_file, #csv_file?, #current_month?, #datum?, #day_month_year, #debug?, #delete_file, #directory_of_the_exam_topics?, #directory_to_my_exam_topics, #directory_to_the_exam_topics?, #disable_colours, #disable_colours_globally, #do_not_be_verbose, #do_not_use_opn, #do_these_curricula_include_both_a_bachelor_and_a_master_curriculum?, #does_this_line_include_a_german_weekday?, #does_this_line_include_a_html_colour?, #e, #ecomment, #editor?, #ee, #enable_colours, #enable_colours_globally, #enable_debug, #encoding?, #english_to_german_weekday, #ensure_main_encoding, #ensure_that_the_log_directory_exists, #eparse, #erev, #esystem, #etomato, #exit_program, #expand_topic, #extract_dd_mm_yyyy, #file_lecture_information?, #file_read_with_proper_encoding, #filter_away_invalid_questions, #find_corresponding_exam_topic, #first_argument?, #first_non_hyphen_argument?, #from_curriculum_id_to_university, #german_date_notation, #german_weekday?, #german_weekdays?, #has_a_bachelor_curriculum?, #has_a_master_curriculum?, #has_this_exam_topic_been_solved?, #has_year_entry?, #hh_mm_ss_dd_mm_yyyy, #horizontal_bar?, #html_log_directory?, #is_a_master_lecture?, #is_a_weekday?, #is_connected_to_the_www?, #is_on_roebe?, #is_prüfungsimmanent?, #is_this_a_valid_html_colour?, #is_this_day_today?, #konsole_support?, #last_file?, #lectures_from_indi1, #lectures_from_indi2, #lectures_from_indi3, #lectures_from_indi4, #lectures_from_the_bachelor_genetik_curriculum, #lectures_from_the_individual_vektor_curricula, #load_yaml_dataset, #move_file, #n_days_difference_from_today_to_this_day, #no_ext_name, #no_file_at, #number_to_month, #obtain, #obtain_lectures_from_this_curriculum, #open_in_editor, #opnn, #path_to_exam_topic?, #read_dataset_from_a_curriculum_file, #read_in_exam_dataset_from_this_file, #read_this_file_with_default_encoding, #readlines_from_this_input_file, #register_sigint, #remove_empty_strings, #remove_escape_sequences, #remove_internal_comments, #remove_leading_comments, #remove_leading_weekday_names_from, #remove_newlines, #remove_tags_from_this_input, #rename_kde_konsole_tab, #rename_konsole_tab?, #replace_all_html_colours_in_this, #replace_bold_token_with_default_colour, #replace_italic_token_with_default_colour, #replace_italic_token_with_this_colour, #replace_regular_numbers_with_unicode_numbers, #replace_underline_token_with_default_colour, #replace_underline_token_with_this_colour, #return_all_bachelor_lectures, #return_all_exams_on_this_day, #return_all_numbers_from, #return_all_registered_curricula, #return_all_steop_lectures, #return_current_year, #return_dataset_for_this_topic, #return_dataset_from_this_curriculum_file, #return_dd_mm_yy_and_time_from, #return_directory_for, #return_ects_points_from_these_lectures, #return_either_grey_or_the_custom_colour_for_answers, #return_either_grey_or_the_custom_colour_for_questions, #return_file_for_this_curriculum, #return_german_name_for_this_english_month, #return_german_weekday_of?, #return_hh_mm_ss_from, #return_hour_from_this, #return_lectures_from_bachelor_vektor_curriculum, #return_lectures_with_at_the_least_one_upcoming_exam, #return_local_path_of_this_pwdstud, #return_lva_number_of_this_lecture, #return_month_fitting_to_this_input, #return_n_days_until_monday, #return_n_days_until_sunday, #return_n_exam_questions_in, #return_n_questions_were_answered_for_this_topic, #return_name_of_the_weekday, #return_name_of_this_curriculum, #return_name_of_this_curriculum_based_on_its_id_number, #return_opnn, #return_passed_courses, #return_pwd, #return_random_curriculum, #return_sanitized_dataset_from_the_file_lecture_information, #return_solved_lectures, #return_steop_lectures, #return_the_remote_homepage_url, #return_this_line_has_n_characters_as_html_colour_tags, #return_title_from_this_curriculum_file, #rev, #right_arrow?, #rinstall2, #roebe_exam_directory?, #ruby_src_dir_at_home?, #runmode?, #sanitize_this_topic, #sanitize_url, #select_lectures_with_this_key, #set_be_quiet, #set_be_verbose, #set_commandline_arguments, #set_commandline_mode, #set_runmode, #set_this_cd_alias_to, #set_www_mode, #sfancy, #shall_we_make_use_of_unicode_symbols?, #show_todays_date, #simp, #sort_these_lecture_names_by_time, #spacer, #swarn, #this_month_has_n_days?, #total_ects_points_passed, #translate_dd_mm_yyyy_to_weekday, #try_to_require_the_rcfiles, #turn_this_array_into_the_full_dataset, #unicode_blocks_line, #use_colours?, #use_opn?, #vertical_bar?, #web_liner, #weekday?, #weekdays?, #word_wrap, #write_what_into, #www_connection_is_unavailable, #www_mode?, #yaml_dir?

Methods included from Colours

#bold_blue, #bold_red, #bold_yellow, #cadetblue, #chartreuse, crimson, #darkgray, darkgreen, darkseagreen, deepskyblue, #deepskyblue, default_colour, #dimgray, disable_colours, #dodgerblue, e, #efancy, enable_colours, #ewarn, #forestgreen, gold, #grey, lightblue, lightgreen, #lightslategray, #lightsteelblue, #mediumorchid, #mediumseagreen, #oldlace, #olivedrab, orange, orangered, #orchid, palevioletred, #papayawhip, rev, royalblue, sdir, #sdir, sfancy, #sfancy, sfile, #sfile, simp, #simp, slateblue, steelblue, #steelblue, #swarn, swarn, #teal, turquoise, use_colours?, use_this_colour_for_exam_answers, use_this_colour_for_exam_questions, #yellow

Constructor Details

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

#

initialize

#


23
24
25
26
27
28
29
30
31
32
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 23

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

Instance Method Details

#determine_all_entries_that_include_the_desired_entryObject

#

determine_all_entries_that_include_the_desired_entry

#


228
229
230
231
232
233
234
235
236
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 228

def determine_all_entries_that_include_the_desired_entry
  @dataset.select! {|key, inner_hash|
    outdated_lecture = inner_hash[:outdated_lecture]
    # ===================================================================== #
    # Outdated lectures are ignored since as of April 2022.
    # ===================================================================== #
    inner_hash.has_key?(@search_for_this_entry) and !(outdated_lecture == true)
  }
end

#generate_the_relevant_ruby_codeObject

#

generate_the_relevant_ruby_code (generate tag, gen tag)

This method will first generate the relevant ruby code, that can then be saved into a file.

#


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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 76

def generate_the_relevant_ruby_code
  @string = <<-EOF
#!/usr/bin/ruby -w
# Encoding: #{Studium.main_encoding?}
# frozen_string_literal: true
# =========================================================================== #
# require 'studium/utility_scripts/#{@use_this_as_the_toplevel_name_for_the_method}.rb'
# =========================================================================== #
module Studium

# ========================================================================= #
# === Studium.#{@use_this_as_the_toplevel_name_for_the_method}
#
# This method will take a given input, as String, and then return
# a corresponding remote URL. If the input is not registered then
# this method will return the original input String.
# ========================================================================= #
def self.#{@use_this_as_the_toplevel_name_for_the_method}(i)
  case i # case tag
EOF
  @string = @string.dup # De-freeze it.
  @dataset.each_pair {|name_of_the_lecture, remote_url|
    # ===================================================================== #
    # We have to convert the variable name_of_the_lecture into its
    # regex-representation. In the past Regexp.quote() was used,
    # but this was changed to a manual gsub-chain for more control.
    # ===================================================================== #
    replacement = name_of_the_lecture.gsub(/\//,'\/?').
                                      gsub(/\(/,'\(?').
                                      gsub(/\)/,'\)?').
                                      gsub(/-/,'(-|_)?').
                                      gsub(/,/,',?'). # ← Need ',' to be optional.
                                      gsub(/ /,'(-|_| )?').
                                      gsub(/\./,'\.?').
                                      gsub(/"/,'"?') # ← This is necessary to make " optional, such as in "omics".
    _ = '/^'+replacement+'$/i'.dup
    # ===================================================================== #
    # The next clause will also generate a slightly shorter "alias" to
    # the lecture at hand, by omitting the leading number part such as
    # "289252".
    # ===================================================================== #
    if name_of_the_lecture.include?(' ') and
       name_of_the_lecture.start_with?(/\d/)
      # =================================================================== #
      # If there is a leading number and at the least one ' ' token:
      # =================================================================== #
      splitted = name_of_the_lecture.split(' ')
      short_name = splitted[1..-1].join(' ').strip
      replacement = short_name.gsub(/\//,'\/?').
                               gsub(/\(/,'\(?').
                               gsub(/\)/,'\)?').
                               gsub(/-/,'(-|_)?').
                               gsub(/,/,',?'). # ← Need ',' to be optional.
                               gsub(/ /,'(-|_| )?').
                               gsub(/\./,'\.?').
                               gsub(/"/,'"?') # ← This is necessary to make " optional, such as in "omics".
      _ << ','+N
      _ << '         /^'+replacement+'$/i'.dup
    elsif name_of_the_lecture.include?(' ') and
         !(name_of_the_lecture =~ /\d+/)
      # =================================================================== #
      # The clause here is for entries such as:
      #
      #   "Basiswissen Mathematik für die Bioinformatik"
      #
      # This entry has no number at all, so we will also omit the first
      # word altogether, and add the rest as an assumed alias.
      # =================================================================== #
      splitted = name_of_the_lecture.split(' ')
      short_name = splitted[1..-1].join(' ').strip
      replacement = short_name.gsub(/\//,'\/?').
                               gsub(/\(/,'\(?').
                               gsub(/\)/,'\)?').
                               gsub(/ /,'(-|_| )?')
      _ << ','+N
      _ << '         /^'+replacement+'$/i'.dup
    end
    @string << '    # '+('='*71)+' #'+N
    @string << "    # === #{@rf_or_moodle} \"#{name_of_the_lecture}\"#{N}"
    @string << '    # '+('='*71)+' #'+N
    # ===================================================================== #
    # Next add the corresponding when-clause:
    # ===================================================================== #
    @string << '    when '+_.to_s+N
    if remote_url.is_a? Array # This here is just debugging quickly.
      # =================================================================== #
      # An Array can be used for remote_url, indicating the current URL in 
      # use, and older, outdated entries, e. g. 2019W and 2020W.
      # =================================================================== #
      remote_url = remote_url.first
    end
    @string << '      "'+remote_url.to_s+'"'+N
  }
  @string << '    else'+N
  @string << '      i'+N
  @string << '    end'+N
  @string << '  end'
  # ======================================================================= #
  # The next is only valid for :homepage entries, not :moodle entries.
  # ======================================================================= #
  if @search_for_this_entry == :homepage
    @string << "; self.instance_eval { alias "\
             "return_remote_url_of_this_lecture #{@use_this_as_the_toplevel_name_for_the_method} } "\
             "# === Studium.return_remote_url_of_this_lecture#{N}"
    @string << "       self.instance_eval { alias "\
             "return_url_from                   #{@use_this_as_the_toplevel_name_for_the_method} } "\
             "# === Studium.return_url_from#{N}"
    # ===================================================================== #
    # Add alias for:
    #   Studium.return_homepage_of_this_lecture
    # ===================================================================== #
    @string << "       self.instance_eval { alias "\
               "return_homepage_of_this_lecture   #{@use_this_as_the_toplevel_name_for_the_method} } "\
               "# === Studium.return_remote_homepage_of_this_lecture#{N}"
    # ===================================================================== #
    # Add alias for:
    #   Studium.return_homepage_for
    # ===================================================================== #
    @string << "       self.instance_eval { alias "\
               "return_homepage_for               #{@use_this_as_the_toplevel_name_for_the_method} } "\
               "# === Studium.return_homepage_for#{N}"
    # ===================================================================== #
    # Next add a toplevel method that allows us to query whether the
    # given input, assumed to be a remote URL, is included in the above
    # case/when menu interface:
    # ===================================================================== #
    @string << N+'  # '+('='*71)+' #'+N
    @string << '  # === Studium.is_this_url_registered?'+N
    @string << '  #'+N
    @string << '  # This method can be used to determine whether a given URL is registered'+N
    @string << '  # within the Studium-namespace, or whether it is not.'+N
    @string << '  # '+('='*71)+' #'+N
    @string << '  def self.is_this_url_registered?(i)'+N
    @string << '    remote_url = Studium.return_remote_url_of_this_lecture(i)'+N
    @string << '    !(remote_url == i) # If both are the same, then the URL is NOT registered.'+N
    @string << '  end'
  # ======================================================================= #
  # === :moodle
  # ======================================================================= #
  elsif @search_for_this_entry == :moodle
    @string << "; self.instance_eval { alias "\
               "return_remote_moodle_link_of_this_lecture #{@use_this_as_the_toplevel_name_for_the_method} } "\
               "# === Studium.return_remote_moodle_link_of_this_lecture"
  end
  @string << "#{N}#{N}"
  @string << 'end' # ← This is the trailing end.
  return @string
end

#resetObject

#

reset (reset tag)

#


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
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 37

def reset
  super()
  # ======================================================================= #
  # === @namespace
  # ======================================================================= #
  @namespace = NAMESPACE
  # ======================================================================= #
  # === @use_this_as_the_toplevel_name_for_the_method
  # ======================================================================= #
  @use_this_as_the_toplevel_name_for_the_method = 'return_remote_homepage_of_this_lecture'
  # ======================================================================= #
  # === @dataset
  # ======================================================================= #
  @dataset = return_dataset_from_the_file_lecture_information
  # ======================================================================= #
  # === @store_into
  # ======================================================================= #
  @store_into =
    "#{RUBY_STUDIUM_HOME_DIR}utility_scripts/return_remote_homepage_of_this_lecture.rb"
  # ======================================================================= #
  # === @search_for_this_entry
  #
  # The following variable keeps track as to which entry we are
  # selecting for. By default we may pick :homepage, but another
  # useful entry is :moodle.
  # ======================================================================= #
  @search_for_this_entry = :homepage
  # ======================================================================= #
  # === @rf_or_moodle
  # ======================================================================= #
  @rf_or_moodle = 'rf'
end

#runObject

#

run (run tag)

#


279
280
281
282
283
284
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 279

def run
  determine_all_entries_that_include_the_desired_entry
  simplify_these_discovered_entries
  _ = generate_the_relevant_ruby_code
  store(_)
end

#simplify_these_discovered_entries(search_for_this_entry = @search_for_this_entry) ⇒ Object

#

simplify_these_discovered_entries

This method will simplify the @dataset, by keeping track only of the name of the lecture, and the remote URL to it.

#


244
245
246
247
248
249
250
251
252
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 244

def simplify_these_discovered_entries(
    search_for_this_entry = @search_for_this_entry
  )
  _ = {}
  @dataset.each_pair {|key, inner_hash|
    _[key] = inner_hash[search_for_this_entry]
  }
  @dataset = _
end

#store(what, into = @store_into) ⇒ Object

#

store

This method will store the generated string. It only works on is-roebe systems, though.

#


260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/studium/utility_scripts/regexes/generate_regex.rb', line 260

def store(
    what,
    into = @store_into
  )
  if is_on_roebe?
    e "#{rev}Storing into the file `#{sfile(into)}` next."
    write_what_into(what, into)
  else
    e 'We are not on a roebe-system, so we will not automatically'
    e 'generate any files. We will, however had, display the'
    e 'autogenerated string on these systems next:'
    e
    e slateblue(what)
  end
end