Class: Distincter2::D2Checker

Inherits:
Object
  • Object
show all
Defined in:
lib/distincter2/checker.rb

Overview

Class to check for duplicate lines in markdown files

Instance Method Summary collapse

Constructor Details

#initialize(config, mute: false) ⇒ D2Checker

Initialize the D2Checker with a configuration object and an optional mute flag

Parameters:

  • config (Object)

    The configuration object containing settings for the checker

  • mute (Boolean) (defaults to: false)

    A flag indicating whether to mute the output of duplicate lines (default: false)



10
11
12
13
# File 'lib/distincter2/checker.rb', line 10

def initialize(config, mute: false)
  @config = config
  @mute = mute
end

Instance Method Details

#analyze_dir(path) ⇒ Array

Recursively analyze a directory for duplicate lines in markdown files

Parameters:

  • path (String)

    The path to the directory to analyze

Returns:

  • (Array)

    An array of duplicate lines found in the markdown files



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/distincter2/checker.rb', line 29

def analyze_dir(path)
  duplicates = []
  ::Dir.foreach(path).each do |entry|
    next if entry.start_with?('.')

    entry_name = ::File.basename(entry)

    file = "#{::File.absolute_path(path)}/#{entry_name}"
    if ::File.directory?(file)
      analyze_result = analyze_dir(file)
    else
      next unless entry.end_with?('.md')

      next unless can_analyze(entry_name)

      analyze_result = analyze_file(file)
    end

    duplicates += analyze_result unless analyze_result.empty?
  end

  duplicates
end

#analyze_file(path) ⇒ Array

Analyze a markdown file for duplicate lines

Parameters:

  • path (String)

    The path to the markdown file to analyze

Returns:

  • (Array)

    An array of duplicate lines found in the markdown file



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/distincter2/checker.rb', line 67

def analyze_file(path)
  lines = []
  ::File.open(path, 'r') do |file|
    file.each_line do |line|
      lines << line if !line.empty? && line.start_with?('-')
    end
  end

  duplicates = lines.select { |line| lines.count(line) > 1 }
                    .uniq
  unless @mute
    duplicates.each do |duplicate|
      puts("#{path} : #{duplicate}")
    end
  end

  duplicates
end

#can_analyze(entry) ⇒ Boolean

Check if a file can be analyzed based on the configuration settings

Parameters:

  • entry (String)

    The name of the file to check

Returns:

  • (Boolean)

    True if the file can be analyzed, false otherwise



57
58
59
60
61
# File 'lib/distincter2/checker.rb', line 57

def can_analyze(entry)
  return false unless @config.version == 'v1'

  @config.exclude_paths.none?(entry)
end

#check(path) ⇒ Array

Check for duplicate lines in markdown files within a given directory

Parameters:

  • path (String)

    The path to the directory to check

Returns:

  • (Array)

    An array of duplicate lines found in the markdown files



19
20
21
22
23
# File 'lib/distincter2/checker.rb', line 19

def check(path)
  duplicates = analyze_dir(path)

  exit(duplicates.empty? ? 0 : 1)
end