Class: Colours::ReplaceTokensWithColourCode
- Inherits:
-
Base
- Object
- Base
- Colours::ReplaceTokensWithColourCode
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, two: :lightseagreen, three: :mediumseagreen, four: :mediumorchid, five: :lightgreen, rev: ::Colours.rev, default_colour: ::Colours.rev }
Class Method Summary
collapse
-
.[](i = ARGV) ⇒ Object
# === Colours::ReplaceTokensWithColourCode[] ========================================================================= #.
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
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
)
if block_given?
yielded = yield
if yielded.is_a? Hash
do_update_the_main_hash_with_this_dataset(yielded)
end
end
case run_already
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>.
#
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?
#
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?
#
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
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, rev = nil )
rev = ::Colours.rev if rev.nil? if i and does_this_string_contain_a_special_token?(i)
use_this_colour_for_the_default_colour = rev.to_sym case use_this_as_replacement_hash
when :default,
:default_hash
use_this_as_replacement_hash = @hash_replacement_colours
end
scanned_result = i.scan(
@main_regex
).flatten
_ = use_this_as_replacement_hash rev = _[:rev] if _.is_a?(Hash) and _.has_key?(:rev)
scanned_result.each {|this_numbered_word|
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 =
::Colours.return_main_regex_to_use_for_obtaining_the_full_content(this_numbered_word)
begin
if use_this_colour_for_the_default_colour == :"\e[0;37m" 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) 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] use_this_regex =
::Colours.return_main_regex_to_use_for_obtaining_the_full_content(this_numbered_word)
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
}
end
@sanitized_line = i.dup return @sanitized_line end
|
#reset ⇒ Object
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 = HASH_DEFAULT_REPLACEMENT_COLOURS
@sanitized_line = nil
@main_regex = main_regex?
end
|
#rev? ⇒ 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
|
#run ⇒ Object
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?
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
|