markdown_exec 1.3.8 → 1.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/colorize.rb CHANGED
@@ -2,67 +2,78 @@
2
2
 
3
3
  # encoding=utf-8
4
4
 
5
- # │0 │ to restore default │
6
- # │ │ color │
7
- # ├───┼───────────────────────┤
8
- # │ │ │
9
- # │1 │ for brighter colors │
10
- # ├───┼───────────────────────┤
11
- # │ │ │
12
- # │4 │ for underlined text │
13
- # ├───┼───────────────────────┤
14
- # │ │ │
15
- # │5 │ for flashing text
5
+ # Extends Ruby's native String class to include ANSI coloring functionality.
6
+ # Adds methods to apply RGB colors, named colors, and other formatting to strings.
16
7
  class String
17
- def plain
18
- self
8
+ # Handles dynamic method calls to create RGB colors.
9
+ #
10
+ # @param method_name [Symbol] The name of the method being called.
11
+ # @param args [Array] The arguments passed to the method.
12
+ # @param block [Proc] An optional block.
13
+ # @return [String] The formatted string.
14
+ def method_missing(method_name, *args, &block)
15
+ case method_name.to_s
16
+ when /^fg_rgb_/
17
+ fg_rgb_color($'.gsub('_', ';'))
18
+ when /^fg_rgbh_/
19
+ hex_to_rgb($')
20
+ else
21
+ super
22
+ end
19
23
  end
20
24
 
21
- def black
22
- "\033[30m#{self}\033[0m"
25
+ # Generates an ANSI control sequence for the string.
26
+ #
27
+ # @return [String] The string wrapped in an ANSI control sequence.
28
+ def ansi_control_sequence
29
+ "\033[#{self}\033[0m"
23
30
  end
24
31
 
25
- def red
26
- "\033[31m#{self}\033[0m"
32
+ # Applies a 24-bit RGB foreground color to the string.
33
+ #
34
+ # @param rgb [String] The RGB color, expressed as a string like "1;2;3".
35
+ # @return [String] The string with the applied RGB foreground color.
36
+ def fg_rgb_color(rgb)
37
+ "38;2;#{rgb}m#{self}".ansi_control_sequence
27
38
  end
28
39
 
29
- def bred
30
- "\033[1;31m#{self}\033[0m"
40
+ # Converts hex color codes to RGB and applies them to the string.
41
+ #
42
+ # @param hex_str [String] The RGB color, expressed as a hex string like "FF00FF".
43
+ # @return [String] The string with the applied RGB foreground color.
44
+ def hex_to_rgb(hex_str)
45
+ fg_rgb_color(
46
+ hex_str.split('_').map { |hex| hex.to_i(16).to_s }.join(';')
47
+ )
31
48
  end
32
49
 
33
- def green
34
- "\033[32m#{self}\033[0m"
35
- end
36
-
37
- def bgreen
38
- "\033[1;32m#{self}\033[0m"
39
- end
40
-
41
- def yellow
42
- "\033[33m#{self}\033[0m"
43
- end
44
-
45
- def byellow
46
- "\033[1;33m#{self}\033[0m"
47
- end
48
-
49
- def blue
50
- "\033[34m#{self}\033[0m"
51
- end
52
-
53
- def magenta
54
- "\033[35m#{self}\033[0m"
50
+ # Provides a plain, unmodified version of the string.
51
+ #
52
+ # @return [String] The original string.
53
+ def plain
54
+ self
55
55
  end
56
56
 
57
- def cyan
58
- "\033[36m#{self}\033[0m"
59
- end
57
+ # A collection of methods for applying named colors.
58
+ #
59
+ # For example, #black applies a black foreground color to the string.
60
+ # These are provided for convenience and easy readability.
60
61
 
61
- def white
62
- "\033[37m#{self}\033[0m"
63
- end
62
+ def black; "30m#{self}".ansi_control_sequence; end
63
+ def bred; "1;31m#{self}".ansi_control_sequence; end
64
+ def bgreen; "1;32m#{self}".ansi_control_sequence; end
65
+ def byellow; "1;33m#{self}".ansi_control_sequence; end
66
+ def magenta; "35m#{self}".ansi_control_sequence; end
67
+ def cyan; "36m#{self}".ansi_control_sequence; end
68
+ def white; "37m#{self}".ansi_control_sequence; end
69
+ def bwhite; "1;37m#{self}".ansi_control_sequence; end
64
70
 
65
- def bwhite
66
- "\033[1;37m#{self}\033[0m"
67
- end
71
+ # More named colors using RGB hex values
72
+ def blue; fg_rgbh_00_00_FF; end
73
+ def green; fg_rgbh_00_FF_00; end
74
+ def indigo; fg_rgbh_4B_00_82; end
75
+ def orange; fg_rgbh_FF_7F_00; end
76
+ def red; fg_rgbh_FF_00_00; end
77
+ def violet; fg_rgbh_94_00_D3; end
78
+ def yellow; fg_rgbh_FF_FF_00; end
68
79
  end
data/lib/fcb.rb CHANGED
@@ -18,7 +18,9 @@ module MarkdownExec
18
18
  body: nil,
19
19
  call: nil,
20
20
  headings: [],
21
+ dname: nil,
21
22
  name: nil,
23
+ oname: nil,
22
24
  reqs: [],
23
25
  shell: '',
24
26
  title: '',
@@ -27,39 +29,9 @@ module MarkdownExec
27
29
  }.merge(options)
28
30
  end
29
31
 
30
- def to_h
31
- @attrs
32
- end
33
-
34
- def to_yaml
35
- @attrs.to_yaml
36
- end
37
-
38
- private
39
-
40
- # 2023-10-07 proposed but not functional with code
41
- #
42
- # def method_missing(method, *args, &block)
43
- # method_name = method.to_s
44
-
45
- # if method_name[-1] == '='
46
- # @attrs[method_name.chop.to_sym] = args[0]
47
- # elsif @attrs.key?(method_name.to_sym)
48
- # @attrs[method_name.to_sym]
49
- # else
50
- # super
51
- # end
52
- # rescue StandardError => err
53
- # warn(error = "ERROR ** FCB.method_missing(method: #{method_name}, *args: #{args.inspect}, &block)")
54
- # warn err.inspect
55
- # warn(caller[0..4])
56
- # raise err # Here, we simply propagate the original error instead of wrapping it in a StandardError.
57
- # end
58
-
59
32
  # :reek:ManualDispatch
60
33
  def method_missing(method, *args, &block)
61
34
  method_name = method.to_s
62
-
63
35
  if @attrs.respond_to?(method_name)
64
36
  @attrs.send(method_name, *args, &block)
65
37
  elsif method_name[-1] == '='
@@ -79,6 +51,14 @@ module MarkdownExec
79
51
  def respond_to_missing?(method_name, _include_private = false)
80
52
  @attrs.key?(method_name.to_sym) || super
81
53
  end
54
+
55
+ def to_h
56
+ @attrs
57
+ end
58
+
59
+ def to_yaml
60
+ @attrs.to_yaml
61
+ end
82
62
  end
83
63
  end
84
64
 
@@ -92,7 +72,9 @@ if $PROGRAM_NAME == __FILE__
92
72
  body: 'Sample body',
93
73
  call: 'Sample call',
94
74
  headings: %w[Header1 Header2],
75
+ dname: 'Sample name',
95
76
  name: 'Sample name',
77
+ oname: 'Sample name',
96
78
  reqs: %w[req1 req2],
97
79
  shell: 'bash',
98
80
  text: 'Sample Text',
data/lib/filter.rb CHANGED
@@ -35,7 +35,7 @@ module MarkdownExec
35
35
  wrap_name: nil
36
36
  }
37
37
 
38
- name = fcb.fetch(:name, '')
38
+ name = fcb.oname
39
39
  shell = fcb.fetch(:shell, '')
40
40
 
41
41
  apply_name_filters(options, filters, name)
@@ -107,7 +107,7 @@ module MarkdownExec
107
107
  # @param fcb [Hash] The fenced code block to be evaluated.
108
108
  #
109
109
  def self.apply_other_filters(options, filters, fcb)
110
- name = fcb.fetch(:name, '')
110
+ name = fcb.oname
111
111
  shell = fcb.fetch(:shell, '')
112
112
  filters[:fcb_chrome] = fcb.fetch(:chrome, false)
113
113
 
@@ -125,7 +125,7 @@ module MarkdownExec
125
125
 
126
126
  return unless options[:bash_only]
127
127
 
128
- filters[:shell_default] = (shell == 'bash')
128
+ filters[:shell_default] = (shell == BLOCK_TYPE_BASH)
129
129
  end
130
130
 
131
131
  # Evaluates the filter settings to make a final decision on
@@ -166,13 +166,17 @@ if $PROGRAM_NAME == __FILE__
166
166
  require 'bundler/setup'
167
167
  Bundler.require(:default)
168
168
 
169
+ require 'fcb'
169
170
  require 'minitest/autorun'
170
171
 
171
172
  module MarkdownExec
172
173
  class FilterTest < Minitest::Test
173
174
  def setup
174
175
  @options = {}
175
- @fcb = {}
176
+ @fcb = FCB.new(
177
+ dname: nil,
178
+ oname: nil
179
+ )
176
180
  end
177
181
 
178
182
  # Tests for fcb_select? method
@@ -191,27 +195,27 @@ if $PROGRAM_NAME == __FILE__
191
195
  def test_hidden_name_condition
192
196
  @options[:hide_blocks_by_name] = true
193
197
  @options[:block_name_hidden_match] = 'hidden'
194
- @fcb[:name] = 'hidden_block'
198
+ @fcb[:oname] = 'hidden_block'
195
199
  refute Filter.fcb_select?(@options, @fcb)
196
200
  end
197
201
 
198
202
  def test_include_name_condition
199
203
  @options[:hide_blocks_by_name] = true
200
204
  @options[:block_name_indlude_match] = 'include'
201
- @fcb[:name] = 'include_block'
205
+ @fcb[:oname] = 'include_block'
202
206
  assert Filter.fcb_select?(@options, @fcb)
203
207
  end
204
208
 
205
209
  def test_wrap_name_condition
206
210
  @options[:hide_blocks_by_name] = true
207
211
  @options[:block_name_wrapper_match] = 'wrap'
208
- @fcb[:name] = 'wrap_block'
212
+ @fcb[:oname] = 'wrap_block'
209
213
  assert Filter.fcb_select?(@options, @fcb)
210
214
  end
211
215
 
212
216
  def test_name_exclude_condition
213
217
  @options[:block_name] = 'test'
214
- @fcb[:name] = 'sample'
218
+ @fcb[:oname] = 'sample'
215
219
  refute Filter.fcb_select?(@options, @fcb)
216
220
  end
217
221
 
@@ -223,7 +227,7 @@ if $PROGRAM_NAME == __FILE__
223
227
 
224
228
  def test_name_select_condition
225
229
  @options[:select_by_name_regex] = 'select'
226
- @fcb[:name] = 'select_this'
230
+ @fcb[:oname] = 'select_this'
227
231
  assert Filter.fcb_select?(@options, @fcb)
228
232
  end
229
233
 
@@ -235,7 +239,7 @@ if $PROGRAM_NAME == __FILE__
235
239
 
236
240
  def test_bash_only_condition_true
237
241
  @options[:bash_only] = true
238
- @fcb[:shell] = 'bash'
242
+ @fcb[:shell] = BLOCK_TYPE_BASH
239
243
  assert Filter.fcb_select?(@options, @fcb)
240
244
  end
241
245
 
@@ -7,5 +7,5 @@ module MarkdownExec
7
7
  BIN_NAME = 'mde'
8
8
  GEM_NAME = 'markdown_exec'
9
9
  TAP_DEBUG = 'MDE_DEBUG'
10
- VERSION = '1.3.8'
10
+ VERSION = '1.4'
11
11
  end