My Blog

Fixing Middleman-spellcheck


Sometimes when you make a change to the software, it is interesting to predict how long will such change take. Fixing Middleman-spellcheck was initially only about letting myself to select words which I would consider correct and do it from within the front-matter of each Middleman’s article files. It ended up taking more than I anticipated, and below is short description on what went wrong.

Middleman plugin infrastructure lets one to run a filter on several stages of the build process, and Middleman-spellcheck runs at the end, once all files are converted from .md to .html files.

Unfortunately the moment I run Middleman with Middleman-spellcheck enabled I got many, many valid words recognized as misspelled words.

Problem appears to be reported as issue #7.

I think no easy model exists for fixing the source code of Gems installed in a system-wide location.

I ended up cloning the repository of the project:

$ cd /w/repos
$ git clone

and basically doing:

$ sudo bash
# cd /Library/Ruby/Gems/2.0.0/gems
# mv middleman-spellcheck-0.7.5 middleman-spellcheck-0.7.5.old
# ln -s /w/repos/middleman-spellcheck middleman-spellcheck-0.7.5

since I don’t know any better way to point Middleman to use a different Gem. Middleman-spell is structured more or less like that:

[wkoszek-macbook:/w/repos/middleman-spellcheck] wk% tree . ├── Gemfile ├── LICENSE.txt ├── ├── Rakefile … ├── lib │   ├── middleman-spellcheck │   │   ├── extension.rb │   │   ├── spellchecker.rb │   │   └── version.rb │   ├── middleman-spellcheck.rb │   └── middleman_extension.rb ├── middleman-spellcheck.gemspec └── spec └── lib └── middleman-spellcheck └── spellcheck_spec.rb

22 directories, 31 files

extension.rb is the Middleman-specific code, while spellcheck.rb is where interaction with Aspell is present.

Here’s our wolf:

  def self.query(text, lang='en')
    result = `echo "#{text}" | #{@@aspell_path} -a -l #{lang}`
    raise 'Aspell command not found' unless result
    new_result = result.split("\n")
    new_result[1..-1] || []

My first guess is was that #{text} has some quotes ", but it wasn’t it. The way I try to attack the problem is always with the simplified use case. The pattern is: find for which input the program is failing and try to reproduce such failure. In my case I have 145 .md files, but managed to find one which is fairly short.

Here are the errors:

spellcheck  Running spell checker for /blog/2012/12/15/book-the-22-laws-of-marketing/
    misspell  The word 'nd' is misspelled
    misspell  The word 'entirely' is misspelled
    misspell  The word 'want' is misspelled
    misspell  The word 'think' is misspelled
    misspell  The word 'to' is misspelled
    misspell  The word 'numbers' is misspelled
    misspell  The word 'has' is misspelled

While nd is probably correctly classified as a problem, “want” and “think” are not. It was time to get more visibility:

diff --git a/lib/middleman-spellcheck/spellchecker.rb b/lib/middleman-spellcheck/spellchecker.rb
index 05a48d1..b82c33a 100644