Class: MdSpell::SpellChecker

Inherits:
Object
  • Object
show all
Defined in:
lib/mdspell/spell_checker.rb

Overview

A class for finding spelling errors in document.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ SpellChecker

Create a new instance from specified file.

Parameters:

  • filename (String)

    a name of file to load.



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/mdspell/spell_checker.rb', line 17

def initialize(filename)
  if filename == '-'
    @filename = 'stdin'
    text = STDIN.read
  else
    @filename = filename
    text = File.read(filename)
  end

  @document = Kramdown::Document.new(text, input: 'GFM')
end

Instance Attribute Details

#documentObject (readonly)

A Kramdown::Document object containing the parsed markdown document.



13
14
15
# File 'lib/mdspell/spell_checker.rb', line 13

def document
  @document
end

#filenameObject (readonly)

Name of the file this object was created from.



10
11
12
# File 'lib/mdspell/spell_checker.rb', line 10

def filename
  @filename
end

Instance Method Details

#typosObject

Returns found spelling errors.



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/mdspell/spell_checker.rb', line 30

def typos
  results = []
  FFI::Aspell::Speller.open(Configuration[:language]) do |speller|
    TextLine.scan(document).each do |line|
      line.words.each do |word|
        next if ignored? word
        unless speller.correct? word
          results << Typo.new(line, word, speller.suggestions(word))
        end
      end
    end
  end

  results
end