Super Simple Speller v2


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. #!/usr/bin/env ruby -w
  2.  
  3. require 'cgi'
  4. require ENV['TM_SUPPORT_PATH'] + "/lib/exit_codes.rb"
  5. require ENV['TM_SUPPORT_PATH'] + "/lib/escape.rb"
  6.  
  7. COCOA_DIALOG = "#{ENV['TM_SUPPORT_PATH']}/bin/CocoaDialog"
  8. HTML_FILE = "/cygdrive/c/tmp/sss_tmp.html"
  9.  
  10. selection = STDIN.read
  11.  
  12. IO.popen("aspell -a", 'w+') do |aspell|
  13. aspell.puts(selection)
  14. aspell.close_write
  15. to_skip = []
  16. aspell.each do |check|
  17. if check =~ /^\&/
  18. data, raw_suggestions = check.split(":")
  19. signal, original, count, offset = data.split
  20. next if to_skip.include?(original)
  21. suggestions = ""
  22. raw_suggestions.split(', ').each { |s| suggestions += "<a href=\'cocoadialog://ReplaceAll #{CGI.escape(s.chomp)}\'>#{CGI.escapeHTML(s.chomp)}</a> "}
  23. html_dialog = <<EOF
  24. <div style='font-family: Verdana, Lucida, Arial, Helvetica, sans-serif; font-size: 11px;'>
  25. <p>There are #{count} suggestions for \'#{original}\' (click on one to replace):</p>
  26. <p style='border: medium double #4D819D; padding: .3cm; background-color: #D2D2D2; word-spacing: 5px;'>#{suggestions}</p>
  27. <ul>
  28. <li><a style='text-decoration: none' href=\"cocoadialog://Skip\"><strong>Skip</strong> #{original} this time</a></li>
  29. <li><a style='text-decoration: none' href=\"cocoadialog://SkipAll\"><strong>Skip all</strong> occurrences of #{original} in this document</a></li>
  30. <li><a style='text-decoration: none' href=\"cocoadialog://Add\"><strong>Add</strong> #{original} to your personal dictionary</a></li>
  31. <li><a style='text-decoration: none' href=\"cocoadialog://End\"><strong>End</strong> spell checking, committing any changes you've made</a></li>
  32. <li><a style='text-decoration: none' href=\"cocoadialog://Cancel\"><strong>Cancel</strong>, discarding any changes you've made</a></li>
  33. </ul></p>
  34. </div>
  35. EOF
  36. open(HTML_FILE, 'w') { |f| f << html_dialog}
  37. choice=%x("#{COCOA_DIALOG}" html --title "Misspelled Word" --html-from-file #{HTML_FILE} --width 500 --height 300)
  38. button, replacement = choice.split(" ")
  39. case button
  40. when "Cancel"
  41. TextMate.exit_show_tool_tip("Spell-check cancelled!")
  42. when "End"
  43. break
  44. when "SkipAll"
  45. to_skip.push(original)
  46. when "ReplaceAll"
  47. replacement.chomp!
  48. selection.gsub!(/#{original}/, replacement) # Replace done globally so all instances of original replaced
  49. when "Replace"
  50. selection.gsub!(/#{original}/, replacement)
  51. # Todo: Need to use the offest here to replace the original using the offset variable,
  52. # which is a 0-offset index of the character position where the misspelled word
  53. # starts.
  54. when "Add"
  55. `echo -e "*#{original}\n#" | aspell -a`
  56. `"#{COCOA_DIALOG}" ok-msgbox \
  57. --text "Added '#{original}' to your personal dictionary." \
  58. --no-cancel --timeout 2`
  59. to_skip.push(original)
  60. end
  61. end
  62. end
  63. end
  64. print selection

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.