From 75bc5455af8c3c3381066aac3d5fff42264cac6f Mon Sep 17 00:00:00 2001 From: Kornelius Kalnbach Date: Fri, 19 Aug 2011 03:09:35 +0200 Subject: Major rewrite of encoders to support IO output; fixed some minor scanner bugs; cleanups; dropped NitroXHTML scanner; improved tests --- bin/coderay | 40 +++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) (limited to 'bin') diff --git a/bin/coderay b/bin/coderay index fa87fa9..acb05ed 100755 --- a/bin/coderay +++ b/bin/coderay @@ -105,23 +105,38 @@ when 'highlight', nil if output_file output_filetype ||= CodeRay::FileType[output_file] else - output_filetype ||= tty? ? :term : :page + output_filetype ||= :term end + output_filetype = :page if output_filetype.to_s == 'html' + if input_file input = File.read input_file else input = $stdin.read end - output = CodeRay.scan(input, input_filetype).encode(output_filetype) - - if output_file - File.open output_file, 'w' do |file| - file.puts output + begin + file = + if output_file + File.open output_file, 'w' + else + $stdout.sync = true + $stdout + end + CodeRay.encode(input, input_filetype, output_filetype, :out => file) + file.puts + rescue CodeRay::PluginHost::PluginNotFound => boom + if boom.message[/CodeRay::(\w+)s could not load plugin :?(.*?): /] + puts "I don't know the #$1 \"#$2\"." + else + puts boom.message end - else - puts output + # puts "I don't know this plugin: #{boom.message[/Could not load plugin (.*?): /, 1]}." + rescue CodeRay::Scanners::Scanner::ScanError # FIXME: rescue Errno::EPIPE + # ignore + ensure + file.close end end when 'list' @@ -151,6 +166,13 @@ when 'commands' when 'help' help else - puts "Unknown command: #{subcommand}" + $stdout = $stderr help + puts + if subcommand[/\A\w+\z/] + puts "Unknown command: #{subcommand}" + else + puts "File not found: #{subcommand}" + end + exit 1 end -- cgit v1.2.1