Class: YARD::Spellcheck::Checker

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

Overview

Handles loading and spellchecking YARD Documentation.

Constant Summary collapse

SKIP_TAGS =

YARD tags to not spellcheck.

Set['example', 'since', 'see', 'api']
WORD_REGEXP =

The Regexp to use for scanning in words.

/[^\W_][\w'-]*[^\W_]/
ACRONYM_REGEXP =

The Regexp to filter out Acronyms.

/(([A-Z]\.){2,}|[A-Z]{2,})/
CAMEL_CASE_REGEXP =

The Regexp to filter out CamelCase words.

/([A-Z][a-z]+){2,}/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Checker

Initializes the spellchecker.

Parameters:

  • options (Hash) (defaults to: {})

    Additional options.

Options Hash (options):

  • :lang (String, Symbol) — default: FFI::Hunspell.lang

    The language to spellcheck against.

  • :ignore (Array<String>, Set<String>)

    The words to ignore.

  • :add (Array<String>)

    The words to add to the dictionary.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yard/spellcheck/checker.rb', line 50

def initialize(options={})
  @lang = options.fetch(:lang,FFI::Hunspell.lang)
  @ignore = Set[]
  @added = Set[]

  if options[:ignore]
    @ignore += options[:ignore]
  end

  if options[:add]
    @added += options[:add]
  end

  @misspelled = Hash.new { |hash,key| hash[key] = 0 }
end

Instance Attribute Details

#addedObject (readonly)

The words to add to the dictionary.



30
31
32
# File 'lib/yard/spellcheck/checker.rb', line 30

def added
  @added
end

#ignoreObject (readonly)

The words to ignore.



27
28
29
# File 'lib/yard/spellcheck/checker.rb', line 27

def ignore
  @ignore
end

#langObject

The language to spellcheck against.



24
25
26
# File 'lib/yard/spellcheck/checker.rb', line 24

def lang
  @lang
end

#misspelledObject (readonly)

The misspelled words.



33
34
35
# File 'lib/yard/spellcheck/checker.rb', line 33

def misspelled
  @misspelled
end

Instance Method Details

#check!(names = []) {|element, typos| ... } ⇒ Enumerator

Spellchecks the YARD Documentation.

Parameters:

  • names (Array<String>) (defaults to: [])

    The Classes/Modules to spellcheck.

Yields:

  • (element, typos)

    The given block will be passed each element and any typos found.

Yield Parameters:

  • element (YARD::Docstring, YARD::Tag)

    An element from the YARD Documentation.

  • typos (Set<String>)

    Any typos found within the element.

Returns:

  • (Enumerator)

    If no block is given, an Enumerator will be returned.



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/yard/spellcheck/checker.rb', line 84

def check!(names=[],&block)
  return enum_for(:check!) unless block

  # load the YARD cache
  YARD::Registry.load!

  # clear any statistics from last run
  @misspelled.clear

  FFI::Hunspell.dict(@lang) do |dict|
    # add user specified words
    @added.each { |word| dict.add(word.dup) }

    unless names.empty?
      names.each do |name|
        if (obj = YARD::Registry.at(name))
          spellcheck_object(obj,dict,&block)
        end
      end
    else
      YARD::Registry.each do |obj| 
        spellcheck_object(obj,dict,&block)
      end
    end
  end
end

#spellcheck(text, dict) ⇒ Set<String> (protected)

Spellchecks a piece of text.

Parameters:

  • text (String)

    The text to spellcheck.

  • dict (FFI::Hunspell::Dictionary)

    The dictionary to use.

Returns:

  • (Set<String>)

    The misspelled words from the text.



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/yard/spellcheck/checker.rb', line 161

def spellcheck(text,dict)
  typos = Set[]

  text.each_line do |line|
    # ignore indented lines
    next if line =~ /^\s{2,}/

    line.scan(WORD_REGEXP).each do |word|
      # ignore all underscored names
      next if word.include?('_')

      # ignore all acronyms and CamelCase words
      next if (word =~ ACRONYM_REGEXP || word =~ CAMEL_CASE_REGEXP)

      # skip ignored words
      next if @ignore.include?(word)

      if (@misspelled.has_key?(word) || !dict.valid?(word))
        @misspelled[word] += 1

        typos << word
      end
    end
  end

  return typos
end

#spellcheck_object(obj, dict) {|element, typos| ... } ⇒ Object (protected)

Spellchecks a YARD Documentation object.

Parameters:

  • obj (YARD::CodeObject::Base)

    The YARD Documentation object.

  • dict (FFI::Hunspell::Dictionary)

    The dictionary to spellcheck against.

Yields:

  • (element, typos)

    The given block will be passed each element and any typos found.

Yield Parameters:

  • element (YARD::Docstring, YARD::Tag)

    An element from the YARD Documentation.

  • typos (Set<String>)

    Any typos found within the element.



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/yard/spellcheck/checker.rb', line 131

def spellcheck_object(obj,dict)
  docstring = obj.docstring

  unless (typos = spellcheck(docstring,dict)).empty?
    yield docstring, typos
  end

  docstring.tags.each do |tag|
    next if SKIP_TAGS.include?(tag.tag_name)

    if tag.text
      unless (typos = spellcheck(tag.text,dict)).empty?
        yield tag, typos
      end
    end
  end
end