Class: Mdlint::Config
- Inherits:
-
Object
- Object
- Mdlint::Config
- 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
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #find_config_file ⇒ String?
-
#initialize(base_path = Dir.pwd) ⇒ Config
constructor
A new instance of Config.
- #load ⇒ Object
- #load_config_file(path) ⇒ Object
- #merge(cli_options) ⇒ Object
- #merge_options(file_options) ⇒ Object
- #normalize_command_map(value) ⇒ Object
- #normalize_options(parsed) ⇒ Object
- #normalize_rules(rules) ⇒ Object
- #normalize_timeout(value) ⇒ Integer
- #normalize_wrap(value) ⇒ Object
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
#options ⇒ Object (readonly)
Returns the value of attribute options.
40 41 42 |
# File 'lib/mdlint/config.rb', line 40 def @options end |
Instance Method Details
#find_config_file ⇒ String?
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 |
#load ⇒ 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 = load_config_file(config_file) () @options end |
#load_config_file(path) ⇒ 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) || {} (parsed) rescue Psych::SyntaxError => e warn "Warning: Invalid YAML in #{path}: #{e.}" {} end |
#merge(cli_options) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/mdlint/config.rb', line 56 def merge() .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
176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/mdlint/config.rb', line 176 def () .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
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
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 (parsed) = {} [:check] = parsed[:check] if parsed.key?(:check) [:diff] = parsed[:diff] if parsed.key?(:diff) [:quiet] = parsed[:quiet] if parsed.key?(:quiet) [:wrap] = normalize_wrap(parsed[:wrap]) if parsed.key?(:wrap) [:number] = parsed[:number] if parsed.key?(:number) [:end_of_line] = parsed[:end_of_line].to_s.downcase.to_sym if parsed[:end_of_line] [:rules] = normalize_rules(parsed[:rules]) if parsed.key?(:rules) if parsed[:disable] [:disable] = Array(parsed[:disable]).map(&:to_s) end [:severity] = parsed[:severity].to_sym if parsed[:severity] [:fail_level] = parsed[:fail_level].to_sym if parsed[:fail_level] [:format] = parsed[:format].to_s if parsed[:format] [:dialect] = parsed[:dialect].to_sym if parsed[:dialect] [:preset] = parsed[:preset].to_sym if parsed[:preset] [:plugins] = Array(parsed[:plugins]).map(&:to_s) if parsed[:plugins] [:check_links] = parsed[:check_links] if parsed.key?(:check_links) [:check_external_links] = parsed[:check_external_links] if parsed.key?(:check_external_links) [:check_code_blocks] = parsed[:check_code_blocks] if parsed.key?(:check_code_blocks) [:code_block_commands] = normalize_command_map(parsed[:code_block_commands]) if parsed[:code_block_commands] [:code_block_format_commands] = normalize_command_map(parsed[:code_block_format_commands]) if parsed[:code_block_format_commands] [:code_block_timeout] = normalize_timeout(parsed[:code_block_timeout]) if parsed.key?(:code_block_timeout) [:toc] = parsed[:toc] if parsed.key?(:toc) [:table_align] = parsed[:table_align] if parsed.key?(:table_align) [:jobs] = parsed[:jobs].to_i if parsed[:jobs] [:cache] = parsed[:cache] if parsed.key?(:cache) [:cache_path] = parsed[:cache_path].to_s if parsed[:cache_path] if parsed[:exclude] [:exclude] = Array(parsed[:exclude]) end end |
#normalize_rules(rules) ⇒ 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
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
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 |