pipetext 0.1.3 → 0.1.5

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.
data/lib/pipetext.rb CHANGED
@@ -1,809 +1,14 @@
1
- require_relative 'substitute_emoji_names.rb'
1
+ # frozen_string_literal: true
2
2
 
3
- module PipeText
4
-
5
- public
6
-
7
- def pipetext_init(box_mode=true, ampersand_mode=false)
8
- attributes = {
9
- 'pipe' => false, # Pipe already been found?
10
- 'repeat_pattern' => false, # Used by |<#>~repeat pattern~
11
- 'pattern' => String.new, # Used by |<#>~repeat pattern~ to capture
12
- 'pattern_escape' => false, # Has an escape \ already been found in front of this character?
13
- 'ampersand' => false, # Has an ampersand already been found in front of this character?
14
- 'ampersand_mode' => ampersand_mode, # Do we even process ampersands for background colors?
15
- 'blink' => false, # Is blink turned on?
16
- 'bold' => false,
17
- 'crossed_out' => false,
18
- 'faint' => false,
19
- 'found' => false, # At the end -- did we find a match?
20
- 'italic' => false,
21
- 'inverse' => false,
22
- 'underline' => false,
23
- 'box' => -1, # Default to |O (no boxes)
24
- 'box_mode' => box_mode,
25
- 'num' => 0, # Number of times to repeat pattern
26
- 'end_capture' => false, # Used to capture the end column number
27
- 'end' => 0, # Number which current denotes the end of the column
28
- 'emoji_capture' => false, # Used to capture emoji description
29
- 'emoji' => String.new, # Used to capture emoji description
30
- 'unicode_capture' => 0, # Used to capture Unicode using 6 character UTF-16 hex format
31
- 'unicode' => String.new,
32
- 'palette_capture' => 0, # Used to capture 8-bit color using 2 character hex format
33
- 'p' => String.new, # |p00 to |pFF
34
- 'color_capture' => 0, # Used to capture RGB color using #RRGGBB format
35
- 'r' => String.new,
36
- 'g' => String.new,
37
- 'b' => String.new,
38
- 'fg' => String.new, # Needed to restore after background change
39
- 'bg' => String.new # Needed to restore after foreground change
40
- }
41
- end
42
-
43
- def pipe(text, attributes)
44
- new_text = String.new
45
- text.chars.each do |character|
46
- process_character(character, new_text, attributes)
47
- end
48
- # Clean up in case we've captured something we didn't process yet
49
- if(attributes['color_capture'] > 0)
50
- emit_color(new_text, attributes)
51
- elsif(attributes['palette_capture'] > 0)
52
- emit_palette_color(new_text, attributes)
53
- elsif(attributes['unicode_capture'] > 0)
54
- emit_unicode(new_text, attributes)
55
- elsif(attributes['emoji_capture'] == true)
56
- new_text << "|[" + attributes['emoji']
57
- end
58
- return new_text
59
- end
60
-
61
- def pipetext(text, box_mode=true, ampersand_mode=false)
62
- pipe(text, pipetext_init(box_mode, ampersand_mode))
63
- end
64
-
65
- def write(text, box_mode=true, ampersand_mode=false)
66
- puts(pipetext(text, box_mode, ampersand_mode))
67
- end
68
-
69
- # Defaults to using & for background colors
70
- def paint(text, box_mode=true, ampersand_mode=true)
71
- puts(pipetext(text, box_mode, ampersand_mode))
72
- end
73
-
74
- def ignored_character(character, ignored_characters)
75
- ignored_characters.chars.each do |ignored|
76
- if(character == ignored)
77
- return true
78
- end
79
- end
80
- return false
81
- end
82
-
83
- # Match abbreviated text descriptions for emojis by default ignore case and punctuation
84
- # Allows for 'space anchoring' so you can use |[smi f w he e] as an abbreviation for
85
- # |[smiling face with heart-eyes]
86
- def abbreviated_match(input, match, case_match=false, ignored_characters=",.':-")
87
- if(!input || !match)
88
- return 0
89
- end
90
- count = 0
91
- offset = 0
92
- input.chars.each_with_index do |character, index|
93
- while(ignored_character(match[index + offset], ignored_characters))
94
- offset += 1
95
- end
96
- if(character == match[index + offset])
97
- count += 1
98
- elsif(case_match == false && character.downcase == match[index + offset].downcase)
99
- count += 1
100
- elsif(character == ' ' && match[index + offset..-1] =~ / /)
101
- count += 1
102
- while(match[index + offset] != character)
103
- offset += 1
104
- end
105
- else
106
- count = 0
107
- offset = 0
108
- break
109
- end
110
- end
111
- return count + offset
112
- end
113
-
114
- # This is not entirely accurate because of emojis
115
- def printable_length(string)
116
- length = 0
117
- escape = false
118
- string.chars.each do |character|
119
- if(character.ord == 27)
120
- escape = true
121
- elsif(character.ord >= 32)
122
- if(escape == true && character.ord == 109)
123
- escape = false
124
- elsif(escape == false)
125
- length += 1
126
- end
127
- end
128
- end
129
- return length
130
- end
131
-
132
- private
133
-
134
- def process_character(character, new_text, attributes)
135
- if(attributes['repeat_pattern'] == false)
136
- # Will still need to process character, first process incorrect formats
137
- if(attributes['color_capture'] > 0 && character !~ /[0-9,A-F,a-f]/)
138
- emit_color(new_text, attributes)
139
- elsif(attributes['palette_capture'] > 0 && character !~ /[0-9,A-F,a-f]/)
140
- emit_palette_color(new_text, attributes)
141
- elsif(attributes['unicode_capture'] > 0 && character !~ /[0-9,A-F,a-f,+]/ ||
142
- (attributes['unicode_capture'] != 1 && character == '+'))
143
- emit_unicode(new_text, attributes)
144
- end
145
- end
146
- if(attributes['end_capture'] == true && character !~ /[0-9]/)
147
- attributes['end'] = attributes['num']
148
- attributes['num'] = 0
149
- attributes['end_capture'] = false
150
- end
151
- if(attributes['end_capture'] == true && character =~ /[0-9]/)
152
- if(character == '0') # |10+
153
- if(attributes['num'] > 0)
154
- attributes['num'] *= 10
155
- end
156
- elsif(character >= '1' && character <= '9') # |1+ through |9+
157
- if(attributes['num'] > 0)
158
- attributes['num'] *= 10
159
- end
160
- attributes['num'] += character.to_i
161
- end
162
- elsif(character == '|' && attributes['repeat_pattern'] == false)
163
- process_pipe(character, new_text, attributes)
164
- elsif(character == '~' && attributes['pipe'] == true &&
165
- attributes['num'] > 0 && attributes['pattern_escape'] == false)
166
- process_repeat_pattern(character, new_text, attributes)
167
- elsif(attributes['repeat_pattern'] == true)
168
- capture_character_pattern(character, attributes)
169
- elsif(attributes['color_capture'] > 0 && character =~ /[0-9,A-F,a-f]/)
170
- capture_color(character, new_text, attributes)
171
- elsif(attributes['palette_capture'] > 0 && character =~ /[0-9,A-F,a-f]/)
172
- capture_palette_color(character, new_text, attributes)
173
- elsif(attributes['emoji_capture'] == true)
174
- if(character == ']')
175
- emit_emoji(new_text, attributes)
176
- else
177
- attributes['emoji'] << character
178
- end
179
- elsif(attributes['unicode_capture'] == 1 && character == '+') # Skip
180
- return
181
- elsif(attributes['unicode_capture'] > 0 && character =~ /[0-9,A-F,a-f]/)
182
- capture_unicode(character, new_text, attributes)
183
- elsif(attributes['pipe'] == true &&
184
- attributes['repeat_pattern'] == false &&
185
- attributes['pattern_escape'] == true)
186
- process_escaped_character(character, new_text, attributes)
187
- elsif(character == '&' && attributes['ampersand_mode'] == true &&
188
- attributes['pipe'] == false)
189
- process_ampersand(character, new_text, attributes)
190
- elsif(attributes['pipe'] == true)
191
- process_piped_character(character, new_text, attributes)
192
- elsif(attributes['ampersand'] == true)
193
- process_ampersanded_character(character, new_text, attributes)
194
- elsif(attributes['box'] == 0)
195
- new_text << process_box_zero_replace(character)
196
- elsif(attributes['box'] == 1)
197
- new_text << process_box_one_replace(character)
198
- elsif(attributes['box'] == 2)
199
- new_text << process_box_two_replace(character)
200
- else
201
- new_text << character
202
- end
203
- end
204
-
205
- def emit_color(new_text, attributes)
206
- r = attributes['r'].to_i(16).to_s
207
- g = attributes['g'].to_i(16).to_s
208
- b = attributes['b'].to_i(16).to_s
209
- if(attributes['ampersand'] == true) # Background Color
210
- new_text << "\e[48;2;#{r};#{g};#{b}m"
211
- attributes['ampersand'] = false
212
- else # Foreground Color
213
- new_text << "\e[38;2;#{r};#{g};#{b}m"
214
- end
215
- attributes['color_capture'] = 0
216
- attributes['r'] = String.new
217
- attributes['g'] = String.new
218
- attributes['b'] = String.new
219
- end
220
-
221
- def emit_palette_color(new_text, attributes)
222
- p = attributes['p'].to_i(16).to_s
223
- if(attributes['ampersand'] == true) # Background Color
224
- new_text << "\e[48;5;#{p}m"
225
- attributes['ampersand'] = false
226
- else # Foreground Color
227
- new_text << "\e[38;5;#{p}m"
228
- end
229
- attributes['palette_capture'] = 0
230
- attributes['p'] = String.new
231
- end
232
-
233
- def emit_unicode(new_text, attributes)
234
- new_text << [attributes['unicode'].to_i(16)].pack('U*')
235
- attributes['unicode_capture'] = 0
236
- attributes['unicode'] = String.new
237
- end
238
-
239
- def emit_emoji(new_text, attributes)
240
- emoji = String.new
241
- match_length = 0
242
- value_length = 0
243
- $substitute_emoji_names.each do |key, value|
244
- if(attributes['emoji'] == key) # Use the most precise match first
245
- emoji = value
246
- break
247
- elsif(attributes['emoji'].length <= key.length) # Otherwise use shortest match
248
- match = abbreviated_match(attributes['emoji'], key)
249
- if(match == 0 && key =~ /-/)
250
- match = abbreviated_match(attributes['emoji'], key.sub(/-/, ' '))
251
- end
252
- if(match > 0 && (match <= match_length || match_length == 0) &&
253
- (value.length <= value_length || value_length == 0))
254
- emoji = value
255
- match_length = match
256
- value_length = value.length
257
- end
258
- end
259
- end
260
- if(emoji == String.new) # No match, put copy input
261
- new_text << "|[" + attributes['emoji'] + "]"
262
- else
263
- emoji.split(/\|U/).each do |e|
264
- if(e != "")
265
- attributes['unicode'] = e.sub(/^\+?/, '')
266
- emit_unicode(new_text, attributes)
267
- end
268
- end
269
- attributes['emoji'] = String.new
270
- end
271
- attributes['emoji_capture'] = false
272
- end
273
-
274
- def process_pipe(character, new_text, attributes)
275
- if(attributes['pipe'] == true && attributes['num'] == 0) # ||
276
- attributes['pipe'] = false
277
- new_text << character
278
- else
279
- attributes['pipe'] = true
280
- end
281
- end
282
-
283
- def process_repeat_pattern(character, new_text, attributes)
284
- if(attributes['repeat_pattern'] == true) # ~ at end of |5~Repeat 5 times~
285
- attributes['num'].times do
286
- new_text << pipetext(attributes['pattern'], attributes['box_mode'], attributes['ampersand_mode'])
287
- end
288
- attributes['num'] = 0
289
- attributes['pipe'] = false
290
- attributes['pattern_escape'] = false
291
- attributes['repeat_pattern'] = false
292
- attributes['pattern'] = String.new
293
- else # ~ after number in |5~Repeat 5 times~
294
- attributes['repeat_pattern'] = true
295
- end
296
- end
297
-
298
- def escape_fix(text)
299
- text.gsub(/\\a/, "\a").gsub(/\\b/, "\b").gsub(/\\e/, "\e")
300
- .gsub(/\\f/, "\f").gsub(/\\n/, "\n").gsub(/\\r/, "\r")
301
- .gsub(/\\t/, "\t").gsub(/\\v/, "\v").gsub(/\\~/, '~')
302
- end
303
-
304
- def process_escaped_character(character, new_text, attributes)
305
- if(attributes['num'] > 0)
306
- attributes['num'].times do
307
- new_text << escape_fix("\\#{character}")
308
- end
309
- else
310
- new_text << escape_fix("\\#{character}")
311
- end
312
- attributes['num'] = 0
313
- attributes['pipe'] = false
314
- attributes['pattern_escape'] = false
315
- attributes['pattern'] = String.new
316
- attributes['repeat_pattern'] = false
317
- end
318
-
319
- def capture_color(character, new_text, attributes)
320
- if(character =~ /[0-9,A-F,a-f]/)
321
- if(attributes['color_capture'] <= 2)
322
- attributes['r'] << character
323
- elsif(attributes['color_capture'] <= 4)
324
- attributes['g'] << character
325
- elsif(attributes['color_capture'] <= 6)
326
- attributes['b'] << character
327
- end
328
- if(attributes['color_capture'] == 6)
329
- emit_color(new_text, attributes)
330
- else
331
- attributes['color_capture'] += 1
332
- end
333
- end
334
- end
335
-
336
- def capture_character_pattern(character, attributes)
337
- if(character == '\\')
338
- attributes['pattern_escape'] = true
339
- else
340
- if(attributes['pattern_escape'] == true)
341
- attributes['pattern'] << "\\#{character}"
342
- attributes['pattern_escape'] = false
343
- else
344
- attributes['pattern'] << character
345
- end
346
- end
347
- end
348
-
349
- def capture_palette_color(character, new_text, attributes)
350
- if(character =~ /[0-9,A-F,a-f]/)
351
- if(attributes['palette_capture'] <= 2)
352
- attributes['p'] << character
353
- end
354
- if(attributes['palette_capture'] == 2)
355
- emit_palette_color(new_text, attributes)
356
- else
357
- attributes['palette_capture'] += 1
358
- end
359
- end
360
- end
361
-
362
- def capture_unicode(character, new_text, attributes)
363
- if(character =~ /[0-9,A-F,a-f]/)
364
- if(attributes['unicode_capture'] <= 6)
365
- attributes['unicode'] << character
366
- end
367
- if(attributes['unicode_capture'] == 6)
368
- emit_unicode(new_text, attributes)
369
- else
370
- attributes['unicode_capture'] += 1
371
- end
372
- end
373
- end
374
-
375
- def process_ampersand(character, new_text, attributes)
376
- if(attributes['ampersand'] == true) # &&
377
- attributes['ampersand'] = false
378
- new_text << character
379
- else
380
- attributes['ampersand'] = true
381
- end
382
- end
383
-
384
- def update_attributes(new_text, attributes)
385
- if(attributes['bold'] == true)
386
- new_text << "\e[1m"
387
- end
388
- if(attributes['faint'] == true)
389
- new_text << "\e[2m"
390
- end
391
- if(attributes['italic'] == true)
392
- new_text << "\e[3m"
393
- end
394
- if(attributes['underline'] == true)
395
- new_text << "\e[4m"
396
- end
397
- if(attributes['blink'] == true)
398
- new_text << "\e[5m"
399
- end
400
- if(attributes['inverse'] == true)
401
- new_text << "\e[7m"
402
- end
403
- if(attributes['crossed_out'] == true)
404
- new_text << "\e[9m"
405
- end
406
- end
407
-
408
- def process_piped_character(character, new_text, attributes)
409
- attributes['found'] = true # Assume we will find the next character
410
- if(attributes['num'] == 0) # We are not in repeat character mode
411
- case character
412
- when '&' # |& - Toggle & on/off for Background Colors
413
- if(attributes['ampersand_mode'] == true)
414
- attributes['ampersand_mode'] = false
415
- else
416
- attributes['ampersand_mode'] = true
417
- end
418
- attributes['pipe'] = false
419
- when '!' # |! - Clear screen
420
- new_text << "\e[H\e[J"
421
- when '+' # |+ - Bold
422
- if(attributes['bold'] == false)
423
- new_text << "\e[1m"
424
- attributes['bold'] = true
425
- else
426
- new_text << "\e[22m"
427
- attributes['bold'] = false
428
- end
429
- when '.' # |. - Faint / Dim
430
- if(attributes['faint'] == false)
431
- new_text << "\e[2m"
432
- attributes['faint'] = true
433
- else
434
- new_text << "\e[22m"
435
- attributes['faint'] = false
436
- end
437
- when '~' # |~ - Italic
438
- if(attributes['italic'] == false)
439
- new_text << "\e[3m"
440
- attributes['italic'] = true
441
- else
442
- new_text << "\e[23m"
443
- attributes['italic'] = false
444
- end
445
- when '_' # |_ - Underline
446
- if(attributes['underline'] == false)
447
- new_text << "\e[4m"
448
- attributes['underline'] = true
449
- else
450
- new_text << "\e[24m"
451
- attributes['underline'] = false
452
- end
453
- when '@' # |@ - Blink
454
- if(attributes['blink'] == false)
455
- new_text << "\e[5m"
456
- attributes['blink'] = true
457
- else
458
- new_text << "\e[25m"
459
- attributes['blink'] = false
460
- end
461
- when 'i', 'I' # |i - Inverse
462
- if(attributes['inverse'] == false)
463
- new_text << "\e[7m"
464
- attributes['inverse'] = true
465
- else
466
- new_text << "\e[27m"
467
- attributes['inverse'] = false
468
- end
469
- when 'x', 'X' # |x - Crossed Out
470
- if(attributes['crossed_out'] == false)
471
- new_text << "\e[9m"
472
- attributes['crossed_out'] = true
473
- else
474
- new_text << "\e[29m"
475
- attributes['crossed_out'] = false
476
- end
477
- when '#' # |#RRGGBB
478
- attributes['color_capture'] = 1
479
- when 'P', 'p' # |P or |p - 2 character hex format color (256 colors)
480
- attributes['palette_capture'] = 1
481
- when 'U', 'u' # |U or |u - Unicode 6 character hex format
482
- attributes['unicode_capture'] = 1
483
- when 'K', 'k' # |K or |k - Foreground text black
484
- attributes['fg'] = "\e[30m"
485
- new_text << "\e[0;30m"
486
- attributes['bold'] = false
487
- update_attributes(new_text, attributes)
488
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
489
- when 'S', 's' # |S or |s - Foreground text smoke
490
- attributes['fg'] = "\e[1;30m"
491
- new_text << "\e[1;30m"
492
- attributes['bold'] = false
493
- update_attributes(new_text, attributes)
494
- attributes['bold'] = true
495
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
496
- when 'r' # |r - Foreground text red
497
- attributes['fg'] = "\e[31m"
498
- new_text << "\e[0;31m"
499
- attributes['bold'] = false
500
- update_attributes(new_text, attributes)
501
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
502
- when 'R' # |R - Foreground text bright red
503
- attributes['fg'] = "\e[1;31m"
504
- new_text << "\e[1;31m"
505
- attributes['bold'] = false
506
- update_attributes(new_text, attributes)
507
- attributes['bold'] = true
508
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
509
- when 'g' # |g - Foreground text green
510
- attributes['fg'] = "\e[32m"
511
- new_text << "\e[0;32m"
512
- attributes['bold'] = false
513
- update_attributes(new_text, attributes)
514
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
515
- when 'G' # |G - Foreground text bright green
516
- attributes['fg'] = "\e[1;32m"
517
- new_text << "\e[1;32m"
518
- attributes['bold'] = false
519
- update_attributes(new_text, attributes)
520
- attributes['bold'] = true
521
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
522
- when 'y' # |y - Foreground text yellow (brown)
523
- attributes['fg'] = "\e[33m"
524
- new_text << "\e[0;33m"
525
- attributes['bold'] = false
526
- update_attributes(new_text, attributes)
527
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
528
- when 'Y' # |Y - Foreground text bright yellow
529
- attributes['fg'] = "\e[1;33m"
530
- new_text << "\e[1;33m"
531
- attributes['bold'] = false
532
- update_attributes(new_text, attributes)
533
- attributes['bold'] = true
534
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
535
- when 'b' # |b - Foreground text blue
536
- attributes['fg'] = "\e[34m"
537
- new_text << "\e[0;34m"
538
- attributes['bold'] = false
539
- update_attributes(new_text, attributes)
540
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
541
- when 'B' # |B - Foreground text bright blue
542
- attributes['fg'] = "\e[1;34m"
543
- new_text << "\e[1;34m"
544
- attributes['bold'] = false
545
- update_attributes(new_text, attributes)
546
- attributes['bold'] = true
547
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
548
- when 'm' # |m - Foreground text magenta
549
- attributes['fg'] = "\e[35m"
550
- new_text << "\e[0;35m"
551
- attributes['bold'] = false
552
- update_attributes(new_text, attributes)
553
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
554
- when 'M' # |M - Foreground text bright magenta
555
- attributes['fg'] = "\e[1;35m"
556
- new_text << "\e[1;35m"
557
- attributes['bold'] = false
558
- update_attributes(new_text, attributes)
559
- attributes['bold'] = true
560
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
561
- when 'c' # |c - Foreground text cyan
562
- attributes['fg'] = "\e[36m"
563
- new_text << "\e[0;36m"
564
- attributes['bold'] = false
565
- update_attributes(new_text, attributes)
566
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
567
- when 'C' # |C - Foreground text bright cyan
568
- attributes['fg'] = "\e[1;36m"
569
- new_text << "\e[1;36m"
570
- attributes['bold'] = false
571
- update_attributes(new_text, attributes)
572
- attributes['bold'] = true
573
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
574
- when 'W', 'w' # |W or |w - Foreground text white
575
- attributes['fg'] = "\e[1;37m"
576
- new_text << "\e[1;37m"
577
- attributes['bold'] = false
578
- update_attributes(new_text, attributes)
579
- attributes['bold'] = true
580
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
581
- when 'N', 'n' # |N or |n - Foreground text normal
582
- attributes['fg'] = ""
583
- new_text << "\e[0;37m"
584
- attributes['bold'] = false
585
- update_attributes(new_text, attributes)
586
- new_text << attributes['bg'] == "\e[0m" ? "" : attributes['bg']
587
- when 'O' # |O - Box mode off
588
- attributes['box'] = -1
589
- when 'o' # |o - Box mode 0
590
- attributes['box'] = 0
591
- when '-' # |- - Box mode 1
592
- if(attributes['box_mode'] == true)
593
- attributes['box'] = 1
594
- else # We didn't find the next character
595
- attributes['found'] = false
596
- end
597
- when '=' # |= - Box mode 2
598
- if(attributes['box_mode'] == true)
599
- attributes['box'] = 2
600
- else # We didn't find the next character
601
- attributes['found'] = false
602
- end
603
- when ';' # extend to end column with spaces
604
- if(new_text =~ /\n?(.*)\Z/)
605
- spaces = attributes['end'] - printable_length($1)
606
- else
607
- spaces = attributes['end']
608
- end
609
- spaces.times do
610
- new_text << " "
611
- end
612
- when ']' # |]0-9 - end column number
613
- attributes['end_capture'] = true
614
- when '[' # |[emoji]
615
- attributes['emoji_capture'] = true
616
- when '\\' # |\ - Escape mode
617
- attributes['pattern_escape'] = true
618
- else # We didn't find the next character
619
- attributes['found'] = false
620
- end
621
- elsif(character == '\\')
622
- attributes['pattern_escape'] = true
623
- else # We didn't find the next character
624
- attributes['found'] = false
625
- end
626
- if(attributes['found'] == false)
627
- if(character == '0') # |10+
628
- if(attributes['num'] > 0)
629
- attributes['num'] *= 10
630
- end
631
- elsif(character >= '1' && character <= '9') # |1+ through |9+
632
- if(attributes['num'] > 0)
633
- attributes['num'] *= 10
634
- end
635
- attributes['num'] += character.to_i
636
- else
637
- if(attributes['num'] <= 0) # No replacement found
638
- new_text << '|' + character
639
- else # Repeat number replacement found
640
- if(attributes['box'] == 1)
641
- attributes['num'].times do
642
- new_text << process_box_one_replace(character)
643
- end
644
- attributes['num'] = 0
645
- elsif(attributes['box'] == 2)
646
- attributes['num'].times do
647
- new_text << process_box_two_replace(character)
648
- end
649
- attributes['num'] = 0
650
- else
651
- attributes['num'].times do
652
- new_text << character
653
- end
654
- attributes['num'] = 0
655
- end
656
- end
657
- attributes['pipe'] = false
658
- end
659
- elsif(attributes['pattern_escape'] == false)
660
- attributes['pipe'] = false
661
- end
662
- end
663
-
664
- def process_ampersanded_character(character, new_text, attributes)
665
- case character
666
- when '#' # &#RRGGBB
667
- attributes['color_capture'] = 1
668
- return
669
- when 'P', 'p' # |P or |p - 2 character hex format color (256 colors)
670
- attributes['palette_capture'] = 1
671
- return
672
- when 'W', 'w' # &W or &w - Background white
673
- new_text << "\e[0;47m"
674
- attributes['bg'] = "\e[47m"
675
- update_attributes(new_text, attributes)
676
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
677
- when 'C', 'c' # &C or &c - Background cyan
678
- new_text << "\e[0;46m"
679
- attributes['bg'] = "\e[46m"
680
- update_attributes(new_text, attributes)
681
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
682
- when 'M', 'm' # &M or &m - Background magenta
683
- new_text << "\e[0;45m"
684
- attributes['bg'] = "\e[45m"
685
- update_attributes(new_text, attributes)
686
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
687
- when 'B', 'b' # &B or &b - Background blue
688
- new_text << "\e[0;44m"
689
- attributes['bg'] = "\e[44m"
690
- update_attributes(new_text, attributes)
691
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
692
- when 'Y', 'y' # &Y or &y - Background yellow (brown)
693
- new_text << "\e[0;43m"
694
- attributes['bg'] = "\e[43m"
695
- update_attributes(new_text, attributes)
696
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
697
- when 'G', 'g' # &G or &g - Background green
698
- new_text << "\e[0;42m"
699
- attributes['bg'] = "\e[42m"
700
- update_attributes(new_text, attributes)
701
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
702
- when 'R', 'r' # &R or &r - Background red
703
- new_text << "\e[0;41m"
704
- attributes['bg'] ="\e[41m"
705
- update_attributes(new_text, attributes)
706
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
707
- when 'S', 's', 'K', 'k' # &S, &s, &K, &k - Background black/smoke
708
- new_text << "\e[0;40m"
709
- attributes['bg'] = "\e[40m"
710
- update_attributes(new_text, attributes)
711
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
712
- when 'N', 'n' # &N or &n - Background normal
713
- new_text << "\e[0m"
714
- attributes['bg'] = ""
715
- update_attributes(new_text, attributes)
716
- new_text << attributes['fg'] == "\e[0m" ? "" : attributes['fg']
717
- else
718
- new_text << '&' + character
719
- end
720
- attributes['ampersand'] = false
721
- end
722
-
723
- def process_box_zero_replace(character)
724
- case character
725
- when '['
726
- '+'
727
- when ']'
728
- '+'
729
- when '-'
730
- '-'
731
- when '!'
732
- '|'
733
- when '>'
734
- '+'
735
- when '<'
736
- '+'
737
- when '+'
738
- '+'
739
- when '{'
740
- '+'
741
- when '}'
742
- '+'
743
- when 'v'
744
- '+'
745
- when '^'
746
- '+'
747
- else
748
- character
749
- end
750
- end
751
-
752
- def process_box_one_replace(character)
753
- case character
754
- when '['
755
- '┌'
756
- when ']'
757
- '┐'
758
- when '-'
759
- '─'
760
- when '!'
761
- '│'
762
- when '>'
763
- '├'
764
- when '<'
765
- '┤'
766
- when '+'
767
- '┼'
768
- when '{'
769
- '└'
770
- when '}'
771
- '┘'
772
- when 'v'
773
- '┬'
774
- when '^'
775
- '┴'
776
- else
777
- character
778
- end
779
- end
780
-
781
- def process_box_two_replace(character)
782
- case character
783
- when '['
784
- '╔'
785
- when ']'
786
- '╗'
787
- when '-'
788
- '═'
789
- when '!'
790
- '║'
791
- when '>'
792
- '╠'
793
- when '<'
794
- '╣'
795
- when '+'
796
- '╬'
797
- when '{'
798
- '╚'
799
- when '}'
800
- '╝'
801
- when 'v'
802
- '╦'
803
- when '^'
804
- '╩'
805
- else
806
- character
3
+ # Allow older versions of Ruby to respond to the require_relative method
4
+ if(!Kernel.respond_to?(:require_relative))
5
+ module Kernel
6
+ def require_relative(path)
7
+ require File.join(File.dirname(caller[0]), path.to_str)
807
8
  end
808
9
  end
809
10
  end
11
+
12
+ require_relative 'pipetext/version.rb'
13
+ require_relative 'pipetext/pipetext.rb'
14
+ require_relative 'pipetext/substitute_emoji_names.rb'