Class: Spellchecker

Inherits:
Object
  • Object
show all
Defined in:
lib/middleman-spellcheck/spellchecker.rb

Constant Summary collapse

@@aspell_path =
"aspell"
@@aspell_cmdargs =
""
@@debug_enabled =
0

Class Method Summary collapse

Class Method Details

.aspell_pathObject



10
11
12
# File 'lib/middleman-spellcheck/spellchecker.rb', line 10

def self.aspell_path
  @@aspell_path
end

.aspell_path=(path) ⇒ Object



6
7
8
# File 'lib/middleman-spellcheck/spellchecker.rb', line 6

def self.aspell_path=(path)
  @@aspell_path = path
end

.check(text, lang) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/middleman-spellcheck/spellchecker.rb', line 73

def self.check(text, lang)
  # do ’ -> ' for aspell. Otherwise 's' is passed as a word to aspell.
  text.gsub! '', '\''
  sdbg "self.check got raw text:\n#{text}\n"

  words = text.split(/[^A-Za-z']+/).select { |s|
     s != "" and s != "'s" and s != "'"
  }
  sdbg "self.check word array:\n#{words}\n"

  results = query(words, lang).map do |query_result|
    correct?(query_result)
  end

  words.zip(results).map {|word, correctness| { word: word, correct: correctness } }
end

.cmdargsObject



18
19
20
# File 'lib/middleman-spellcheck/spellchecker.rb', line 18

def self.cmdargs
  @@aspell_cmdargs
end

.cmdargs=(args) ⇒ Object



14
15
16
# File 'lib/middleman-spellcheck/spellchecker.rb', line 14

def self.cmdargs=(args)
  @@aspell_cmdargs = args
end

.correct?(result_string) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
# File 'lib/middleman-spellcheck/spellchecker.rb', line 69

def self.correct?(result_string)
  result_string == "*"
end

.debug_enabledObject



26
27
28
# File 'lib/middleman-spellcheck/spellchecker.rb', line 26

def self.debug_enabled
  @@debug_enabled
end

.debug_enabled=(args) ⇒ Object



22
23
24
# File 'lib/middleman-spellcheck/spellchecker.rb', line 22

def self.debug_enabled=(args)
  @@debug_enabled = args
end

.query(words, lang) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/middleman-spellcheck/spellchecker.rb', line 37

def self.query(words, lang)
  args = "-a -l #{lang}"
  if @@aspell_cmdargs != ""
    args = @@aspell_cmdargs
  end
  cmd = %Q<#{@@aspell_path} #{args}>
  result = []

  sdbg "Starting aspell"
  IO.popen(cmd, "a+", :err=>[:child, :out]) { |f|
    val = f.gets.strip()	# skip the Aspell's intro
    sdbg "Expected Aspell intro, got #{val}"
    words.each do |word|
      sdbg "-> Writing word '#{word}'"
      f.write(word + "\n")
      f.flush

      # should be * or &
      word_check_res = f.gets.strip()
      sdbg "<- got result '#{word_check_res}'"

      # skip the empty line
      val = f.gets()
	sdbg "Expected empty line, got '#{val}'"

      result << word_check_res
    end
  }
  raise 'Aspell command not found' unless result
  result || []
end

.sdbg(*args) ⇒ Object



30
31
32
33
34
35
# File 'lib/middleman-spellcheck/spellchecker.rb', line 30

def self.sdbg(*args)
  if @@debug_enabled <= 0
    return
  end
  print "# DBG ", *args, "\n"
end