1#!/usr/bin/env ruby
2
3require 'optparse'
4require 'pathname'
5require 'webrick/htmlutils'
6
7$LOAD_PATH << Pathname.new(__FILE__).dirname.realpath.to_s
8
9require 'PrettyPatch'
10
11BACKTRACE_SEPARATOR = "\n\tfrom "
12
13options = { :html_exceptions => false }
14OptionParser.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 }
20end.parse!
21
22patch_data = nil
23if ARGV.length == 0 || ARGV[0] == '-' then
24    patch_data = $stdin.read
25else
26    File.open(ARGV[0]) { |file| patch_data = file.read }
27end
28
29begin
30    puts PrettyPatch.prettify(patch_data)
31rescue => 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"
37end
38