Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added personal wordlist saving via --spell-add-words

  • Loading branch information...
commit ada3fcc098d46a5ad92f2096e4aea4b2a617fb0e 1 parent 5be4781
@drbrain drbrain authored
View
46 lib/rdoc/generator/spellcheck.rb
@@ -22,6 +22,7 @@ class RDoc::Generator::Spellcheck
# Please keep this list sorted in your pull requests
DEFAULT_WORDS = %w[
+ http
https
newb
sudo
@@ -41,8 +42,9 @@ class RDoc::Generator::Spellcheck
def self.setup_options options
default_language, = ENV['LANG'].split '.'
- options.spell_language = default_language
- options.quiet = true # suppress statistics
+ options.spell_add_words = false
+ options.spell_language = default_language
+ options.quiet = true # suppress statistics
op = options.option_parser
@@ -57,8 +59,33 @@ def self.setup_options options
language
end
+ op.separator nil
+ op.separator 'Spellcheck options:'
+ op.separator nil
+
+ op.on('--spell-add-words [WORDLIST]',
+ 'Adds words to the aspell personal wordlist.',
+ 'The word list may be a comma-separated',
+ 'list of words which must contain multiple',
+ 'words, a file or empty to read words from',
+ 'stdin') do |wordlist|
+ words = if wordlist.nil? then
+ $stdin.read.split
+ elsif wordlist =~ /,/ then
+ wordlist.split ','
+ else
+ open wordlist do |io|
+ io.read.split
+ end
+ end
+
+ options.spell_add_words = words
+ end
+
+ op.separator nil
+
op.on('--spell-language=LANGUAGE', SpellLanguage,
- 'Language to use for spell checking',
+ 'Language to use for spell checking.',
"The default language is #{default_language}") do |language|
options.spell_language = language
end
@@ -72,6 +99,14 @@ def initialize options # :not-new:
@spell = Aspell.new @options.spell_language
@spell.suggestion_mode = Aspell::NORMAL
@spell.set_option 'run-together', 'true'
+
+ if words = @options.spell_add_words then
+ words.each do |word|
+ @spell.add_to_personal word
+ end
+
+ @spell.save_all_word_lists
+ end
end
##
@@ -261,6 +296,11 @@ def suggestion_text text, word, offset
class RDoc::Options
##
+ # Enables addition of words to the personal wordlist
+
+ attr_accessor :spell_add_words
+
+ ##
# The Aspell dictionary language to use. Defaults to the language in the
# LANG environment variable.
View
68 test/test_rdoc_generator_spellcheck.rb
@@ -1,5 +1,7 @@
require 'rubygems'
require 'minitest/autorun'
+require 'stringio'
+require 'tempfile'
gem 'rdoc', '~> 3.12'
@@ -38,12 +40,58 @@ def test_class_setup_options_default
options.parse %w[--format spellcheck]
+ refute options.spell_add_words
assert_equal 'en_US', options.spell_language
assert options.quiet
ensure
ENV['LANG'] = orig_lang
end
+ def test_class_setup_options_spell_add_words
+ $stdin = StringIO.new "foo bar\nbaz"
+
+ options = RDoc::Options.new
+
+ options.parse %w[
+ --format spellcheck
+ --no-ignore-invalid
+ --spell-add-words
+ ]
+
+ assert_equal %w[foo bar baz], options.spell_add_words
+ ensure
+ $stdin = STDIN
+ end
+
+ def test_class_setup_options_spell_add_words_list
+ options = RDoc::Options.new
+
+ options.parse %w[
+ --format spellcheck
+ --no-ignore-invalid
+ --spell-add-words foo,bar
+ ]
+
+ assert_equal %w[foo bar], options.spell_add_words
+ end
+
+ def test_class_setup_options_spell_add_words_file
+ Tempfile.open 'words.txt' do |io|
+ io.write "foo bar\nbaz"
+ io.rewind
+
+ options = RDoc::Options.new
+
+ options.parse %W[
+ --format spellcheck
+ --no-ignore-invalid
+ --spell-add-words #{io.path}
+ ]
+
+ assert_equal %w[foo bar baz], options.spell_add_words
+ end
+ end
+
def test_class_setup_options_spell_language
options = RDoc::Options.new
@@ -56,6 +104,26 @@ def test_class_setup_options_spell_language
assert_equal 'en_GB', options.spell_language
end
+ def test_initialize_add_words
+ orig_aspell_conf = ENV['ASPELL_CONF']
+
+ Tempfile.open 'personal_wordlist' do |wordlist|
+ Tempfile.open 'personal_repl' do |repl|
+ ENV['ASPELL_CONF'] = "personal #{wordlist.path};repl #{repl.path}"
+ @options.spell_add_words = %w[funkify thingus]
+
+ sc = @SC.new @options
+
+ spell = Aspell.new 'en_US'
+
+ assert spell.check('funkify'), 'funkify not added to personal wordlist'
+ assert spell.check('thingus'), 'thingus not added to personal wordlist'
+ end
+ end
+ ensure
+ ENV['ASPELL_CONF'] = orig_aspell_conf
+ end
+
def test_find_misspelled
c = comment @text
Please sign in to comment.
Something went wrong with that request. Please try again.