Sanitize HTML With Sanitize
From the archive — written 17 years ago. About old posts.
Handy gem:
[sourcecode language='ruby']
!/usr/bin/env ruby
require "rubygems"
require "sanitize"
markup = STDIN.read
Sanitize.clean!(markup, Sanitize::Config::BASIC)
puts markup
[/sourcecode]
Sanitize is built on Hpricot and provides quick-n-easy HTML cleanup. I plonked the above into a TextMate command and it automatically makes Word HTML 90 percent more tolerable by discarding style and class attributes and leaving just the most basic markup.
You can also configure it to leave some tags alone, permit some tags to have certain attributes but not others, etc. There are also ‘Restricted’ and ‘Relaxed’ levels of sanitization.
Insert some gushing about Ruby making my life better here. I don’t care if its syntax is too forgiving, loose or whatever: When I can eliminate a source of irritation in six lines of code, I’ll capitalize every vowel if required.
#html #ruby