Class: Clausewitz::Spelling::Checker

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

Constant Summary collapse

DEFAULT_SUGGESTION_COUNT =
3

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Checker

Returns a new instance of Checker.



16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/clausewitz/spelling/checker.rb', line 16

def initialize(opts = {})
  @custom_dict_root = opts[:custom_dict_root]
  @custom_dict_root = Pathname.new(@custom_dict_root) if @custom_dict_root
  @custom_dicts     = opts[:custom_dicts]     || []
  @dialect_map      = opts[:dialect_map]      || {}
  @suggestion_count = opts[:suggestion_count] || DEFAULT_SUGGESTION_COUNT
  @verbose          = opts[:verbose]
  @commit_range     = opts[:commit_range]

  @check_cache = {}

  load_dictionaries!
end

Instance Method Details

#check_file(filepath) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/clausewitz/spelling/checker.rb', line 53

def check_file(filepath)
  $stderr.puts "Checking file '#{filepath}'..." if @verbose
  results = []
  begin
    filepath = Pathname.new(filepath)
    validate_filepath!(filepath)
  rescue => e
    return InvalidFilepathResult.new(filepath, e)
  end

  $stderr.puts "Skipping directory '#{filepath}'..." if filepath.directory?

  begin
    contents = Clausewitz::Localisation.parse_file(filepath)
  rescue Clausewitz::Localisation::UnparseableFileError => e
    return UnparseableFileResult.new(filepath, e.errors)
  rescue => e
    return UnparseableFileResult.new(filepath, e)
  end

  changed_keys = Set.new
  if @commit_range
    diff = `git diff -U0 '#{@commit_range}' '#{filepath}' 2>/dev/null`
    diff = diff.force_encoding('UTF-8')
    changed_lines = diff.lines.select { |line| line =~ /^\+ / }
    changed_lines.each do |line|
      match = /\+  ([\w\d.'_-]+):([0-9]+)? \"/.match(line)
      next unless match
      if match[2]
        changed_keys.add(match[1] + ':' + match[2])
      else
        changed_keys.add(match[1])
      end
    end
  end

  checks = contents.map do |lang_name, entries|
    lc = language_config(lang_name)
    ignore = []
    if @commit_range
      ignore = entries.keys.select do |key|
        !changed_keys.include?(key)
      end
    end
    check_entries(entries, lc, ignore)
  end
  FileResults.new(filepath, checks)
end

#load_dictionaries!Object



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

def load_dictionaries!
  @loaded_dicts = {}
  Localisation::LANG_MAP.each do |_, config|
    if @dialect_map.key?(config.name)
      config.select_dialect(@dialect_map[config.name])
    end

    dict = FFI::Hunspell.dict(config.full_name)

    @custom_dicts.each do |custom_dict|
      path = @custom_dict_root.join("#{config.full_name}_#{custom_dict}")
      path = Pathname.new("#{path}.dic")
      if path.exist?
        dict.add_dic(path.to_s)
      else
        $stderr.puts("Could not load dictionary '#{path}', skipping...")
      end
    end

    @loaded_dicts[config.name] = dict
  end
end