Created
May 1, 2010 15:19
-
-
Save tune/386410 to your computer and use it in GitHub Desktop.
Add/Remove BOM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ruby | |
require "optparse" | |
mode = :help | |
opt = OptionParser.new | |
opt.on("-a", "Add BOM"){|v| mode = :add} | |
opt.on("-d", "Delete BOM"){|v| mode = :delete} | |
opt.parse!(ARGV) | |
case mode | |
when :add | |
STDOUT.binmode | |
lines = readlines | |
unless lines[0] =~ /^\M-o\M-;\M-?/ then | |
print "\xEF\xBB\xBF" | |
end | |
print lines | |
when :delete | |
STDOUT.binmode | |
lines = readlines | |
lines[0].sub!(/^\xEF\xBB\xBF/, '') | |
print lines | |
when :help | |
STDERR.puts opt.help | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment