To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / gems / coderay-0.9.7 / bin / coderay @ 442:753f1380d6bc
History | View | Annotate | Download (1.93 KB)
| 1 |
#!/usr/bin/env ruby |
|---|---|
| 2 |
# CodeRay Executable |
| 3 |
# |
| 4 |
# Version: 0.2 |
| 5 |
# Author: murphy |
| 6 |
|
| 7 |
require 'coderay' |
| 8 |
|
| 9 |
if ARGV.empty? |
| 10 |
$stderr.puts <<-USAGE |
| 11 |
CodeRay #{CodeRay::VERSION} (http://coderay.rubychan.de)
|
| 12 |
|
| 13 |
Usage: |
| 14 |
coderay file [-<format>] |
| 15 |
coderay -<lang> [-<format>] [< file] [> output] |
| 16 |
|
| 17 |
Defaults: |
| 18 |
lang: based on file extension |
| 19 |
format: ANSI colorized output for terminal, HTML page for files |
| 20 |
|
| 21 |
Examples: |
| 22 |
coderay foo.rb # colorized output to terminal, based on file extension |
| 23 |
coderay foo.rb -loc # print LOC count, based on file extension and format |
| 24 |
coderay foo.rb > foo.html # HTML page output to file, based on extension |
| 25 |
coderay -ruby < foo.rb # colorized output to terminal, based on lang |
| 26 |
coderay -ruby -loc < foo.rb # print LOC count, based on lang |
| 27 |
coderay -ruby -page foo.rb # HTML page output to terminal, based on lang and format |
| 28 |
coderay -ruby -page foo.rb > foo.html # HTML page output to file, based on lang and format |
| 29 |
USAGE |
| 30 |
end |
| 31 |
|
| 32 |
first, second = ARGV |
| 33 |
|
| 34 |
def read |
| 35 |
file = ARGV.grep(/^(?!-)/).last |
| 36 |
if file |
| 37 |
if File.exist?(file) |
| 38 |
File.read file |
| 39 |
else |
| 40 |
$stderr.puts "No such file: #{file}"
|
| 41 |
end |
| 42 |
else |
| 43 |
$stdin.read |
| 44 |
end |
| 45 |
end |
| 46 |
|
| 47 |
if first |
| 48 |
if first[/-(\w+)/] == first |
| 49 |
lang = $1 |
| 50 |
input = read |
| 51 |
tokens = :scan |
| 52 |
else |
| 53 |
file = first |
| 54 |
unless File.exist? file |
| 55 |
$stderr.puts "No such file: #{file}"
|
| 56 |
exit 2 |
| 57 |
end |
| 58 |
tokens = CodeRay.scan_file file |
| 59 |
end |
| 60 |
else |
| 61 |
$stderr.puts 'No lang/file given.' |
| 62 |
exit 1 |
| 63 |
end |
| 64 |
|
| 65 |
if second |
| 66 |
if second[/-(\w+)/] == second |
| 67 |
format = $1.to_sym |
| 68 |
else |
| 69 |
raise 'invalid format (must be -xxx)' |
| 70 |
end |
| 71 |
else |
| 72 |
if $stdout.tty? |
| 73 |
format = :term |
| 74 |
else |
| 75 |
$stderr.puts 'No format given; setting to default (HTML Page).' |
| 76 |
format = :page |
| 77 |
end |
| 78 |
end |
| 79 |
|
| 80 |
if tokens == :scan |
| 81 |
output = CodeRay::Duo[lang => format].highlight input |
| 82 |
else |
| 83 |
output = tokens.encode format |
| 84 |
end |
| 85 |
out = $stdout |
| 86 |
out.puts output |