#!C:/ruby/bin/ruby # CodeRay Executable # # Version: 0.1 # Author: murphy require 'optparse' def err msg $stderr.puts msg end begin require 'coderay' if ARGV.empty? puts <<-USAGE CodeRay #{CodeRay::Version} (http://rd.cYcnus.de/coderay) Usage: coderay -lang [-format] < file > output coderay file [-format] USAGE end first, second = ARGV if first if first[/-(\w+)/] == first lang = $1.to_sym input = $stdin.read tokens = CodeRay.scan input, lang elsif first == '-' lang = $1.to_sym input = $stdin.read tokens = CodeRay.scan input, lang else file = first tokens = CodeRay.scan_file file output_filename, output_ext = file, /#{Regexp.escape(File.extname(file))}$/ end else raise 'No lang/file given.' end if second if second[/-(\w+)/] == second format = $1.to_sym else raise 'Invalid format (must be -xxx).' end else $stderr.puts 'No format given; setting to default (HTML)' format = :html end output = tokens.encode format out = $stdout if output_filename output_filename.sub output_ext, CodeRay::Encoders[format]::FILE_EXTENSION if File.exist? output_filename err 'File %s already exists.' % output_filename exit else out = File.open output_filename, 'w' end else end out.print output rescue => boom err "Error: #{boom.message}\n" err boom.backtrace err '-' * 50 err ARGV.options exit 1 end