Class: YARD::Spellcheck::Checker
- Inherits:
-
Object
- Object
- YARD::Spellcheck::Checker
- 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
-
#added ⇒ Object
readonly
The words to add to the dictionary.
-
#ignore ⇒ Object
readonly
The words to ignore.
-
#lang ⇒ Object
The language to spellcheck against.
-
#misspelled ⇒ Object
readonly
The misspelled words.
Instance Method Summary collapse
-
#check!(names = []) {|element, typos| ... } ⇒ Enumerator
Spellchecks the YARD Documentation.
-
#initialize(options = {}) ⇒ Checker
constructor
Initializes the spellchecker.
-
#spellcheck(text, dict) ⇒ Set<String>
protected
Spellchecks a piece of text.
-
#spellcheck_object(obj, dict) {|element, typos| ... } ⇒ Object
protected
Spellchecks a YARD Documentation object.
Constructor Details
#initialize(options = {}) ⇒ Checker
Initializes the spellchecker.
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(={}) @lang = .fetch(:lang,FFI::Hunspell.lang) @ignore = Set[] @added = Set[] if [:ignore] @ignore += [:ignore] end if [:add] @added += [:add] end @misspelled = Hash.new { |hash,key| hash[key] = 0 } end |
Instance Attribute Details
#added ⇒ Object (readonly)
The words to add to the dictionary.
30 31 32 |
# File 'lib/yard/spellcheck/checker.rb', line 30 def added @added end |
#ignore ⇒ Object (readonly)
The words to ignore.
27 28 29 |
# File 'lib/yard/spellcheck/checker.rb', line 27 def ignore @ignore end |
#lang ⇒ Object
The language to spellcheck against.
24 25 26 |
# File 'lib/yard/spellcheck/checker.rb', line 24 def lang @lang end |
#misspelled ⇒ Object (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.
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.
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.
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..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 |