markdown_exec 1.3.7 → 1.3.9

Sign up to get free protection for your applications and to get access to all the features.
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
@@ -6,13 +6,22 @@
6
6
  module MarkdownExec
7
7
  # Filter
8
8
  #
9
- # The Filter class provides utilities to determine the inclusion of fenced code blocks (FCB)
10
- # based on a set of provided options. The primary function, `fcb_select?`, checks
11
- # various properties of an FCB and decides whether to include or exclude it.
9
+ # The Filter class provides utilities to determine the inclusion of
10
+ # fenced code blocks (FCB) based on a set of provided options. The
11
+ # primary function, `fcb_select?`, checks various properties of an
12
+ # FCB and decides whether to include or exclude it.
12
13
  #
13
14
  # :reek:UtilityFunction
14
-
15
15
  class Filter
16
+ # Determines whether to include or exclude a fenced code block
17
+ # (FCB) based on the provided options.
18
+ #
19
+ # @param options [Hash] The options used for filtering FCBs.
20
+ # @param fcb [Hash] The fenced code block to be evaluated.
21
+ # @return [Boolean] True if the FCB should be included; false if
22
+ # it should be excluded.
23
+ # @raise [StandardError] If an error occurs during the evaluation.
24
+ #
16
25
  def self.fcb_select?(options, fcb)
17
26
  filters = {
18
27
  name_default: true,
@@ -21,13 +30,15 @@ module MarkdownExec
21
30
  shell_default: true,
22
31
  shell_exclude: nil,
23
32
  shell_select: nil,
24
- hidden_name: nil
33
+ hidden_name: nil,
34
+ include_name: nil,
35
+ wrap_name: nil
25
36
  }
26
37
 
27
- name = fcb.fetch(:name, '')
38
+ name = fcb.oname
28
39
  shell = fcb.fetch(:shell, '')
29
40
 
30
- apply_name_filters(options, filters, name)
41
+ apply_name_filters(options, filters, name) #if shell == 'bash'
31
42
  apply_shell_filters(options, filters, shell)
32
43
  apply_other_filters(options, filters, fcb)
33
44
 
@@ -37,6 +48,14 @@ module MarkdownExec
37
48
  raise err
38
49
  end
39
50
 
51
+ # Applies name-based filters to determine whether to include or
52
+ # exclude a fenced code block (FCB)
53
+ # based on the block's name and provided options.
54
+ #
55
+ # @param options [Hash] The options used for filtering FCBs.
56
+ # @param filters [Hash] The filter settings to be updated.
57
+ # @param name [String] The name of the fenced code block.
58
+ #
40
59
  def self.apply_name_filters(options, filters, name)
41
60
  if name.present? && options[:block_name]
42
61
  if name =~ /#{options[:block_name]}/
@@ -59,6 +78,14 @@ module MarkdownExec
59
78
  filters[:name_exclude] = !!(name =~ /#{options[:exclude_by_name_regex]}/)
60
79
  end
61
80
 
81
+ # Applies shell-based filters to determine whether to include or
82
+ # exclude a fenced code block (FCB)
83
+ # based on the block's shell type and provided options.
84
+ #
85
+ # @param options [Hash] The options used for filtering FCBs.
86
+ # @param filters [Hash] The filter settings to be updated.
87
+ # @param shell [String] The shell type of the fenced code block.
88
+ #
62
89
  def self.apply_shell_filters(options, filters, shell)
63
90
  filters[:shell_expect] = shell == 'expect'
64
91
 
@@ -71,34 +98,58 @@ module MarkdownExec
71
98
  filters[:shell_exclude] = !!(shell =~ /#{options[:exclude_by_shell_regex]}/)
72
99
  end
73
100
 
101
+ # Applies additional filters to determine whether to include or
102
+ # exclude a fenced code block (FCB)
103
+ # based on various criteria and provided options.
104
+ #
105
+ # @param options [Hash] The options used for filtering FCBs.
106
+ # @param filters [Hash] The filter settings to be updated.
107
+ # @param fcb [Hash] The fenced code block to be evaluated.
108
+ #
74
109
  def self.apply_other_filters(options, filters, fcb)
75
- name = fcb.fetch(:name, '')
110
+ name = fcb.oname
76
111
  shell = fcb.fetch(:shell, '')
77
112
  filters[:fcb_chrome] = fcb.fetch(:chrome, false)
78
113
 
79
- if name.present? && options[:hide_blocks_by_name] &&
80
- options[:block_name_hidden_match].present?
81
- filters[:hidden_name] = !!(name =~ /#{options[:block_name_hidden_match]}/)
82
- end
83
-
84
- if shell.present? && options[:hide_blocks_by_shell] &&
85
- options[:block_shell_hidden_match].present?
86
- !!(shell =~ /#{options[:block_shell_hidden_match]}/)
114
+ if name.present? && options[:hide_blocks_by_name]
115
+ filters[:hidden_name] =
116
+ !!(options[:block_name_hidden_match].present? &&
117
+ name =~ /#{options[:block_name_hidden_match]}/)
87
118
  end
119
+ filters[:include_name] =
120
+ !!(options[:block_name_include_match].present? &&
121
+ name =~ /#{options[:block_name_include_match]}/)
122
+ filters[:wrap_name] =
123
+ !!(options[:block_name_wrapper_match].present? &&
124
+ name =~ /#{options[:block_name_wrapper_match]}/)
88
125
 
89
126
  return unless options[:bash_only]
90
127
 
91
128
  filters[:shell_default] = (shell == 'bash')
92
129
  end
93
130
 
131
+ # Evaluates the filter settings to make a final decision on
132
+ # whether to include or exclude a fenced
133
+ # code block (FCB) based on the provided options.
134
+ #
135
+ # @param options [Hash] The options used for filtering FCBs.
136
+ # @param filters [Hash] The filter settings to be evaluated.
137
+ # @return [Boolean] True if the FCB should be included; false
138
+ # if it should be excluded.
139
+ #
94
140
  def self.evaluate_filters(options, filters)
95
- if options[:no_chrome] && filters[:fcb_chrome]
141
+ if options[:no_chrome] && filters[:fcb_chrome] == true
96
142
  false
97
- elsif options[:exclude_expect_blocks] && filters[:shell_expect]
143
+ elsif options[:exclude_expect_blocks] && filters[:shell_expect] == true
98
144
  false
99
145
  elsif filters[:hidden_name] == true
146
+ false
147
+ elsif filters[:include_name] == true
100
148
  true
101
- elsif filters[:name_exclude] == true || filters[:shell_exclude] == true || filters[:name_select] == false || filters[:shell_select] == false
149
+ elsif filters[:wrap_name] == true
150
+ true
151
+ elsif filters[:name_exclude] == true || filters[:shell_exclude] == true ||
152
+ filters[:name_select] == false || filters[:shell_select] == false
102
153
  false
103
154
  elsif filters[:name_select] == true || filters[:shell_select] == true
104
155
  true
@@ -112,70 +163,95 @@ module MarkdownExec
112
163
  end
113
164
 
114
165
  if $PROGRAM_NAME == __FILE__
166
+ require 'bundler/setup'
167
+ Bundler.require(:default)
168
+
169
+ require 'fcb'
115
170
  require 'minitest/autorun'
116
171
 
117
- require_relative 'tap'
118
- include Tap
172
+ module MarkdownExec
173
+ class FilterTest < Minitest::Test
174
+ def setup
175
+ @options = {}
176
+ @fcb = FCB.new(
177
+ dname: nil,
178
+ oname: nil
179
+ )
180
+ end
119
181
 
120
- class FilterTest < Minitest::Test
121
- def test_no_chrome_condition
122
- options = { no_chrome: true }
123
- fcb = { chrome: true }
124
- refute MarkdownExec::Filter.fcb_select?(options, fcb)
125
- end
182
+ # Tests for fcb_select? method
183
+ def test_no_chrome_condition
184
+ @options[:no_chrome] = true
185
+ @fcb[:chrome] = true
186
+ refute Filter.fcb_select?(@options, @fcb)
187
+ end
126
188
 
127
- def test_exclude_expect_blocks_condition
128
- options = { exclude_expect_blocks: true }
129
- fcb = { shell: 'expect' }
130
- refute MarkdownExec::Filter.fcb_select?(options, fcb)
131
- end
189
+ def test_exclude_expect_blocks_condition
190
+ @options[:exclude_expect_blocks] = true
191
+ @fcb[:shell] = 'expect'
192
+ refute Filter.fcb_select?(@options, @fcb)
193
+ end
132
194
 
133
- def test_hidden_name_condition
134
- options = { hide_blocks_by_name: true, block_name_hidden_match: 'hidden' }
135
- fcb = { name: 'hidden_block' }
136
- assert MarkdownExec::Filter.fcb_select?(options, fcb)
137
- end
195
+ def test_hidden_name_condition
196
+ @options[:hide_blocks_by_name] = true
197
+ @options[:block_name_hidden_match] = 'hidden'
198
+ @fcb[:oname] = 'hidden_block'
199
+ refute Filter.fcb_select?(@options, @fcb)
200
+ end
138
201
 
139
- def test_name_exclude_condition
140
- options = { block_name: 'test' }
141
- fcb = { name: 'sample' }
142
- refute MarkdownExec::Filter.fcb_select?(options, fcb)
143
- end
202
+ def test_include_name_condition
203
+ @options[:hide_blocks_by_name] = true
204
+ @options[:block_name_indlude_match] = 'include'
205
+ @fcb[:oname] = 'include_block'
206
+ assert Filter.fcb_select?(@options, @fcb)
207
+ end
144
208
 
145
- def test_shell_exclude_condition
146
- options = { exclude_by_shell_regex: 'exclude_this' }
147
- fcb = { shell: 'exclude_this_shell' }
148
- refute MarkdownExec::Filter.fcb_select?(options, fcb)
149
- end
209
+ def test_wrap_name_condition
210
+ @options[:hide_blocks_by_name] = true
211
+ @options[:block_name_wrapper_match] = 'wrap'
212
+ @fcb[:oname] = 'wrap_block'
213
+ assert Filter.fcb_select?(@options, @fcb)
214
+ end
150
215
 
151
- def test_name_select_condition
152
- options = { select_by_name_regex: 'select' }
153
- fcb = { name: 'select_this' }
154
- assert MarkdownExec::Filter.fcb_select?(options, fcb)
155
- end
216
+ def test_name_exclude_condition
217
+ @options[:block_name] = 'test'
218
+ @fcb[:oname] = 'sample'
219
+ refute Filter.fcb_select?(@options, @fcb)
220
+ end
156
221
 
157
- def test_shell_select_condition
158
- options = { select_by_shell_regex: 'select_this' }
159
- fcb = { shell: 'select_this_shell' }
160
- assert MarkdownExec::Filter.fcb_select?(options, fcb)
161
- end
222
+ def test_shell_exclude_condition
223
+ @options[:exclude_by_shell_regex] = 'exclude_this'
224
+ @fcb[:shell] = 'exclude_this_shell'
225
+ refute Filter.fcb_select?(@options, @fcb)
226
+ end
162
227
 
163
- def test_bash_only_condition_true
164
- options = { bash_only: true }
165
- fcb = { shell: 'bash' }
166
- assert MarkdownExec::Filter.fcb_select?(options, fcb)
167
- end
228
+ def test_name_select_condition
229
+ @options[:select_by_name_regex] = 'select'
230
+ @fcb[:oname] = 'select_this'
231
+ assert Filter.fcb_select?(@options, @fcb)
232
+ end
168
233
 
169
- def test_bash_only_condition_false
170
- options = { bash_only: true }
171
- fcb = { shell: 'zsh' }
172
- refute MarkdownExec::Filter.fcb_select?(options, fcb)
173
- end
234
+ def test_shell_select_condition
235
+ @options[:select_by_shell_regex] = 'select_this'
236
+ @fcb[:shell] = 'select_this_shell'
237
+ assert Filter.fcb_select?(@options, @fcb)
238
+ end
239
+
240
+ def test_bash_only_condition_true
241
+ @options[:bash_only] = true
242
+ @fcb[:shell] = 'bash'
243
+ assert Filter.fcb_select?(@options, @fcb)
244
+ end
174
245
 
175
- def test_default_case
176
- options = {}
177
- fcb = {}
178
- assert MarkdownExec::Filter.fcb_select?(options, fcb)
246
+ def test_bash_only_condition_false
247
+ @options[:bash_only] = true
248
+ @fcb[:shell] = 'zsh'
249
+ refute Filter.fcb_select?(@options, @fcb)
250
+ end
251
+
252
+ def test_default_case
253
+ assert Filter.fcb_select?(@options, @fcb)
254
+ end
179
255
  end
180
256
  end
181
257
  end
@@ -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.7'
10
+ VERSION = '1.3.9'
11
11
  end