Class: MarkdownExec::Filter

Inherits:
Object show all
Defined in:
lib/filter.rb

Overview

Filter

The Filter class provides utilities to determine the inclusion of fenced code blocks (FCB) based on a set of provided options. The primary function, ‘fcb_select?`, checks various properties of an FCB and decides whether to include or exclude it.

:reek:UtilityFunction

Class Method Summary collapse

Class Method Details

.apply_name_filters(options, filters, name) ⇒ Object

Applies name-based filters to determine whether to include or exclude a fenced code block (FCB) based on the block’s name and provided options.

Parameters:

  • options (Hash)

    The options used for filtering FCBs.

  • filters (Hash)

    The filter settings to be updated.

  • name (String)

    The name of the fenced code block.



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/filter.rb', line 69

def self.apply_name_filters(options, filters, name)
  filters[:name_select] = true
  filters[:name_exclude] = false

  if name.present? && filters[:name_select].nil? &&
     options[:select_by_name_regex].present?
    filters[:name_select] =
      !!(name =~ Regexp.new(options[:select_by_name_regex]))
  end

  unless name.present? && filters[:name_exclude].nil? &&
         options[:exclude_by_name_regex].present?
    return
  end

  filters[:name_exclude] =
    !!(name =~ Regexp.new(options[:exclude_by_name_regex]))
end

.apply_other_filters(options, filters, fcb) ⇒ Object

Applies additional filters to determine whether to include or exclude a fenced code block (FCB) based on various criteria and provided options.

Parameters:

  • options (Hash)

    The options used for filtering FCBs.

  • filters (Hash)

    The filter settings to be updated.

  • fcb (Hash)

    The fenced code block to be evaluated.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/filter.rb', line 124

def self.apply_other_filters(options, filters, fcb)
  name = fcb.oname
  shell = fcb.shell
  filters[:fcb_chrome] = fcb.fetch(:chrome, false)
  filters[:shell_default] = (fcb.type == BlockType::SHELL)

  if options[:block_disable_match].present? &&
     fcb.start_line =~ Regexp.new(options[:block_disable_match])
    fcb.disabled = TtyMenu::DISABLE
  end
  return unless name.present?

  if options[:hide_blocks_by_name]
    filters[:hidden_name] =
      !!(options[:block_name_hidden_match].present? &&
                name =~ Regexp.new(options[:block_name_hidden_match]))
  end
  filters[:include_name] =
    !!(options[:block_name_include_match].present? &&
              name =~ Regexp.new(options[:block_name_include_match]))
  filters[:wrap_name] =
    !!(options[:block_name_wrapper_match].present? &&
              name =~ Regexp.new(options[:block_name_wrapper_match]))
end

.apply_shell_filters(options, filters, shell:, type:) ⇒ Object

Applies shell-based filters to determine whether to include or exclude a fenced code block (FCB) based on the block’s shell type and provided options.

Parameters:

  • options (Hash)

    The options used for filtering FCBs.

  • filters (Hash)

    The filter settings to be updated.

  • shell (String)

    The shell type of the fenced code block.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/filter.rb', line 96

def self.apply_shell_filters(options, filters, shell:, type:)
  raise if shell.nil?

  filters[:shell_expect] = type == 'expect'
  if shell.empty? && options[:bash_only]
    filters[:shell_exclude] = true
    return
  end

  if shell.present? && options[:select_by_shell_regex].present?
    filters[:shell_select] =
      !!(shell =~ Regexp.new(options[:select_by_shell_regex]))
  end

  return unless shell.present? && options[:exclude_by_shell_regex].present?

  filters[:shell_exclude] =
    !!(shell =~ Regexp.new(options[:exclude_by_shell_regex]))
end

.evaluate_filters(options, filters) ⇒ Boolean

Evaluates the filter settings to make a final decision on whether to include or exclude a fenced code block (FCB) based on the provided options.

if it should be excluded.

Parameters:

  • options (Hash)

    The options used for filtering FCBs.

  • filters (Hash)

    The filter settings to be evaluated.

Returns:

  • (Boolean)

    True if the FCB should be included; false



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/filter.rb', line 158

def self.evaluate_filters(options, filters)
  if filters[:depth] == true
    false
  elsif filters[:fcb_chrome] == true
    !options[:no_chrome]
  elsif options[:exclude_expect_blocks] && filters[:shell_expect] == true
    false
  elsif filters[:hidden_name] == true
    false
  elsif filters[:include_name] == true
    true
  elsif filters[:wrap_name] == true
    true
  elsif filters[:name_exclude] == true || filters[:shell_exclude] == true ||
        filters[:name_select] == false || filters[:shell_select] == false
    false
  elsif filters[:name_select] == true || filters[:shell_select] == true
    true
  elsif filters[:name_default] == false || filters[:shell_default] == false
    false
  else
    true
  end
end

.fcb_select?(options, fcb) ⇒ Boolean

Determines whether to include or exclude a fenced code block (FCB) based on the provided options.

it should be excluded.

Parameters:

  • options (Hash)

    The options used for filtering FCBs.

  • fcb (Hash)

    The fenced code block to be evaluated.

Returns:

  • (Boolean)

    True if the FCB should be included; false if

Raises:

  • (StandardError)

    If an error occurs during the evaluation.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/filter.rb', line 36

def self.fcb_select?(options, fcb)
  filters = {
    name_default: true,
    name_exclude: nil,
    name_select: nil,
    shell_default: true,
    shell_exclude: nil,
    shell_select: nil,
    hidden_name: nil,
    include_name: nil,
    wrap_name: nil
  }

  apply_name_filters(options, filters, fcb.oname)
  apply_shell_filters(
    options, filters, shell: fcb.shell || '', type: fcb.type || ''
  )
  apply_other_filters(options, filters, fcb)

  evaluate_filters(options, filters)
rescue StandardError
  warn("ERROR ** Filter::fcb_select?(); #{$!.inspect}")
  raise ArgumentError, $!
end

.prepared_not_in_menu?(options, fcb, match_patterns) ⇒ Boolean

check if a block is not in the menu based on multiple match patterns

Parameters:

  • options (Hash)

    Options hash containing various settings

  • fcb (Hash)

    Hash representing a file code block

  • match_patterns (Array<String>)

    Array of regular expression patterns for matching

Returns:

  • (Boolean)

    True if the block should not be in the menu, false otherwise



191
192
193
194
195
196
197
# File 'lib/filter.rb', line 191

def self.prepared_not_in_menu?(options, fcb, match_patterns)
  return false unless fcb.type == BlockType::SHELL

  match_patterns.any? do |pattern|
    options[pattern].present? && fcb.oname =~ /#{options[pattern]}/
  end
end

.yield_to_block_if_applicable(fcb, selected_types, configuration = {}, &block) ⇒ Object

Yields to the provided block with specified parameters if

certain conditions are met.

The method checks if a block is given, if the selected_types

include :blocks,

and if the fcb_select? method returns true for the given fcb.

Parameters:

  • fcb (Object)

    The object to be evaluated and potentially passed to the block.

  • selected_types (Array<Symbol>)

    A collection of message types, one of which must be :blocks.

  • block (Block)

    A block to be called if conditions are met.



210
211
212
213
214
215
216
217
# File 'lib/filter.rb', line 210

def self.yield_to_block_if_applicable(fcb, selected_types,
                                      configuration = {}, &block)
  if block_given? &&
     block_type_selected?(selected_types, :blocks) &&
     fcb_select?(configuration, fcb)
    block.call :blocks, fcb
  end
end