Class: Colours::ReplaceTokensWithColourCode

Inherits:
Base
  • Object
show all
Defined in:
lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb

Overview

Colours::ReplaceTokensWithColourCode

Constant Summary collapse

ARRAY_ALLOWED_HTML_COLOURS =
#

ARRAY_ALLOWED_HTML_COLOURS

#
::Colours.array_html_colours?.map {|entry|
  entry.to_sym
}
ARRAY_ALLOWED_NUMBERED_WORDS =
#

ARRAY_ALLOWED_NUMBERED_WORDS

#
%w(
  one
  two
  three
  four
  five
)
HASH_DEFAULT_REPLACEMENT_COLOURS =
#

HASH_DEFAULT_REPLACEMENT_COLOURS

#
{
  one:            :teal,           # This could also be :steelblue.
  two:            :lightseagreen,  # Or :dodgerblue.
  three:          :mediumseagreen, # ← Used to be 'peru' up until 23.11.2019.
  four:           :mediumorchid,   # ← Used to be 'olivedrab' up until 06.12.2022.
  five:           :lightgreen,     # Or :olivedrab.
  rev:            ::Colours.rev,     # Or  USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR.
  default_colour: ::Colours.rev      # This is actually the same as :rev. Could also be USE_THIS_COLOUR_FOR_THE_DEFAULT_COLOUR.
}

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#commandline_arguments?, #e, #extend_module_256_colours, #first_argument?, #second_argument?, #set_commandline_arguments

Constructor Details

#initialize(commandline_arguments = nil, run_already = true, &block) ⇒ ReplaceTokensWithColourCode

#

initialize

#


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
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 70

def initialize(
    commandline_arguments = nil,
    run_already           = true,
    &block
  )
  reset
  set_commandline_arguments(
    commandline_arguments
  )
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # === Handle Hashes next
    # ===================================================================== #
    if yielded.is_a? Hash
      do_update_the_main_hash_with_this_dataset(yielded)
    end
  end
  case run_already
  # ======================================================================= #
  # === :do_not_run_yet
  # ======================================================================= #
  when :do_not_run_yet
    run_already = false
  end
  run if run_already
end

Class Method Details

.[](i = ARGV) ⇒ Object

#

Colours::ReplaceTokensWithColourCode[]

#


338
339
340
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 338

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

Instance Method Details

#do_update_the_main_hash_with_this_dataset(i) ⇒ Object

#

do_update_the_main_hash_with_this_dataset

#


157
158
159
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 157

def do_update_the_main_hash_with_this_dataset(i)
  @hash_replacement_colours.update(i)
end

#does_this_string_contain_a_special_token?(i) ⇒ Boolean

#

does_this_string_contain_a_special_token?

A special token is either a HTML colour, or something like <one></one>.

#

Returns:

  • (Boolean)


322
323
324
325
326
327
328
329
330
331
332
333
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 322

def does_this_string_contain_a_special_token?(i)
  scanned_result = i.scan(
    @main_regex
  ).flatten
  does_it_include_a_HTML_colour = scanned_result.any? {|entry|
    ARRAY_ALLOWED_HTML_COLOURS.include?(entry.to_sym)
  }
  does_it_include_a_numbered_word = scanned_result.any? {|entry|
    ARRAY_ALLOWED_NUMBERED_WORDS.include?(entry)
  }
  return (does_it_include_a_HTML_colour or does_it_include_a_numbered_word) 
end

#hash_replacement_colours?Boolean Also known as: main_hash?

#

hash_replacement_colours?

#

Returns:

  • (Boolean)


164
165
166
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 164

def hash_replacement_colours?
  @hash_replacement_colours
end

#is_this_a_HTML_colour?(i) ⇒ Boolean

#

is_this_a_HTML_colour?

#

Returns:

  • (Boolean)


136
137
138
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 136

def is_this_a_HTML_colour?(i)
  ARRAY_ALLOWED_HTML_COLOURS.include?(i)
end

#main_regex?Boolean

#

main_regex?

#

Returns:

  • (Boolean)


150
151
152
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 150

def main_regex?
  REGEX_FOR_HTML_COLOURS
end

#parse_this_complex_line(i, use_this_as_replacement_hash = :default, rev = nil) ⇒ Object Also known as: parse

#

parse_this_complex_line (parse tag)

This is the method that will parse a complex line. A complex line is a line that may include e. g. <one> as well as <royalblue> and similar such entities.

This method will always assign towards @sanitized_line whenever it is called.

#


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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 185

def parse_this_complex_line(
    i,
    use_this_as_replacement_hash = :default, # This is, as the name suggests, the replacement Hash.
    rev                          = nil       # This then defaults to ::Colours.revert
  )
  rev = ::Colours.rev if rev.nil? # Add a small safeguard here.
  if i and does_this_string_contain_a_special_token?(i)
    use_this_colour_for_the_default_colour = rev.to_sym # Default.
    # ===================================================================== #
    # === The replacement Hash
    #
    # Specify our "replacement-hash", that is colours used for e. g.
    # <one>, <two> or <three> or HTML colours such as <royalblue>
    # and so forth.
    #
    # This clause only matters if we have a special token in the given
    # String.
    # ===================================================================== #
    case use_this_as_replacement_hash
    # ===================================================================== #
    # === :default
    #
    # The default entry-point.
    # ===================================================================== #
    when :default,
         :default_hash
      # =================================================================== #
      # The following hash can be used as a default "replacement" Hash.
      # =================================================================== #
      use_this_as_replacement_hash = @hash_replacement_colours
    end
    # ===================================================================== #
    # Next we can finally replace the found keyword. We have to make a
    # decision here, though - if it is part of the Array containing
    # all HTML colours, then we'll replace it with the HTML colour at
    # hand; otherwise we'll use the replacement Hash as-is:
    # ===================================================================== #
    scanned_result = i.scan(
      @main_regex
    ).flatten
    # ===================================================================== #
    # We then know that this line contains either a HTML colour or a
    # numbered word, such as one, two, three, four or five.
    # ===================================================================== #
    _ = use_this_as_replacement_hash # Our replacement Hash.
    rev = _[:rev] if _.is_a?(Hash) and _.has_key?(:rev)
    scanned_result.each {|this_numbered_word|
      # =================================================================== #
      # Since as of December 2021 we only accept Symbols here, hence
      # the .to_sym call next.
      # =================================================================== #
      this_numbered_word = this_numbered_word.to_sym
      i = i.dup if i.frozen?
      if is_this_a_HTML_colour?(this_numbered_word)
        use_this_regex =
          # =============================================================== #
          # We will simply re-use the regex-generator near the top of this
          # file here.
          # =============================================================== #
          ::Colours.return_main_regex_to_use_for_obtaining_the_full_content(this_numbered_word)
        # ================================================================= #
        # We must use .gsub!() because the colour-string may occur more
        # than once in the given String.
        # ================================================================= #
        begin
        if use_this_colour_for_the_default_colour == :"\e[0;37m" # Ad-hoc fix.
          i.gsub!(
            use_this_regex,
            ::Colours::HtmlColoursMethods.send(this_numbered_word.to_sym, "\\1") { :omit_end }+
            use_this_colour_for_the_default_colour.to_s
          )
        else
          i.gsub!(
            use_this_regex,
            ::Colours::HtmlColoursMethods.send(this_numbered_word.to_sym, "\\1") { :omit_end }+
            ::Colours::HtmlColoursMethods.send(use_this_colour_for_the_default_colour,'') { :omit_end }
          )
        end
        rescue NoMethodError => exception
          pp 'use_this_colour_for_the_default_colour was:'
          pp use_this_colour_for_the_default_colour
          pp exception
        end
      elsif _.has_key?(this_numbered_word) # This is for a numbered word, such as <one>.
        # ================================================================= #
        # === :default_colour
        # ================================================================= #
        if use_this_as_replacement_hash.has_key? :default_colour
          use_this_colour_for_the_default_colour = use_this_as_replacement_hash[:default_colour]
        end
        if use_this_as_replacement_hash.has_key? :rev
          use_this_colour_for_the_default_colour = use_this_as_replacement_hash[:rev]
        end
        replacement_colour = _[this_numbered_word] # This will become the new main colour.
        use_this_regex =
          # =============================================================== #
          # We will simply re-use the regex-generator near the top of this
          # file here.
          # =============================================================== #
          ::Colours.return_main_regex_to_use_for_obtaining_the_full_content(this_numbered_word)
        # ================================================================= #
        # We must use .gsub!() because the colour-string may occur more
        # than once in the given String.
        # ================================================================= #
        i.gsub!(
          use_this_regex,
          ::Colours.colourize(replacement_colour.to_sym, "\\1")+
          ::Colours::HtmlColoursMethods.send(use_this_colour_for_the_default_colour,'') { :omit_end }
        )
      end
    }
  # else # else we do not have to do anything.
  end
  @sanitized_line = i.dup # And assign it here.
  return @sanitized_line # Always return it here.
end

#resetObject

#

reset (reset tag)

#


104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 104

def reset
  # ======================================================================= #
  # === @hash_replacement_colours
  #
  # We need to specify a default replacement-Hash. The user can override
  # it, or individual elements.
  # ======================================================================= #
  @hash_replacement_colours = HASH_DEFAULT_REPLACEMENT_COLOURS
  # ======================================================================= #
  # === @sanitized_line
  # ======================================================================= #
  @sanitized_line = nil
  # ======================================================================= #
  # === @main_regex
  # ======================================================================= #
  @main_regex = main_regex?
end

#rev?Boolean

#

rev?

#

Returns:

  • (Boolean)


125
126
127
128
129
130
131
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 125

def rev?
  if @hash_replacement_colours.has_key?(:rev)
    return @hash_replacement_colours[:rev]
  else
    ::Colours.rev
  end
end

#runObject

#

run (run tag)

#


305
306
307
308
309
310
311
312
313
314
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 305

def run
  second_argument = second_argument?
  if second_argument and second_argument.is_a?(Hash)
    set_hash_replacement_colours(second_argument)
  end
  _ = first_argument?
  if _ and !_.nil?
    parse(_)
  end
end

#sanitized_line?Boolean Also known as: result?

#

sanitized_line?

#

Returns:

  • (Boolean)


143
144
145
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 143

def sanitized_line?
  @sanitized_line
end

#set_hash_replacement_colours(i) ⇒ Object Also known as: set_use_this_hash

#

set_hash_replacement_colours

#


171
172
173
# File 'lib/colours/replace_tokens_with_colour_code/replace_tokens_with_colour_code.rb', line 171

def set_hash_replacement_colours(i)
  @hash_replacement_colours = i
end