class Yast::SpellcheckTask

Defines a spellcheck rake task

Constants

CUSTOM_SPELL_CONFIG_FILE
GLOBAL_SPELL_CONFIG_FILE

Attributes

inside_block[R]

Public Class Methods

new() click to toggle source

define the Rake task in the constructor

Calls superclass method
# File lib/tasks/spellcheck_task.rb, line 42
def initialize
  super

  namespace :check do
    desc "Run spell checker (by default for *.md and *.html files)"
    task :spelling do
      run_task
    end
  end
end

Private Instance Methods

block_line?(line) click to toggle source
# File lib/tasks/spellcheck_task.rb, line 173
def block_line?(line)
  line =~ /^\s*```/
end
check_file(file) click to toggle source

check the file using the spellchecker @param file [String] file name @return [Boolean] true on success (no spelling error found)

# File lib/tasks/spellcheck_task.rb, line 141
def check_file(file)
  puts "Checking #{file}..." if verbose == true
  # spell check each line separately so we can report error locations properly
  lines = File.read(file).split("\n")

  success = true
  lines.each_with_index do |text, index|
    misspelled = misspelled_on_line(text)
    next if misspelled.empty?

    success = false
    print_misspelled(misspelled, index, text)
  end

  success
end
colorize?() click to toggle source

optionally colorize the misspelled words if the rainbow gem is present @return [Boolean] true when the colorization support is present

# File lib/tasks/spellcheck_task.rb, line 57
def colorize?
  return @colorize unless @colorize.nil?

  begin
    require "rainbow"
    @colorize = true
  rescue LoadError
    @colorize = false
  end
end
config() click to toggle source

return the merged global and the custom spell configs @return [Hash] the merged configuration to use

# File lib/tasks/spellcheck_task.rb, line 121
def config
  return @config if @config

  @config = read_spell_config(GLOBAL_SPELL_CONFIG_FILE)
  custom_config = read_spell_config(CUSTOM_SPELL_CONFIG_FILE)

  report_duplicates(config["dictionary"], custom_config["dictionary"].to_a)

  custom_config["dictionary"] = @config["dictionary"] + custom_config["dictionary"].to_a
  custom_config["dictionary"].uniq!

  # override the global values by the local if present
  @config.merge!(custom_config)

  @config
end
files_to_check() click to toggle source

evaluate the files to check @return [Array<String>] list of files

# File lib/tasks/spellcheck_task.rb, line 92
def files_to_check
  files = config["check"].reduce([]) { |a, e| a + Dir[e] }
  config["ignore"].reduce(files) { |a, e| a - Dir[e] }
end
misspelled_on_line(text) click to toggle source
# File lib/tasks/spellcheck_task.rb, line 166
def misspelled_on_line(text)
  switch_block_tag if block_line?(text)
  return [] if inside_block

  speller.list_misspelled([text]) - config["dictionary"]
end
print_misspelled(list, index, text) click to toggle source
read_spell_config(file) click to toggle source

read a Yaml config file

# File lib/tasks/spellcheck_task.rb, line 98
def read_spell_config(file)
  return {} unless File.exist?(file)

  puts "Loading config file (#{file})..." if verbose == true
  require "yaml"
  YAML.load_file(file)
end
report_duplicates(dict1, dict2) click to toggle source

print the duplicate dictionary entries @param dict1 [Array<String>] the first dictionary @param dict2 [Array<String>] the second dictionary

# File lib/tasks/spellcheck_task.rb, line 109
def report_duplicates(dict1, dict2)
  duplicates = dict1 & dict2
  return if duplicates.empty?

  warn "Warning: Found dictionary duplicates in the local dictionary " \
       "(#{CUSTOM_SPELL_CONFIG_FILE}):\n"
  duplicates.each { |duplicate| warn "  #{duplicate}" }
  $stderr.puts
end
run_task() click to toggle source

run the task

# File lib/tasks/spellcheck_task.rb, line 184
def run_task
  if files_to_check.all? { |file| check_file(file) }
    puts "Spelling OK."
  else
    warn "Spellcheck failed! (Fix it or add the words to " \
         "'#{CUSTOM_SPELL_CONFIG_FILE}' file if it is OK.)"
    exit 1
  end
end
speller() click to toggle source

create an Aspell speller object @return [Aspell] the speller object

# File lib/tasks/spellcheck_task.rb, line 70
def speller
  return @speller if @speller

  # raspell is an optional dependency, handle the missing case nicely
  begin
    require "raspell"
  rescue LoadError
    warn "ERROR: Ruby gem \"raspell\" is not installed."
    exit 1
  end

  # initialize aspell
  @speller = Aspell.new("en_US")
  @speller.suggestion_mode = Aspell::NORMAL
  # ignore the HTML tags in the text
  @speller.set_option("mode", "html")

  @speller
end
switch_block_tag() click to toggle source
# File lib/tasks/spellcheck_task.rb, line 177
def switch_block_tag
  @inside_block = !@inside_block
end