Class: Distincter2::D2Checker
- Inherits:
-
Object
- Object
- Distincter2::D2Checker
- Defined in:
- lib/distincter2/checker.rb
Overview
Class to check for duplicate lines in markdown files
Instance Method Summary collapse
-
#analyze_dir(path) ⇒ Array
Recursively analyze a directory for duplicate lines in markdown files.
-
#analyze_file(path) ⇒ Array
Analyze a markdown file for duplicate lines.
-
#can_analyze(entry) ⇒ Boolean
Check if a file can be analyzed based on the configuration settings.
-
#check(path) ⇒ Array
Check for duplicate lines in markdown files within a given directory.
-
#initialize(config, mute: false) ⇒ D2Checker
constructor
Initialize the D2Checker with a configuration object and an optional mute flag.
Constructor Details
#initialize(config, mute: false) ⇒ D2Checker
Initialize the D2Checker with a configuration object and an optional mute flag
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
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
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
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
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 |