af83

Spellchecking for HTML

After HTML Truncator, I want to present you my new Ruby gem: HTML Spellchecker.

When you edit a text in a textarea, there's a good chance that your browser spell-checks it and underlines the misspelled words. But you can't do that for other HTML elements. For example, it can be useful to have spellcheck while previewing contents.

You can try to play with the contenteditable and spellcheck attributes of HTML5, but it will only works in some recent browser and it will be fragile as it will involve a lot of javascript. The other way is server-side spellchecking.

It's not really hard but you have to be careful: it's very easy to mess the HTML by marking the href attribute of a link as misspelled. It's probably also a good idea to not try to spellcheck code tags.

For my own usage, I've created HTML Spellchecker, which does spellchecking in Ruby and takes care of the HTML stuff. It uses Nokogiri to parse the HTML and for the textual contents, it checks each word with hunspell-ffi, a Ruby interface for hunspell (the Open-Source spell-checker library used by LibreOffice and Firefox).

It's really simple to use it:

require "html_spellchecker"
HTML_Spellchecker.english.spellcheck("<p>This is xzqwy.</p>")
# => "<p>This is <span class="misspelled">xzqwy</span>.</p>"

And I hope it can be useful for you too!

blog comments powered by Disqus