filter_rename 1.0.0 → 1.2.0

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.
@@ -1,596 +1,1045 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FilterRename
4
-
5
4
  module Filters
6
-
7
5
  class Select < FilterBase
8
- def self.hint; 'Select the target where apply further transformations'; end
9
- def self.params; 'name|ext|folder|...'; end
6
+ def self.hint
7
+ "Select the target where apply further transformations"
8
+ end
9
+
10
+ def self.params
11
+ "name|ext|folder|..."
12
+ end
10
13
 
11
14
  def filter(params)
12
- raise InvalidTarget, params[0] unless has_target? params[0].to_sym
15
+ raise InvalidTarget, params[0] unless target? params[0].to_sym
16
+
13
17
  set_config :target, params[0].to_sym
14
18
  end
15
19
  end
16
20
 
17
21
  class Config < FilterBase
18
- def self.hint; 'Set config PARAM to VALUE'; end
19
- def self.params; 'PARAM:VALUE[,PARAMS2:VALUE2,...]'; end
22
+ def self.hint
23
+ "Set config PARAM to VALUE"
24
+ end
25
+
26
+ def self.params
27
+ "PARAM:VALUE[,PARAMS2:VALUE2,...]"
28
+ end
20
29
 
21
30
  def filter(params)
22
31
  params.each do |par|
23
- set_config(par.split(':')[0], par.split(':')[1])
32
+ set_config(par.split(":")[0], par.split(":")[1])
24
33
  end
25
34
  end
26
35
  end
27
36
 
28
37
  #------------------------
29
38
 
30
-
31
39
  class AddNumber < FilterNumber
32
- def self.hint; 'Add NUM to the NTH number'; end
33
- def self.params; 'NTH,NUM'; end
40
+ def self.hint
41
+ "Add NUM to the NTH number"
42
+ end
34
43
 
35
- def filtered_number(num, params, param_num)
44
+ def self.params
45
+ "NTH,NUM"
46
+ end
47
+
48
+ def filtered_number(num, _param_num)
36
49
  num.to_i + params[1].to_i
37
50
  end
38
51
  end
39
52
 
53
+ class AddNumberFrom < FilterNumber
54
+ def self.hint
55
+ "Add the number from TARGET to the NTH"
56
+ end
57
+
58
+ def self.params
59
+ "NTH,TARGET"
60
+ end
61
+
62
+ def filtered_number(num, _param_num)
63
+ num.to_i + get_string(params[1]).to_i
64
+ end
65
+ end
40
66
 
41
67
  class Append < FilterBase
42
- def self.hint; 'Append the TEXT to the current target'; end
43
- def self.params; 'TEXT'; end
68
+ def self.hint
69
+ "Append the TEXT to the current target"
70
+ end
71
+
72
+ def self.params
73
+ "TEXT"
74
+ end
44
75
 
45
76
  def filter(params)
46
- super "#{get_string}#{params[0]}"
77
+ super("#{get_string}#{params[0]}")
47
78
  end
48
79
  end
49
80
 
50
-
51
81
  class AppendFrom < FilterBase
52
- def self.hint; 'Append the text from TARGET'; end
53
- def self.params; 'TARGET'; end
82
+ def self.hint
83
+ "Append the text from TARGET"
84
+ end
85
+
86
+ def self.params
87
+ "TARGET"
88
+ end
54
89
 
55
90
  def filter(params)
56
- super "#{get_string}#{get_string(params[0])}"
91
+ super("#{get_string}#{get_string(params[0])}")
57
92
  end
58
93
  end
59
94
 
60
-
61
95
  class AppendAsWordTo < FilterBase
62
- def self.hint; 'Append the TEXT to TARGET as a word'; end
63
- def self.params; 'TEXT,TARGET'; end
96
+ def self.hint
97
+ "Append the TEXT to TARGET as a word"
98
+ end
99
+
100
+ def self.params
101
+ "TEXT,TARGET"
102
+ end
64
103
 
65
104
  def filter(params)
66
105
  ws = get_config(:word_separator)
67
106
  set_string(get_string(params[1]).to_s.split(ws).push(params[0]).join(ws), params[1])
68
- super get_string
107
+ super(get_string)
69
108
  end
70
109
  end
71
110
 
72
-
73
111
  class AppendTo < FilterBase
74
- def self.hint; 'Append the TEXT to TARGET'; end
75
- def self.params; 'TEXT,TARGET'; end
112
+ def self.hint
113
+ "Append the TEXT to TARGET"
114
+ end
115
+
116
+ def self.params
117
+ "TEXT,TARGET"
118
+ end
76
119
 
77
120
  def filter(params)
78
121
  set_string(get_string(params[1]).to_s + params[0], params[1])
79
- super get_string
122
+ super(get_string)
80
123
  end
81
124
  end
82
125
 
83
-
84
126
  class AppendNumberTo < FilterNumber
85
- def self.hint; 'Append the NTH number to TARGET'; end
86
- def self.params; 'NTH,TARGET'; end
127
+ def self.hint
128
+ "Append the NTH number to TARGET"
129
+ end
87
130
 
88
- def filtered_number(num, params, param_num)
131
+ def self.params
132
+ "NTH,TARGET"
133
+ end
134
+
135
+ def self_targeted?
136
+ params[-1] == current_target
137
+ end
138
+
139
+ def filtered_number(num, _param_num)
89
140
  str = get_string(params[1])
90
141
  set_string("#{str}#{num}", params[1])
91
142
  num
92
143
  end
93
144
  end
94
145
 
95
-
96
146
  class AppendToNumber < FilterNumber
97
- def self.hint; 'Append the TEXT to the NTH number'; end
98
- def self.params; 'NTH,TEXT'; end
147
+ def self.hint
148
+ "Append the TEXT to the NTH number"
149
+ end
150
+
151
+ def self.params
152
+ "NTH,TEXT"
153
+ end
99
154
 
100
- def filtered_number(num, params, param_num)
155
+ def filtered_number(num, _param_num)
101
156
  "#{num}#{params[1]}"
102
157
  end
103
158
  end
104
159
 
105
-
106
160
  class AppendToWord < FilterWord
107
- def self.hint; 'Append the TEXT to the NTH word'; end
108
- def self.params; 'NTH,TEXT'; end
161
+ def self.hint
162
+ "Append the TEXT to the NTH word"
163
+ end
164
+
165
+ def self.params
166
+ "NTH,TEXT"
167
+ end
109
168
 
110
- def filtered_word(word, params, param_num)
169
+ def filtered_word(word, _param_num)
111
170
  word + params[1]
112
171
  end
113
172
  end
114
173
 
115
-
116
174
  class AppendWordFrom < FilterWord
117
- def self.hint; 'Append the NTH word from TARGET'; end
118
- def self.params; 'NTH,TARGET'; end
175
+ def self.hint
176
+ "Append the NTH word from TARGET"
177
+ end
119
178
 
120
- def filter(params)
121
- word = get_string(params[1]).split(ws)
122
- idx = word_idx(params[0], word)
123
- set_string [get_string, word[idx]].join(ws)
179
+ def self.params
180
+ "NTH,TARGET"
181
+ end
182
+
183
+ def string_to_loop
184
+ get_string(params[1])
185
+ end
186
+
187
+ def self_targeted?
188
+ true
124
189
  end
125
- end
126
190
 
191
+ def filtered_word(word, _param_num)
192
+ set_string([get_string, word].join(ws))
193
+ word
194
+ end
195
+ end
127
196
 
128
197
  class AppendWordTo < FilterWord
129
- def self.hint; 'Append the NTH word to TARGET'; end
130
- def self.params; 'NTH,TARGET'; end
198
+ def self.hint
199
+ "Append the NTH word to TARGET"
200
+ end
131
201
 
132
- def filtered_word(word, params, param_num)
202
+ def self.params
203
+ "NTH,TARGET"
204
+ end
205
+
206
+ def self_targeted?
207
+ params[-1] == current_target
208
+ end
209
+
210
+ def filtered_word(word, _param_num)
133
211
  set_string([get_string(params[1]), word].join(ws), params[1])
134
212
  word
135
213
  end
136
214
  end
137
215
 
138
-
139
216
  class Capitalize < FilterBase
140
- def self.hint; 'Capitalize each word'; end
141
- def self.params; nil; end
217
+ def self.hint
218
+ "Capitalize each word"
219
+ end
142
220
 
143
- def filter(params)
221
+ def self.params
222
+ nil
223
+ end
224
+
225
+ def filter(_params)
144
226
  ws = get_config(:word_separator)
145
- super (get_string.split(ws).map(&:capitalize)).join(ws)
227
+ super(get_string.split(ws).map(&:capitalize).join(ws))
146
228
  end
147
229
  end
148
230
 
231
+ class CapitalizeWord < FilterWord
232
+ def self.hint
233
+ "Capitalize the NTH word"
234
+ end
235
+
236
+ def self.params
237
+ "NTH"
238
+ end
239
+
240
+ def filtered_word(word, _param_num)
241
+ word.capitalize
242
+ end
243
+ end
149
244
 
150
245
  class CopyFrom < FilterBase
151
- def self.hint; 'Copy the text from TARGET'; end
152
- def self.params; 'TARGET'; end
246
+ def self.hint
247
+ "Copy the text from TARGET"
248
+ end
249
+
250
+ def self.params
251
+ "TARGET"
252
+ end
153
253
 
154
254
  def filter(params)
155
- super get_string(params[0]).to_s
255
+ super(get_string(params[0]).to_s)
156
256
  end
157
257
  end
158
258
 
159
-
160
259
  class CopyNumberTo < FilterNumber
161
- def self.hint; 'Move the NTH number to TARGET'; end
162
- def self.params; 'NTH,TARGET'; end
260
+ def self.hint
261
+ "Copy the NTH number to TARGET"
262
+ end
263
+
264
+ def self.params
265
+ "NTH,TARGET"
266
+ end
163
267
 
164
- def filtered_number(num, params, param_num)
268
+ def self_targeted?
269
+ params[-1] == current_target
270
+ end
271
+
272
+ def filtered_number(num, _param_num)
165
273
  set_string(num, params[1])
166
274
  num
167
275
  end
168
276
  end
169
277
 
278
+ class CopyOccurrenceTo < FilterOccurrence
279
+ def self.hint
280
+ "Copy the NTH occurrence of REGEX to TARGET"
281
+ end
170
282
 
171
- class CopyTo < FilterBase
172
- def self.hint; 'Copy the text selected by REGEX to TARGET'; end
173
- def self.params; 'REGEX,TARGET'; end
283
+ def self.params
284
+ "NTH,REGEX,TARGET"
285
+ end
174
286
 
175
- def filter(params)
176
- set_string(get_string.scan(Regexp.new(wrap_regex(params[0]), get_config(:ignore_case).to_boolean)).pop.to_a.pop, params[1])
177
- super get_string
287
+ def self_targeted?
288
+ params[-1] == current_target
289
+ end
290
+
291
+ def filtered_occurrence(occurrence, _param_num)
292
+ set_string(occurrence, params[-1])
293
+ occurrence
178
294
  end
179
295
  end
180
296
 
297
+ class CopyTo < FilterRegExp
298
+ def self.hint
299
+ "Copy the text selected by REGEX to TARGET"
300
+ end
301
+
302
+ def self.params
303
+ "REGEX,TARGET"
304
+ end
305
+
306
+ def filtered_regexp(matches, params)
307
+ if matches.length > 2
308
+ matches[1..-1].each_with_index { |x, i| set_string(x, "#{params[1]}_#{i.next}") }
309
+ else
310
+ set_string(matches[1], params[1])
311
+ end
312
+ matches[0]
313
+ end
314
+ end
181
315
 
182
316
  class CopyWord < FilterWord
183
- def self.hint; 'Copy the NTH1 word to the NTH2 place'; end
184
- def self.params; 'NTH1,NTH2'; end
317
+ def self.hint
318
+ "Copy the NTH1 word to the NTH2 place"
319
+ end
185
320
 
186
- def indexed_params; 2; end
321
+ def self.params
322
+ "NTH1,NTH2"
323
+ end
324
+
325
+ def indexed_params
326
+ 2
327
+ end
187
328
 
188
- def filtered_word(word, params, param_num)
329
+ def filtered_word(word, param_num)
189
330
  case param_num
190
331
  when 1
191
332
  @word = word
192
- when 2
193
- word = @word + ws + word
333
+ when params_expanded.length
334
+ word = [word, @word].join(ws)
335
+ else
336
+ @word = [@word, word].join(ws)
194
337
  end
195
338
 
196
339
  word
197
340
  end
198
341
  end
199
342
 
343
+ class Delete < FilterRegExp
344
+ def self.hint
345
+ "Remove the text matching REGEX"
346
+ end
200
347
 
201
- class Delete < FilterBase
202
- def self.hint; 'Remove the text matching REGEX'; end
203
- def self.params; 'REGEX1[,REGEX2,...]'; end
348
+ def self.params
349
+ "REGEX"
350
+ end
204
351
 
205
- def filter(params)
206
- params.each do |par|
207
- super get_string.gsub(Regexp.new(par, get_config(:ignore_case).to_boolean), '')
208
- end
352
+ def filtered_regexp(_matches, _params)
353
+ ""
209
354
  end
210
355
  end
211
356
 
212
-
213
357
  class DeleteNumber < FilterNumber
214
- def self.hint; 'Remove the NTH number'; end
215
- def self.params; 'NTH'; end
358
+ def self.hint
359
+ "Remove the NTH number"
360
+ end
216
361
 
217
- def filtered_number(num, params, param_num)
218
- ''
362
+ def self.params
363
+ "NTH"
364
+ end
365
+
366
+ def filtered_number(_num, _param_num)
367
+ ""
219
368
  end
220
369
  end
221
370
 
371
+ class DeleteOccurrence < FilterOccurrence
372
+ def self.hint
373
+ "Delete the NTH occurrence of REGEXP"
374
+ end
222
375
 
223
- class DeleteWord < FilterWord
224
- def self.hint; 'Remove the NTH word'; end
225
- def self.params; 'NTH'; end
376
+ def self.params
377
+ "NTH,REGEXP"
378
+ end
226
379
 
227
- def filtered_word(word, params, param_num)
380
+ def filtered_occurrence(_occurrence, _param_num)
228
381
  nil
229
382
  end
230
383
  end
231
384
 
385
+ class DeleteWord < FilterWord
386
+ def self.hint
387
+ "Remove the NTH word"
388
+ end
232
389
 
233
- class FormatNumber < FilterNumber
234
- def self.hint; 'Format the NTH number adding leading zeroes to have LENGTH'; end
235
- def self.params; 'NTH,LENGTH'; end
390
+ def self.params
391
+ "NTH"
392
+ end
236
393
 
237
- def filtered_number(num, params, param_num)
238
- num.to_i.to_s.rjust(params[1].to_i, '0')
394
+ def filtered_word(_word, _param_num)
395
+ nil
239
396
  end
240
397
  end
241
398
 
399
+ class FormatNumber < FilterNumber
400
+ def self.hint
401
+ "Format the NTH number adding leading zeroes to have LENGTH"
402
+ end
403
+
404
+ def self.params
405
+ "NTH,LENGTH"
406
+ end
407
+
408
+ def filtered_number(num, _param_num)
409
+ num.to_i.to_s.rjust(params[1].to_i, "0")
410
+ end
411
+ end
242
412
 
243
413
  class InsertAfterWord < FilterWord
244
- def self.hint; 'Insert the WORD after the NTH word'; end
245
- def self.params; 'NTH,WORD'; end
414
+ def self.hint
415
+ "Insert the WORD after the NTH word"
416
+ end
417
+
418
+ def self.params
419
+ "NTH,WORD"
420
+ end
246
421
 
247
- def filtered_word(word, params, param_num)
422
+ def filtered_word(word, _param_num)
248
423
  [word, params[1]].join(ws)
249
424
  end
250
425
  end
251
426
 
252
-
253
427
  class InsertBeforeWord < FilterWord
254
- def self.hint; 'Insert the WORD after the NTH word'; end
255
- def self.params; 'NTH,WORD'; end
428
+ def self.hint
429
+ "Insert the WORD after the NTH word"
430
+ end
256
431
 
257
- def filtered_word(word, params, param_num)
432
+ def self.params
433
+ "NTH,WORD"
434
+ end
435
+
436
+ def filtered_word(word, _param_num)
258
437
  [params[1], word].join(ws)
259
438
  end
260
439
  end
261
440
 
441
+ class JoinNumbers < FilterNumber
442
+ def self.hint
443
+ "Join the numbers NTH1 and NTH2 and replace the NTH3 number with it"
444
+ end
262
445
 
263
- class JoinWords < FilterWord
264
- def self.hint; 'Join the words from NTH1 to NTH2'; end
265
- def self.params; 'NTH1,NTH2'; end
446
+ def self.params
447
+ "NTH1,NTH2,NTH3"
448
+ end
266
449
 
267
- def filter(params)
268
- res = get_string.split(ws)
269
- istart = word_idx(params[0], get_string)
270
- iend = word_idx(params[1], get_string)
450
+ def indexed_params
451
+ 3
452
+ end
453
+
454
+ def filtered_number(number, param_num)
455
+ case param_num
456
+ when 1
457
+ @number = number
458
+ number = nil
459
+ when params_expanded.length
460
+ number = @number
461
+ else
462
+ @number += number
463
+ number = nil
464
+ end
271
465
 
272
- res = res.insert(istart, res[istart..iend].join)
273
- set_string res.delete_if.with_index { |x, idx| ((istart.next)..(iend.next)).include?(idx) }.join(ws)
466
+ number
274
467
  end
275
468
  end
276
469
 
470
+ class JoinWords < FilterWord
471
+ def self.hint
472
+ "Join the words NTH1 and NTH2 and replace the NTH3 word with it"
473
+ end
474
+
475
+ def self.params
476
+ "NTH1,NTH2,NTH3"
477
+ end
478
+
479
+ def indexed_params
480
+ 3
481
+ end
482
+
483
+ def filtered_word(word, param_num)
484
+ case param_num
485
+ when 1
486
+ @word = word
487
+ word = nil
488
+ when params_expanded.length
489
+ word = @word
490
+ else
491
+ @word += word
492
+ word = nil
493
+ end
494
+
495
+ word
496
+ end
497
+ end
277
498
 
278
499
  class LeftJustify < FilterBase
279
- def self.hint; 'Add enough CHAR(s) to the right side to have a N-length string'; end
280
- def self.params; 'N,CHAR'; end
500
+ def self.hint
501
+ "Add enough CHAR(s) to the right side to have a N-length string"
502
+ end
503
+
504
+ def self.params
505
+ "N,CHAR"
506
+ end
281
507
 
282
508
  def filter(params)
283
- super get_string.ljust(params[0].to_i, params[1])
509
+ super(get_string.ljust(params[0].to_i, params[1]))
284
510
  end
285
511
  end
286
512
 
287
-
288
513
  class Lowercase < FilterBase
289
- def self.hint; 'Lowercase each word'; end
290
- def self.params; nil; end
514
+ def self.hint
515
+ "Lowercase each word"
516
+ end
291
517
 
292
- def filter(params)
293
- super get_string.downcase
518
+ def self.params
519
+ nil
520
+ end
521
+
522
+ def filter(_params)
523
+ super(get_string.downcase)
294
524
  end
295
525
  end
296
526
 
527
+ class LowercaseWord < FilterWord
528
+ def self.hint
529
+ "Lowercase the NTH word"
530
+ end
297
531
 
298
- class MoveTo < FilterBase
299
- def self.hint; 'Move the text selected by REGEX to TARGET'; end
300
- def self.params; 'REGEX,TARGET'; end
532
+ def self.params
533
+ "NTH"
534
+ end
301
535
 
302
- def filter(params)
303
- regex = Regexp.new(wrap_regex(params[0]), get_config(:ignore_case).to_boolean)
304
- set_string(get_string.scan(regex).pop.to_a.pop, params[1])
305
- str = get_string.gsub(regex, '')
306
- super str
536
+ def filtered_word(word, _param_num)
537
+ word.downcase
307
538
  end
308
539
  end
309
540
 
541
+ class MoveTo < FilterRegExp
542
+ def self.hint
543
+ "Move the text selected by REGEX to TARGET"
544
+ end
545
+
546
+ def self.params
547
+ "REGEX,TARGET"
548
+ end
549
+
550
+ def filtered_regexp(matches, params)
551
+ if matches.length > 2
552
+ matches[1..-1].each_with_index { |x, i| set_string(x, "#{params[1]}_#{i.next}") }
553
+ else
554
+ set_string(matches[1], params[1])
555
+ end
556
+ ""
557
+ end
558
+ end
310
559
 
311
560
  class MoveNumberTo < FilterNumber
312
- def self.hint; 'Move the NTH number to TARGET'; end
313
- def self.params; 'NTH,TARGET'; end
561
+ def self.hint
562
+ "Move the NTH number to TARGET overwriting it"
563
+ end
564
+
565
+ def self.params
566
+ "NTH,TARGET"
567
+ end
568
+
569
+ def self_targeted?
570
+ params[-1] == current_target
571
+ end
314
572
 
315
- def filtered_number(num, params, param_num)
573
+ def filtered_number(num, _param_num)
316
574
  set_string(num, params[1])
317
- ''
575
+ ""
318
576
  end
319
577
  end
320
578
 
579
+ class MoveOccurrenceTo < FilterOccurrence
580
+ def self.hint
581
+ "Move the NTH occurrence of REGEX to TARGET overwriting it"
582
+ end
583
+
584
+ def self.params
585
+ "NTH,REGEX,TARGET"
586
+ end
587
+
588
+ def self_targeted?
589
+ params[-1] == current_target
590
+ end
591
+
592
+ def filtered_occurrence(occurrence, _param_num)
593
+ set_string(occurrence, params[-1])
594
+ nil
595
+ end
596
+ end
321
597
 
322
598
  class MoveWord < FilterWord
323
- def self.hint; 'Move the NTH1 word to the NTH2 place'; end
324
- def self.params; 'NTH1,NTH2'; end
599
+ def self.hint
600
+ "Move the NTH1 word to the NTH2 place"
601
+ end
602
+
603
+ def self.params
604
+ "NTH1,NTH2"
605
+ end
325
606
 
326
- def indexed_params; 2; end
607
+ def indexed_params
608
+ 2
609
+ end
327
610
 
328
- def filtered_word(word, params, param_num)
611
+ def filtered_word(word, param_num)
329
612
  case param_num
330
613
  when 1
331
614
  @word = word
332
615
  res = nil
333
- when 2
616
+ when params_expanded.length
334
617
  res = [word, @word].join(ws)
618
+ else
619
+ @word = [@word, word].join(ws)
620
+ res = nil
335
621
  end
336
622
 
337
623
  res
338
624
  end
339
625
  end
340
626
 
341
-
342
627
  class MoveWordTo < FilterWord
343
- def self.hint; 'Move the NTH word to TARGET'; end
344
- def self.params; 'NTH,TARGET'; end
628
+ def self.hint
629
+ "Move the NTH word to TARGET overwriting it"
630
+ end
631
+
632
+ def self.params
633
+ "NTH,TARGET"
634
+ end
635
+
636
+ def self_targeted?
637
+ params[-1] == current_target
638
+ end
639
+
640
+ def filtered_word(word, param_num)
641
+ case param_num
642
+ when params_expanded.length
643
+ @word = [@word, word].join(ws).strip
644
+ set_string(@word, params[1])
645
+ else
646
+ @word = [@word, word].join(ws)
647
+ end
345
648
 
346
- def filtered_word(word, params, param_num)
347
- set_string(word, params[1])
348
649
  nil
349
650
  end
350
651
  end
351
652
 
352
-
353
653
  class MultiplyNumber < FilterNumber
354
- def self.hint; 'Multiply the NTH number with NUM'; end
355
- def self.params; 'NTH,NUM'; end
654
+ def self.hint
655
+ "Multiply the NTH number with NUM"
656
+ end
356
657
 
357
- def filtered_number(num, params, param_num)
658
+ def self.params
659
+ "NTH,NUM"
660
+ end
661
+
662
+ def filtered_number(num, _param_num)
358
663
  num.to_i * params[1].to_i
359
664
  end
360
665
  end
361
666
 
362
-
363
667
  class Prepend < FilterBase
364
- def self.hint; 'Prepend the current target with TEXT'; end
365
- def self.params; 'TEXT'; end
668
+ def self.hint
669
+ "Prepend the current target with TEXT"
670
+ end
671
+
672
+ def self.params
673
+ "TEXT"
674
+ end
366
675
 
367
676
  def filter(params)
368
- super "#{params[0]}#{get_string}"
677
+ super("#{params[0]}#{get_string}")
369
678
  end
370
679
  end
371
680
 
372
-
373
681
  class PrependFrom < FilterBase
374
- def self.hint; 'Prepend the current target with the text from TARGET'; end
375
- def self.params; 'TARGET'; end
682
+ def self.hint
683
+ "Prepend the current target with the text from TARGET"
684
+ end
685
+
686
+ def self.params
687
+ "TARGET"
688
+ end
376
689
 
377
690
  def filter(params)
378
- super "#{get_string(params[0])}#{get_string}"
691
+ super("#{get_string(params[0])}#{get_string}")
379
692
  end
380
693
  end
381
694
 
382
-
383
695
  class PrependToNumber < FilterNumber
384
- def self.hint; 'Prepend the TEXT to the NTH number'; end
385
- def self.params; 'NTH,TEXT'; end
696
+ def self.hint
697
+ "Prepend the TEXT to the NTH number"
698
+ end
386
699
 
387
- def filtered_number(num, params, param_num)
700
+ def self.params
701
+ "NTH,TEXT"
702
+ end
703
+
704
+ def filtered_number(num, _param_num)
388
705
  "#{params[1]}#{num}"
389
706
  end
390
707
  end
391
708
 
392
-
393
709
  class PrependToWord < FilterWord
394
- def self.hint; 'Prepend the TEXT to the NTH word'; end
395
- def self.params; 'NTH,TEXT'; end
710
+ def self.hint
711
+ "Prepend the TEXT to the NTH word"
712
+ end
713
+
714
+ def self.params
715
+ "NTH,TEXT"
716
+ end
396
717
 
397
- def filtered_word(word, params, param_num)
718
+ def filtered_word(word, _param_num)
398
719
  params[1] + word
399
720
  end
400
721
  end
401
722
 
723
+ class PrependWordFrom < FilterWord
724
+ def self.hint
725
+ "Prepend with TARGET the NTH word"
726
+ end
402
727
 
403
- class Replace < FilterBase
404
- def self.hint; 'Replace the text matching REGEX with REPLACE'; end
405
- def self.params; 'REGEX,REPLACE'; end
728
+ def self.params
729
+ "NTH,TARGET"
730
+ end
406
731
 
407
- def filter(params)
408
- regexp = Regexp.new(params[0], get_config(:ignore_case).to_boolean)
409
- super get_string.gsub(regexp, params[1])
732
+ def string_to_loop
733
+ get_string(params[1])
410
734
  end
411
- end
412
735
 
736
+ def self_targeted?
737
+ true
738
+ end
413
739
 
414
- class ReplaceFrom < FilterBase
415
- def self.hint; 'Replace the REGEX matching text with the TARGET content'; end
416
- def self.params; 'REGEX,TARGET'; end
740
+ def filtered_word(word, _param_num)
741
+ set_string([word, get_string].join(ws))
742
+ word
743
+ end
744
+ end
417
745
 
418
- def filter(params)
419
- regexp = Regexp.new(params[0], get_config(:ignore_case).to_boolean)
420
- super get_string.gsub(regexp, get_string(params[1]).to_s)
746
+ class Replace < FilterRegExp
747
+ def self.hint
748
+ "Replace the text matching REGEX with REPLACE"
421
749
  end
422
750
 
751
+ def self.params
752
+ "REGEX,REPLACE"
753
+ end
754
+
755
+ def filtered_regexp(_matches, params)
756
+ params[1]
757
+ end
423
758
  end
424
759
 
760
+ class ReplaceFrom < FilterRegExp
761
+ def self.hint
762
+ "Replace the REGEX matching text with the TARGET content"
763
+ end
764
+
765
+ def self.params
766
+ "REGEX,TARGET"
767
+ end
768
+
769
+ def filtered_regexp(_matches, params)
770
+ get_string(params[1]).to_s
771
+ end
772
+ end
425
773
 
426
774
  class ReplaceNumber < FilterNumber
427
- def self.hint; 'Replace the NTH number with NUMBER'; end
428
- def self.params; 'NTH,NUMBER'; end
775
+ def self.hint
776
+ "Replace the NTH number with NUMBER"
777
+ end
429
778
 
430
- def filtered_number(num, params, param_num)
779
+ def self.params
780
+ "NTH,NUMBER"
781
+ end
782
+
783
+ def filtered_number(_num, _param_num)
431
784
  params[1]
432
785
  end
433
786
  end
434
787
 
788
+ class ReplaceOccurrence < FilterOccurrence
789
+ def self.hint
790
+ "Replace the NTH occurrence of REGEXP with TEXT"
791
+ end
792
+
793
+ def self.params
794
+ "NTH,REGEXP,TEXT"
795
+ end
796
+
797
+ def filtered_occurrence(_occurrence, _param_num)
798
+ params[2]
799
+ end
800
+ end
435
801
 
436
802
  class ReplaceWord < FilterWord
437
- def self.hint; 'Replace the NTH word with TEXT'; end
438
- def self.params; 'NTH,TEXT'; end
803
+ def self.hint
804
+ "Replace the NTH word with TEXT"
805
+ end
806
+
807
+ def self.params
808
+ "NTH,TEXT"
809
+ end
439
810
 
440
- def filtered_word(word, params, param_num)
811
+ def filtered_word(_word, _param_num)
441
812
  params[1]
442
813
  end
443
814
  end
444
815
 
445
-
446
816
  class ReplaceDate < FilterBase
447
- def self.hint; 'Replace a date from FORMATSRC to FORMATDEST (placeholders: <m>, <B>, <b>, <Y>, <d>)'; end
448
- def self.params; 'FORMATSRC,FORMATDEST[,LANG]'; end
817
+ def self.hint
818
+ "Replace a date from FORMATSRC to FORMATDEST (placeholders: <m>, <B>, <b>, <Y>, <d>)"
819
+ end
820
+
821
+ def self.params
822
+ "FORMATSRC,FORMATDEST[,LANG]"
823
+ end
449
824
 
450
825
  def filter(params)
451
826
  params[2] ||= get_config(:lang)
452
- super get_string.change_date_format(format_src: params[0], format_dest: params[1],
453
- long_months: get_words(:long_months , params[2]),
454
- short_months: get_words(:short_months , params[2]),
827
+ super(get_string.change_date_format(format_src: params[0], format_dest: params[1],
828
+ long_months: get_words(:long_months, params[2]),
829
+ short_months: get_words(:short_months, params[2]),
455
830
  long_days: get_words(:long_days, params[2]),
456
- short_days: get_words(:short_days, params[2]))
831
+ short_days: get_words(:short_days, params[2])))
457
832
  end
458
833
  end
459
834
 
460
-
461
835
  class Reverse < FilterBase
462
- def self.hint; 'Reverse the string'; end
463
- def self.params; nil; end
836
+ def self.hint
837
+ "Reverse the string"
838
+ end
464
839
 
465
- def filter(params)
466
- super get_string.reverse
840
+ def self.params
841
+ nil
467
842
  end
468
- end
469
843
 
844
+ def filter(_params)
845
+ super(get_string.reverse)
846
+ end
847
+ end
470
848
 
471
849
  class RightJustify < FilterBase
472
- def self.hint; 'Apply enough CHAR(s) to the left side to have a N-length string'; end
473
- def self.params; 'N,CHAR'; end
850
+ def self.hint
851
+ "Apply enough CHAR(s) to the left side to have a N-length string"
852
+ end
853
+
854
+ def self.params
855
+ "N,CHAR"
856
+ end
474
857
 
475
858
  def filter(params)
476
- super get_string.rjust(params[0].to_i, params[1])
859
+ super(get_string.rjust(params[0].to_i, params[1]))
477
860
  end
478
861
  end
479
862
 
480
-
481
863
  class Set < FilterBase
482
- def self.hint; 'Set the current or an optional TARGET with TEXT'; end
483
- def self.params; 'TEXT[,TARGET]'; end
864
+ def self.hint
865
+ "Set the current or an optional TARGET with TEXT"
866
+ end
867
+
868
+ def self.params
869
+ "TEXT[,TARGET]"
870
+ end
484
871
 
485
872
  def filter(params)
486
873
  if params[1].nil?
487
874
  str = params[0]
488
875
  else
489
- set_string(params[0], params[1].delete(':<>'))
876
+ set_string(params[0], params[1].delete(":<>"))
490
877
  str = get_string
491
878
  end
492
879
 
493
- super str
880
+ super(str)
494
881
  end
495
882
  end
496
883
 
884
+ class SetNumberWhen < FilterNumber
885
+ def self.hint
886
+ "Set the NTH number to NUMBER when CONDITION is matched"
887
+ end
888
+
889
+ def self.params
890
+ "NTH,CONDITION,NUMBER"
891
+ end
892
+
893
+ def filtered_number(num, _param_num)
894
+ if FilterNumber.calculator(num.to_i, params[-2])
895
+ FilterNumber.calculator(num.to_i, params[-1])
896
+ else
897
+ num
898
+ end
899
+ end
900
+ end
497
901
 
498
902
  class SetWhen < FilterBase
499
- def self.hint; 'Set the current or given TARGET to TEXT when REGEX is matched'; end
500
- def self.params; 'REGEX,TEXT[,TARGET]'; end
903
+ def self.hint
904
+ "Set the current or given TARGET to TEXT when REGEX is matched"
905
+ end
906
+
907
+ def self.params
908
+ "REGEX,TEXT[,TARGET]"
909
+ end
501
910
 
502
911
  def filter(params)
503
912
  target = params[-1] if params.length.odd?
504
913
  set_string(params[1], target) if get_string =~ Regexp.new(params[0], get_config(:ignore_case).to_boolean)
505
- super get_string
914
+ super(get_string)
506
915
  end
507
916
  end
508
917
 
509
-
510
918
  class Spacify < FilterBase
511
- def self.hint; 'Replace CHAR with a space'; end
512
- def self.params; 'CHAR1,CHAR2,...'; end
919
+ def self.hint
920
+ "Replace CHAR with a space"
921
+ end
922
+
923
+ def self.params
924
+ "CHAR1,CHAR2,..."
925
+ end
513
926
 
514
927
  def filter(params)
515
- regexp = Regexp.new(params.join('|'), get_config(:ignore_case).to_boolean)
516
- super get_string.gsub(regexp, ' ')
928
+ regexp = Regexp.new(params.join("|"), get_config(:ignore_case).to_boolean)
929
+ super(get_string.gsub(regexp, " "))
517
930
  end
518
931
  end
519
932
 
520
-
521
933
  class SplitWord < FilterWord
522
- def self.hint; 'Split the NTH word using a REGEX with capturing groups'; end
523
- def self.params; 'NTH,REGEX'; end
934
+ def self.hint
935
+ "Split the NTH word using a REGEX with capturing groups"
936
+ end
937
+
938
+ def self.params
939
+ "NTH,REGEX"
940
+ end
524
941
 
525
- def filtered_word(word, params, param_num)
942
+ def filtered_word(word, _param_num)
526
943
  word.scan(Regexp.new(wrap_regex(params[1]), get_config(:ignore_case))).pop.to_a.join(ws)
527
944
  end
528
945
  end
529
946
 
530
-
531
947
  class Squeeze < FilterBase
532
- def self.hint; 'Squeeze consecutive CHARS in only one'; end
533
- def self.params; 'CHAR'; end
948
+ def self.hint
949
+ "Squeeze consecutive CHARS in only one"
950
+ end
951
+
952
+ def self.params
953
+ "CHAR"
954
+ end
534
955
 
535
956
  def filter(params)
536
- super get_string.gsub Regexp.new("#{params[0]}{2,}", get_config(:ignore_case).to_boolean), params[0]
957
+ super(get_string.gsub Regexp.new("#{params[0]}{2,}", get_config(:ignore_case).to_boolean), params[0])
537
958
  end
538
959
  end
539
960
 
540
-
541
961
  class SwapNumber < FilterNumber
542
- def self.hint; 'Swap the NTH1 number with the NTH2'; end
543
- def self.params; 'NTH1,NTH2'; end
962
+ def self.hint
963
+ "Swap the NTH1 number with the NTH2"
964
+ end
544
965
 
545
- def indexed_params; 2; end
966
+ def self.params
967
+ "NTH1,NTH2"
968
+ end
546
969
 
547
- def filtered_number(num, params, param_num)
970
+ def indexed_params
971
+ 2
972
+ end
973
+
974
+ def filtered_number(num, param_num)
548
975
  case param_num
549
976
  when 1
550
977
  @number = num.clone
551
- num = get_string.get_number(params[1].to_i.pred)
552
- when 2
978
+ @last_number = get_string.get_number(params_expanded[-1].to_i)
979
+ num = @last_number
980
+ when params_expanded.length
553
981
  num = @number
982
+ else
983
+ @number = [@number, num].join(ns)
984
+ num = @last_number
554
985
  end
555
986
 
556
987
  num
557
988
  end
558
989
  end
559
990
 
560
-
561
991
  class SwapWord < FilterWord
562
- def self.hint; 'Swap the NTH1 word with the NTH2'; end
563
- def self.params; 'NTH1,NTH2'; end
992
+ def self.hint
993
+ "Swap the NTH1 word with the NTH2"
994
+ end
564
995
 
565
- def indexed_words; 2; end
996
+ def self.params
997
+ "NTH1,NTH2"
998
+ end
566
999
 
567
- def filtered_word(word, params, param_num)
1000
+ def indexed_params
1001
+ 2
1002
+ end
1003
+
1004
+ def filtered_word(word, param_num)
568
1005
  case param_num
569
1006
  when 1
570
1007
  @word = word.clone
571
- word = get_string.split(ws)[params[1].to_i.pred]
572
- when 2
1008
+ @last_word = get_string.split(ws)[params_expanded[-1]]
1009
+ word = @last_word
1010
+ when params_expanded.length
573
1011
  word = @word
1012
+ else
1013
+ @word = [@word, word].join(ws)
1014
+ word = nil
574
1015
  end
575
1016
 
576
1017
  word
577
1018
  end
578
1019
  end
579
1020
 
580
-
581
1021
  class Template < FilterBase
582
- def self.hint; 'Replace the <placeholders> in TEMPLATE with the relative targets'; end
583
- def self.params; 'TEMPLATE'; end
1022
+ def self.hint
1023
+ "Replace the <placeholders> in TEMPLATE with the relative targets"
1024
+ end
1025
+
1026
+ def self.params
1027
+ "TEMPLATE"
1028
+ end
584
1029
 
585
1030
  def filter(params)
586
- super params[0].gsub(/<([a-z0-9\_]+)>/) { get_string(Regexp.last_match[1]) }
1031
+ super(params[0].gsub(/<([a-z0-9_]+)>/) { get_string(Regexp.last_match[1]) })
587
1032
  end
588
1033
  end
589
1034
 
1035
+ class Translate < FilterBase
1036
+ def self.hint
1037
+ "Replace text in GROUP from SUBGRPSRC to SUBGRPDST"
1038
+ end
590
1039
 
591
- class TranslateWords < FilterBase
592
- def self.hint; 'Replace words in GROUP from SUBGRPS to SUBGRPD'; end
593
- def self.params; 'GROUP,SUBGRPS,SUBGRPD'; end
1040
+ def self.params
1041
+ "GROUP,SUBGRPSRC,SUBGRPDST"
1042
+ end
594
1043
 
595
1044
  def filter(params)
596
1045
  str = get_string
@@ -602,59 +1051,78 @@ module FilterRename
602
1051
  str = str.gsub(Regexp.new(x, get_config(:ignore_case)), get_words(group, lang_dest, i))
603
1052
  end
604
1053
 
605
- super str
1054
+ super(str)
606
1055
  end
607
1056
  end
608
1057
 
609
-
610
1058
  class Trim < FilterBase
611
- def self.hint; 'Remove trailing spaces'; end
612
- def self.params; nil; end
1059
+ def self.hint
1060
+ "Remove trailing spaces"
1061
+ end
613
1062
 
614
- def filter(params)
615
- super get_string.strip
1063
+ def self.params
1064
+ nil
616
1065
  end
617
- end
618
1066
 
1067
+ def filter(_params)
1068
+ super(get_string.strip)
1069
+ end
1070
+ end
619
1071
 
620
1072
  class Uppercase < FilterBase
621
- def self.hint; 'Uppercase each word'; end
622
- def self.params; nil; end
1073
+ def self.hint
1074
+ "Uppercase each word"
1075
+ end
623
1076
 
624
- def filter(params)
625
- super get_string.upcase
1077
+ def self.params
1078
+ nil
1079
+ end
1080
+
1081
+ def filter(_params)
1082
+ super(get_string.upcase)
626
1083
  end
627
1084
  end
628
1085
 
1086
+ class UppercaseWord < FilterWord
1087
+ def self.hint
1088
+ "Uppercase the NTH word"
1089
+ end
629
1090
 
630
- class Wrap < FilterBase
631
- def self.hint; 'Wrap the text matching REGEX with SEPARATOR1 and SEPARATOR2'; end
632
- def self.params; 'REGEX,SEPARATOR1,SEPARATOR2'; end
1091
+ def self.params
1092
+ "NTH"
1093
+ end
633
1094
 
634
- def filter(params)
635
- params[0] = "(#{params[0]})" unless params[0] =~ /[()]+/
636
- regexp = Regexp.new("(#{params[0]})", get_config(:ignore_case).to_boolean)
637
- super get_string.gsub(regexp, "#{params[1]}\\1#{params[2]}")
1095
+ def filtered_word(word, _param_num)
1096
+ word.upcase
638
1097
  end
639
1098
  end
640
1099
 
1100
+ class Wrap < FilterRegExp
1101
+ def self.hint
1102
+ "Wrap the text matching REGEX with SEPARATOR1 and SEPARATOR2"
1103
+ end
641
1104
 
642
- class WrapWords < FilterWord
643
- def self.hint; 'Wrap the words between the NTH1 and the NTH2 with SEPARATOR1 and SEPARATOR2'; end
644
- def self.params; 'NTH1,NTH2,SEPARATOR1,SEPARATOR2'; end
1105
+ def self.params
1106
+ "REGEX,SEPARATOR1,SEPARATOR2"
1107
+ end
645
1108
 
646
- def indexed_words; 2; end
1109
+ def filtered_regexp(matches, params)
1110
+ "#{params[1]}#{matches[0]}#{params[2]}"
1111
+ end
1112
+ end
647
1113
 
648
- def filtered_word(word, params, param_num)
649
- case param_num
650
- when 1
651
- word = "#{params[2]}#{word}"
652
- when 2
653
- word = "#{word}#{params[3]}"
654
- end
655
- word
1114
+ class WrapWord < FilterWord
1115
+ def self.hint
1116
+ "Wrap the NTH word with SEPARATOR1 and SEPARATOR2"
1117
+ end
1118
+
1119
+ def self.params
1120
+ "NTH,SEPARATOR1,SEPARATOR2"
1121
+ end
1122
+
1123
+ def filtered_word(word, _param_num)
1124
+ "#{params[1]}#{word}#{params[2]}"
656
1125
  end
657
1126
  end
658
1127
  end
659
-
660
1128
  end