markdown_exec 1.4.1 → 1.6

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/mdoc.rb CHANGED
@@ -47,7 +47,8 @@ module MarkdownExec
47
47
  # write named variables to block at top of script
48
48
  #
49
49
  fcb[:body].join(' ').split.compact.map do |key|
50
- format(opts[:block_type_port_set_format], { key: key, value: ENV.fetch(key, nil) })
50
+ format(opts[:block_type_port_set_format],
51
+ { key: key, value: ENV.fetch(key, nil) })
51
52
  end
52
53
  end
53
54
 
@@ -69,19 +70,19 @@ module MarkdownExec
69
70
  # @return [Array<Hash>] An array of code blocks required by the specified code block.
70
71
  #
71
72
  def collect_recursively_required_blocks(name)
72
- name_block = get_block_by_name(name)
73
- raise "Named code block `#{name}` not found." if name_block.nil? || name_block.keys.empty?
73
+ name_block = get_block_by_anyname(name)
74
+ if name_block.nil? || name_block.keys.empty?
75
+ raise "Named code block `#{name}` not found."
76
+ end
74
77
 
75
- # all = [name_block.fetch(:name, '')] + recursively_required(name_block[:reqs])
76
78
  all = [name_block.oname] + recursively_required(name_block[:reqs])
77
79
 
78
80
  # in order of appearance in document
79
81
  # insert function blocks
80
- # @table.select { |fcb| all.include? fcb.fetch(:name, '') }
81
82
  @table.select { |fcb| all.include? fcb.oname }
82
83
  .map do |fcb|
83
84
  if (call = fcb[:call])
84
- [get_block_by_name("[#{call.match(/^%\((\S+) |\)/)[1]}]")
85
+ [get_block_by_oname("[#{call.match(/^%\((\S+) |\)/)[1]}]")
85
86
  .merge({ cann: call })]
86
87
  else
87
88
  []
@@ -102,9 +103,10 @@ module MarkdownExec
102
103
  collect_block_code_cann(fcb)
103
104
  elsif fcb[:stdout]
104
105
  collect_block_code_stdout(fcb)
105
- elsif [BLOCK_TYPE_LINK, BLOCK_TYPE_OPTS, BLOCK_TYPE_VARS].include? fcb[:shell]
106
+ elsif [BlockType::LINK, BlockType::OPTS,
107
+ BlockType::VARS].include? fcb[:shell]
106
108
  nil
107
- elsif fcb[:shell] == BLOCK_TYPE_PORT
109
+ elsif fcb[:shell] == BlockType::PORT
108
110
  collect_block_code_shell(fcb)
109
111
  else
110
112
  fcb[:body]
@@ -147,12 +149,16 @@ module MarkdownExec
147
149
  ### hide rows correctly
148
150
 
149
151
  if opts[:hide_blocks_by_name]
150
- selrows.reject { |block| hide_menu_block_per_options opts, block }
151
- else
152
- selrows
153
- end.map do |block|
154
- # block[:name] = block[:text] if block[:name].nil?
155
- block
152
+ selrows = selrows.reject do |block|
153
+ hide_menu_block_per_options opts, block
154
+ end
155
+ end
156
+
157
+ # remove
158
+ # . empty chrome between code; edges are same as blanks
159
+ #
160
+ select_elements_with_neighbor_conditions(selrows) do |prev_element, current, next_element|
161
+ !(current[:chrome] && !current[:oname].present?) || !(!prev_element.nil? && prev_element[:shell].present? && !next_element.nil? && next_element[:shell].present?)
156
162
  end
157
163
  end
158
164
 
@@ -162,8 +168,22 @@ module MarkdownExec
162
168
  # @param default [Hash] The default value to return if the code block is not found.
163
169
  # @return [Hash] The code block as a hash or the default value if not found.
164
170
  #
165
- def get_block_by_name(name, default = {})
166
- @table.select { |fcb| fcb.fetch(:oname, '') == name }.fetch(0, default)
171
+ def get_block_by_anyname(name, default = {})
172
+ @table.select do |fcb|
173
+ fcb.fetch(:dname, '') == name || fcb.fetch(:oname, '') == name
174
+ end.fetch(0, default)
175
+ end
176
+
177
+ def get_block_by_dname(name, default = {})
178
+ @table.select do |fcb|
179
+ fcb.fetch(:dname, '') == name
180
+ end.fetch(0, default)
181
+ end
182
+
183
+ def get_block_by_oname(name, default = {})
184
+ @table.select do |fcb|
185
+ fcb.fetch(:oname, '') == name
186
+ end.fetch(0, default)
167
187
  end
168
188
 
169
189
  # Checks if a code block should be hidden based on the given options.
@@ -174,17 +194,24 @@ module MarkdownExec
174
194
  #
175
195
  # :reek:UtilityFunction
176
196
  def hide_menu_block_per_options(opts, block)
177
- (opts[:hide_blocks_by_name] &&
178
- ((opts[:block_name_hidden_match]&.present? &&
179
- block.oname&.match(Regexp.new(opts[:block_name_hidden_match]))) ||
180
- (opts[:block_name_include_match]&.present? &&
181
- block.oname&.match(Regexp.new(opts[:block_name_include_match]))) ||
182
- (opts[:block_name_wrapper_match]&.present? &&
183
- block.oname&.match(Regexp.new(opts[:block_name_wrapper_match])))) &&
184
- (block.oname&.present? || block[:label]&.present?)
185
- )
197
+ if block.fetch(:chrome, false)
198
+ false
199
+ else
200
+ (opts[:hide_blocks_by_name] &&
201
+ ((opts[:block_name_hidden_match]&.present? &&
202
+ block.oname&.match(Regexp.new(opts[:block_name_hidden_match]))) ||
203
+ (opts[:block_name_include_match]&.present? &&
204
+ block.oname&.match(Regexp.new(opts[:block_name_include_match]))) ||
205
+ (opts[:block_name_wrapper_match]&.present? &&
206
+ block.oname&.match(Regexp.new(opts[:block_name_wrapper_match])))) &&
207
+ (block.oname&.present? || block[:label]&.present?)
208
+ )
209
+ end
186
210
  end
187
211
 
212
+ # def load_auto_blocks(opts)
213
+ # end
214
+
188
215
  # Recursively fetches required code blocks for a given list of requirements.
189
216
  #
190
217
  # @param reqs [Array<String>] An array of requirements to start the recursion from.
@@ -200,13 +227,57 @@ module MarkdownExec
200
227
  next if memo.include? req
201
228
 
202
229
  memo += [req]
203
- get_block_by_name(req).fetch(:reqs, [])
230
+ get_block_by_oname(req).fetch(:reqs, [])
204
231
  end
205
232
  .compact
206
233
  .flatten(1)
207
234
  end
208
235
  memo
209
236
  end
237
+
238
+ def select_elements_with_neighbor_conditions(array,
239
+ last_selected_placeholder = nil, next_selected_placeholder = nil)
240
+ selected_elements = []
241
+ last_selected = last_selected_placeholder
242
+
243
+ array.each_with_index do |current, index|
244
+ next_element = if index < array.size - 1
245
+ array[index + 1]
246
+ else
247
+ next_selected_placeholder
248
+ end
249
+
250
+ if yield(last_selected, current, next_element)
251
+ selected_elements << current
252
+ last_selected = current
253
+ end
254
+ end
255
+
256
+ selected_elements
257
+ end
258
+
259
+ # def select_elements_with_neighbor_conditions(array)
260
+ # # This function filters elements from the array where the current element has property A set to true
261
+ # # and both the previous and next elements have property B set to true.
262
+ # selected_elements = []
263
+
264
+ # array.each_with_index do |element, index|
265
+ # next if index.zero? # Skip the first element since it has no previous element
266
+ # break if index >= array.size - 1 # Break before the last to avoid out-of-bound errors
267
+
268
+ # prev_element = array[index - 1]
269
+ # next_element = array[index + 1]
270
+
271
+ # # Check the conditions for property A on the current element and property B on adjacent elements
272
+ # unless element[:chrome] && !element[:oname].present? && prev_element[:shell].present? && next_element[:shell].present?
273
+ # selected_elements << element
274
+ # # else
275
+ # # pp 'SKIPPING', element
276
+ # end
277
+ # end
278
+
279
+ # selected_elements
280
+ # end
210
281
  end
211
282
  end
212
283
 
@@ -220,9 +291,11 @@ if $PROGRAM_NAME == __FILE__
220
291
  class TestMDoc < Minitest::Test
221
292
  def setup
222
293
  @table = [
223
- { oname: 'block1', body: ['code for block1'], reqs: ['block2'] },
294
+ { oname: 'block1', body: ['code for block1'],
295
+ reqs: ['block2'] },
224
296
  { oname: 'block2', body: ['code for block2'], reqs: nil },
225
- { oname: 'block3', body: ['code for block3'], reqs: ['block1'] }
297
+ { oname: 'block3', body: ['code for block3'],
298
+ reqs: ['block1'] }
226
299
  ]
227
300
  @doc = MDoc.new(@table)
228
301
  end
@@ -234,10 +307,10 @@ if $PROGRAM_NAME == __FILE__
234
307
  # end
235
308
 
236
309
  def test_get_block_by_name
237
- result = @doc.get_block_by_name('block1')
310
+ result = @doc.get_block_by_oname('block1')
238
311
  assert_equal @table[0], result
239
312
 
240
- result_missing = @doc.get_block_by_name('missing_block')
313
+ result_missing = @doc.get_block_by_oname('missing_block')
241
314
  assert_equal({}, result_missing)
242
315
  end
243
316
 
@@ -253,8 +326,9 @@ if $PROGRAM_NAME == __FILE__
253
326
  # end
254
327
 
255
328
  def test_hide_menu_block_per_options
256
- opts = { hide_blocks_by_name: true, block_name_hidden_match: 'block1' }
257
- block = OpenStruct.new(oname: 'block1')
329
+ opts = { hide_blocks_by_name: true,
330
+ block_name_hidden_match: 'block1' }
331
+ block = FCB.new(oname: 'block1')
258
332
  result = @doc.hide_menu_block_per_options(opts, block)
259
333
  assert result # this should be true based on the given logic
260
334
  end