From: Patrick Hurley Date: 2005-03-15T15:28:55+09:00 Subject: [SOLUTION] Quoted Printable (#23) I am a ruby newbie, so be kind. I wrote the code myself, but blatantly stole Dave Burt's test cases - thank you. I also found one test case that breaks my code (and Dave's) that I am not sure what the correct answer is, but I know mine is wrong: Consider: "=== \n" which will cause a new space to be found at the end of a string - is it the case that all space at the end of the line is encoded (increasing size rather needlessly), but simplifying this case? Either way, I am too tired and have other important stuff to do so I will let it go. Please feel free to let me know where I did not do things the "Ruby way" as I am primarily a C++ and Perl guy, but very interested in getting better at Ruby. Thanks pth # # == Synopsis # # Ruby Quiz #23 # # The quoted printable encoding is used in primarily in email, thought it has # recently seen some use in XML areas as well. The encoding is simple to # translate to and from. # # This week's quiz is to build a filter that handles quoted printable # translation. # # Your script should be a standard Unix filter, reading from files listed on # the command-line or STDIN and writing to STDOUT. In normal operation, the # script should encode all text read in the quoted printable format. However, # your script should also support a -d command-line option and when present, # text should be decoded from quoted printable instead. Finally, your script # should understand a -x command-line option and when given, it should encode # <, > and & for use with XML. # # == Usage # # ruby quiz23.rb [-d | --decode ] [ -x | --xml ] # # == Author # Patrick Hurley, Cornell-Mayo Assoc # # == Copyright # Copytright (c) 2005 Cornell-Mayo Assoc # Licensed under the same terms as Ruby. # require 'optparse' require 'rdoc/usage' module QuotedPrintable MAX_LINE_PRINTABLE_ENCODE_LENGTH = 76 def from_qp result = self.gsub(/=\r\n/, "") result.gsub!(/\r\n/m, $/) result.gsub!(/=([\dA-F]{2})/) { $1.hex.chr } result end def to_qp(handle_xml = false) char_mask = if (handle_xml) /[^!-%,-;=?-~\s]/ else /[^!-<>-~\s]/ end # encode the non-space characters result = self.gsub(char_mask) { |ch| "=%02X" % ch[0] } # encode the last space character at end of line result.gsub!(/(\s)(?=#{$/})/o) { |ch| "=%02X" % ch[0] } lines = result.scan(/(?:(?:[^\n]{74}(?==[\dA-F]{2}))|(?:[^\n]{0,76}(?=\n))|(?:[^\n]{1,75}(?!\n{2})))(?:#{$/}*)/); lines.join("=\n").gsub(/#{$/}/m, "\r\n") end def QuotedPrintable.encode STDOUT.binmode while (line = gets) do print line.to_qp end end def QuotedPrintable.decode STDIN.binmode while (line = gets) do # I am a ruby newbie, and I could # not get gets to get the \r\n pairs # no matter how I set $/ - any pointers? line = line.chomp + "\r\n" print line.from_qp end end end class String include QuotedPrintable end if __FILE__ == $0 opts = OptionParser.new opts.on("-h", "--help") { RDoc::usage; } opts.on("-d", "--decode") { $decode = true } opts.on("-x", "--xml") { $handle_xml = true } opts.parse!(ARGV) rescue RDoc::usage('usage') if ($decode) QuotedPrintable.decode() else QuotedPrintable.encode() end end