1 #!/usr/bin/env ruby 2 3 require 'optparse' 4 require 'pathname' 5 require 'webrick/htmlutils' 6 7 $LOAD_PATH << Pathname.new(__FILE__).dirname.realpath.to_s 8 9 require 'PrettyPatch' 10 11 BACKTRACE_SEPARATOR = "\n\tfrom " 12 13 options = { :html_exceptions => false } 14 OptionParser.new do |opts| 15 opts.banner = "Usage: #{File.basename($0)} [options] [patch-file]" 16 17 opts.separator "" 18 19 opts.on("--html-exceptions", "Print exceptions to stdout as HTML") { |h| options[:html_exceptions] = h } 20 end.parse! 21 22 patch_data = nil 23 if ARGV.length == 0 || ARGV[0] == '-' then 24 patch_data = $stdin.read 25 else 26 File.open(ARGV[0]) { |file| patch_data = file.read } 27 end 28 29 begin 30 puts PrettyPatch.prettify(patch_data) 31 rescue => exception 32 raise unless options[:html_exceptions] 33 34 backtrace = exception.backtrace 35 backtrace[0] += ": " + exception + " (" + exception.class.to_s + ")" 36 print "<pre>\n", WEBrick::HTMLUtils::escape(backtrace.join(BACKTRACE_SEPARATOR)), "\n</pre>\n" 37 end 38