Class: Mdlint::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/mdlint/config.rb,
sig/internal.rbs

Constant Summary collapse

CONFIG_FILES =
%w[
  .mdlint.yml
  .mdlint.yaml
  mdlint.yml
  mdlint.yaml
].freeze
DEFAULT_OPTIONS =
{
  check: false,
  diff: false,
  quiet: false,
  exclude: [],
  rules: nil,
  disable: [],
  severity: nil,
  fail_level: :warning,
  format: nil,
  dialect: :commonmark,
  preset: nil,
  plugins: [],
  check_links: false,
  check_external_links: false,
  check_code_blocks: false,
  code_block_commands: {},
  code_block_format_commands: {},
  code_block_timeout: 10,
  toc: false,
  table_align: true,
  jobs: 1,
  cache: false,
  cache_path: ".mdlint_cache"
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base_path = Dir.pwd) ⇒ Config

Returns a new instance of Config.



42
43
44
45
# File 'lib/mdlint/config.rb', line 42

def initialize(base_path = Dir.pwd)
  @base_path = base_path
  @options = DEFAULT_OPTIONS.dup
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



40
41
42
# File 'lib/mdlint/config.rb', line 40

def options
  @options
end

Instance Method Details

#find_config_fileString?

Returns:

  • (String, nil)


72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/mdlint/config.rb', line 72

def find_config_file
  current = @base_path

  loop do
    CONFIG_FILES.each do |name|
      path = File.join(current, name)
      return path if File.exist?(path)
    end

    parent = File.dirname(current)
    break if parent == current

    current = parent
  end

  nil
end

#loadObject

Returns:

  • (Object)


47
48
49
50
51
52
53
54
# File 'lib/mdlint/config.rb', line 47

def load
  config_file = find_config_file
  return @options unless config_file

  file_options = load_config_file(config_file)
  merge_options(file_options)
  @options
end

#load_config_file(path) ⇒ Object

Parameters:

  • path (Object)

Returns:

  • (Object)


90
91
92
93
94
95
96
97
# File 'lib/mdlint/config.rb', line 90

def load_config_file(path)
  content = File.read(path)
  parsed = YAML.safe_load(content, symbolize_names: true) || {}
  normalize_options(parsed)
rescue Psych::SyntaxError => e
  warn "Warning: Invalid YAML in #{path}: #{e.message}"
  {}
end

#merge(cli_options) ⇒ Object

Parameters:

  • cli_options (Object)

Returns:

  • (Object)


56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mdlint/config.rb', line 56

def merge(cli_options)
  cli_options.each do |key, value|
    case key
    when :exclude
      @options[:exclude] = (@options[:exclude] + value).uniq
    when :disable, :plugins
      @options[key] = (@options[key] + Array(value)).uniq
    else
      @options[key] = value unless value.nil?
    end
  end
  @options
end

#merge_options(file_options) ⇒ Object

Parameters:

  • file_options (Object)

Returns:

  • (Object)


176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/mdlint/config.rb', line 176

def merge_options(file_options)
  file_options.each do |key, value|
    case key
    when :exclude
      @options[:exclude] = (@options[:exclude] + value).uniq
    when :disable, :plugins
      @options[key] = (@options[key] + Array(value)).uniq
    else
      @options[key] = value
    end
  end
end

#normalize_command_map(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


163
164
165
166
167
# File 'lib/mdlint/config.rb', line 163

def normalize_command_map(value)
  return {} unless value.is_a?(Hash)

  value.to_h { |language, command| [language.to_s.downcase, command.to_s] }
end

#normalize_options(parsed) ⇒ Object

Parameters:

  • parsed (Object)

Returns:

  • (Object)


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/mdlint/config.rb', line 99

def normalize_options(parsed)
  options = {}

  options[:check] = parsed[:check] if parsed.key?(:check)
  options[:diff] = parsed[:diff] if parsed.key?(:diff)
  options[:quiet] = parsed[:quiet] if parsed.key?(:quiet)
  options[:wrap] = normalize_wrap(parsed[:wrap]) if parsed.key?(:wrap)
  options[:number] = parsed[:number] if parsed.key?(:number)
  options[:end_of_line] = parsed[:end_of_line].to_s.downcase.to_sym if parsed[:end_of_line]
  options[:rules] = normalize_rules(parsed[:rules]) if parsed.key?(:rules)

  if parsed[:disable]
    options[:disable] = Array(parsed[:disable]).map(&:to_s)
  end

  options[:severity] = parsed[:severity].to_sym if parsed[:severity]
  options[:fail_level] = parsed[:fail_level].to_sym if parsed[:fail_level]
  options[:format] = parsed[:format].to_s if parsed[:format]
  options[:dialect] = parsed[:dialect].to_sym if parsed[:dialect]
  options[:preset] = parsed[:preset].to_sym if parsed[:preset]
  options[:plugins] = Array(parsed[:plugins]).map(&:to_s) if parsed[:plugins]
  options[:check_links] = parsed[:check_links] if parsed.key?(:check_links)
  options[:check_external_links] = parsed[:check_external_links] if parsed.key?(:check_external_links)
  options[:check_code_blocks] = parsed[:check_code_blocks] if parsed.key?(:check_code_blocks)
  options[:code_block_commands] = normalize_command_map(parsed[:code_block_commands]) if parsed[:code_block_commands]
  options[:code_block_format_commands] = normalize_command_map(parsed[:code_block_format_commands]) if parsed[:code_block_format_commands]
  options[:code_block_timeout] = normalize_timeout(parsed[:code_block_timeout]) if parsed.key?(:code_block_timeout)
  options[:toc] = parsed[:toc] if parsed.key?(:toc)
  options[:table_align] = parsed[:table_align] if parsed.key?(:table_align)
  options[:jobs] = parsed[:jobs].to_i if parsed[:jobs]
  options[:cache] = parsed[:cache] if parsed.key?(:cache)
  options[:cache_path] = parsed[:cache_path].to_s if parsed[:cache_path]

  if parsed[:exclude]
    options[:exclude] = Array(parsed[:exclude])
  end

  options
end

#normalize_rules(rules) ⇒ Object

Parameters:

  • rules (Object)

Returns:

  • (Object)


139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/mdlint/config.rb', line 139

def normalize_rules(rules)
  return rules unless rules.is_a?(Hash)

  rules.each_with_object({}) do |(rule_id, setting), normalized|
    normalized[rule_id.to_s] = if setting.is_a?(Hash)
                                 normalized_setting = setting.transform_keys(&:to_sym)
                                 normalized_setting[:severity] = normalized_setting[:severity].to_sym if normalized_setting[:severity]
                                 normalized_setting
                               else
                                 setting
                               end
  end
end

#normalize_timeout(value) ⇒ Integer

Parameters:

  • value (Object)

Returns:

  • (Integer)


169
170
171
172
173
174
# File 'lib/mdlint/config.rb', line 169

def normalize_timeout(value)
  timeout = Integer(value)
  timeout.positive? ? timeout : DEFAULT_OPTIONS[:code_block_timeout]
rescue ArgumentError, TypeError
  DEFAULT_OPTIONS[:code_block_timeout]
end

#normalize_wrap(value) ⇒ Object

Parameters:

  • value (Object)

Returns:

  • (Object)


153
154
155
156
157
158
159
160
161
# File 'lib/mdlint/config.rb', line 153

def normalize_wrap(value)
  case value.to_s.downcase
  when "keep" then :keep
  when "no" then :no
  else Integer(value)
  end
rescue ArgumentError, TypeError
  :keep
end