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.



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/filter.rb', line 59

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 =~ /#{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 =~ /#{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.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/filter.rb', line 110

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

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

  filters[:shell_default] = (shell == BlockType::BASH)
end

.apply_shell_filters(options, filters, shell) ⇒ 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.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/filter.rb', line 84

def self.apply_shell_filters(options, filters, shell)
  filters[:shell_expect] = shell == '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 =~ /#{options[:select_by_shell_regex]}/)
  end

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

  filters[:shell_exclude] = !!(shell =~ /#{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



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/filter.rb', line 139

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.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/filter.rb', line 25

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
  }

  name = fcb.oname
  shell = fcb.fetch(:shell, '')

  apply_name_filters(options, filters, name)
  apply_shell_filters(options, filters, shell)
  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



170
171
172
173
174
175
176
# File 'lib/filter.rb', line 170

def self.prepared_not_in_menu?(options, fcb, match_patterns)
  return false unless fcb[:shell] == BlockType::BASH

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

.yield_to_block_if_applicable(fcb, selected_messages, 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_messages 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_messages (Array<Symbol>)

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

  • block (Block)

    A block to be called if conditions are met.



185
186
187
188
189
190
# File 'lib/filter.rb', line 185

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