summaryrefslogtreecommitdiff
path: root/bin
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2011-06-17 23:13:24 +0000
committermurphy <murphy@rubychan.de>2011-06-17 23:13:24 +0000
commit90f70ee61e87e137aa192c5db97c382e1ec7d24b (patch)
treea9d5e9c7e0664faaada8d99741679d0bb8f6403c /bin
parent4c8b5dac9d881b916a49e20401659bf107168e0b (diff)
downloadcoderay-90f70ee61e87e137aa192c5db97c382e1ec7d24b.tar.gz
issue #45: new command line interface!
Diffstat (limited to 'bin')
-rwxr-xr-xbin/coderay167
-rwxr-xr-xbin/coderay_stylesheet4
2 files changed, 90 insertions, 81 deletions
diff --git a/bin/coderay b/bin/coderay
index 690d8fc..70baa99 100755
--- a/bin/coderay
+++ b/bin/coderay
@@ -1,99 +1,112 @@
#!/usr/bin/env ruby
-# CodeRay Executable
-#
-# Version: 0.2
-# Author: murphy
+require 'coderay'
-def err msg
- $stderr.puts msg
-end
+$options, $args = ARGV.partition { |arg| arg[/^-[hv]$|--\w+/] }
+subcommand = $args.detect { |arg| arg[/^\w/] }
+subcommand = nil if subcommand && File.exist?(subcommand)
+$args.delete subcommand
-def read
- if file = ARGV[2]
- File.read file
- else
- $stdin.read
- end
+def option? *options
+ !($options & options).empty?
end
-begin
- require 'coderay'
+def tty?
+ $stdout.tty? || option?('--tty')
+end
- if ARGV.empty?
- puts <<-USAGE
-CodeRay #{CodeRay::VERSION} (http://coderay.rubychan.de)
+def version
+ puts <<-USAGE
+CodeRay #{CodeRay::VERSION}
+ USAGE
+end
+def help
+ puts <<-HELP
Usage:
- coderay -<lang> [-<format>] < file > output
- coderay file [-<format>]
+ coderay [-<language>] [input] [-<format>] [output]
Examples:
- coderay -ruby -statistic < foo.rb
- coderay -ruby < foo.rb # colorized output to terminal
- coderay -ruby -page foo.rb # HTML page output to terminal
- coderay -ruby -page foo.rb > foo.html # HTML page output to file
- coderay codegen.c # generates codegen.c.html
- USAGE
- end
+ coderay -ruby file -loc # count lines of code in Ruby file
+ coderay -ruby < foo.rb # colorized output to terminal
+ coderay -ruby foo.rb -page foo.html # HTML page output to file
+ coderay stylesheet # CSS stylesheet
+ HELP
+end
+
+def commands
+ puts <<-COMMANDS
+ General:
+ help print some help
+ version print CodeRay version
+ commands print this list
+ stylesheet print the CSS stylesheet with the given name
+ COMMANDS
+end
- first, second = ARGV
+if option? '-v', '--version'
+ version
+end
+
+if option? '-h', '--help'
+ help
+end
- if first
- if first[/-(\w+)/] == first
- lang = $1
- input = read
- tokens = :scan
- elsif first == '-'
- lang = $1
- input = read
- tokens = :scan
+case subcommand
+when 'highlight', nil
+ if ARGV.empty?
+ version
+ help
+ else
+ signature = $args.map { |arg| arg[/^-/] ? '-' : 'f' }.join
+ names = $args.map { |arg| arg.sub(/^-/, '') }
+ case signature
+ when /^$/
+ exit
+ when /^ff?$/
+ input_file, output_file, = *names
+ when /^-ff?$/
+ input_filetype, input_file, output_file, = *names
+ when /^-f-f?$/
+ input_filetype, input_file, output_filetype, output_file, = *names
+ when /^--?f?$/
+ input_filetype, output_filetype, output_file, = *names
else
- file = first
- tokens = CodeRay.scan_file file
- output_filename, output_ext = file, /#{Regexp.escape(File.extname(file))}$/
+ raise signature
end
- else
- puts 'No lang/file given.'
- exit 1
- end
-
- if second
- if second[/-(\w+)/] == second
- format = $1
+
+ if input_file
+ input_filetype ||= CodeRay::FileType.fetch input_file, :text, true
+ end
+
+ if output_file
+ output_filetype ||= CodeRay::FileType[output_file]
else
- raise 'invalid format (must be -xxx)'
+ output_filetype ||= tty? ? :term : :page
end
- else
- if $stdout.tty?
- format = :term
+
+ if input_file
+ input = File.read input_file
else
- $stderr.puts 'No format given; setting to default (HTML Page).'
- format = :page
+ input = $stdin.read
end
- end
-
- if tokens == :scan
- output = CodeRay.encoder(format).encode(input, lang)
- else
- output = tokens.encode format
- end
- out = $stdout
- if output_filename
- output_filename += '.' + CodeRay::Encoders[format]::FILE_EXTENSION.to_s
- if File.exist? output_filename
- err 'File %s already exists.' % output_filename
- exit
+
+ output = CodeRay.scan(input, input_filetype).encode(output_filetype)
+
+ if output_file
+ File.open output_file, 'w' do |file|
+ file.puts output
+ end
else
- out = File.open output_filename, 'w'
- puts "Writing to #{output_filename}..."
+ puts output
end
end
- out.puts output
-
-rescue => boom
- err "Error: #{boom.message}\n"
- err boom.backtrace
- err '-' * 50
- err ARGV
- exit 1
+when 'stylesheet'
+ puts CodeRay::Encoders[:html]::CSS.new($args.first).stylesheet
+when 'commands'
+ commands
+when 'help'
+ help
+else
+ puts "Unknown command: #{subcommand}"
+ help
end
diff --git a/bin/coderay_stylesheet b/bin/coderay_stylesheet
deleted file mode 100755
index 0e19395..0000000
--- a/bin/coderay_stylesheet
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env ruby
-require 'coderay'
-
-puts CodeRay::Encoders[:html]::CSS.new.stylesheet