class MarkdownLint::CLI

Constants

CONFIG_FILE

Public Class Methods

probe_config_file(path) click to toggle source
# File lib/mdl/cli.rb, line 162
def self.probe_config_file(path)
  expanded_path = File.expand_path(path)
  return expanded_path if File.exist?(expanded_path)

  # Look for a file up from the working dir
  Pathname.new(expanded_path).ascend do |p|
    next unless p.directory?

    config_file = p.join(CONFIG_FILE)
    return config_file if File.exist?(config_file)
  end
  nil
end
toggle_list(parts, to_sym=false) click to toggle source
# File lib/mdl/cli.rb, line 143
def self.toggle_list(parts, to_sym=false)
  if parts.class == String
    parts = parts.split(',')
  end
  if parts.class == Array
    inc = parts.select{|p| not p.start_with?('~')}
    exc = parts.select{|p| p.start_with?('~')}.map{|p| p[1..-1]}
    if to_sym
      inc.map!{|p| p.to_sym}
      exc.map!{|p| p.to_sym}
    end
    {:include => inc, :exclude => exc}
  else
    # We already converted the string into a list of include/exclude
    # pairs, so just return as is
    parts
  end
end

Public Instance Methods

run(argv=ARGV) click to toggle source
# File lib/mdl/cli.rb, line 107
def run(argv=ARGV)
  parse_options(argv)

  # Load the config file if it's present
  filename = CLI.probe_config_file(config[:config_file])

  # Only fall back to ~/.mdlrc if we are using the default value for -c
  if filename.nil? and config[:config_file] == CONFIG_FILE
    filename = File.expand_path("~/#{CONFIG_FILE}")
  end

  if not filename.nil? and File.exist?(filename)
    MarkdownLint::Config.from_file(filename.to_s)
    if config[:verbose]
      puts "Loaded config from #{filename}"
    end
  end

  # Put values in the config file
  MarkdownLint::Config.merge!(config)

  # Set the correct format for any rules/tags configuration loaded from
  # the config file. Ideally this would probably be done as part of the
  # config class itself rather than here.
  MarkdownLint::Config[:rules] = CLI.toggle_list(
    MarkdownLint::Config[:rules]) unless MarkdownLint::Config[:rules].nil?
  MarkdownLint::Config[:tags] = CLI.toggle_list(
    MarkdownLint::Config[:tags], true) \
    unless MarkdownLint::Config[:tags].nil?

  # Read from stdin if we didn't provide a filename
  if cli_arguments.empty? and not config[:list_rules]
    cli_arguments << "-"
  end
end