diff options
-rwxr-xr-x | bin/coderay | 78 | ||||
-rw-r--r-- | etc/CodeRay.tmproj | 112 | ||||
-rw-r--r-- | lib/coderay/encoder.rb | 11 | ||||
-rw-r--r-- | lib/coderay/encoders/_map.rb | 3 | ||||
-rw-r--r-- | lib/coderay/encoders/page.rb | 12 | ||||
-rw-r--r-- | lib/coderay/for_redcloth.rb | 2 | ||||
-rw-r--r-- | lib/coderay/helpers/plugin.rb | 40 | ||||
-rw-r--r-- | lib/coderay/scanner.rb | 6 | ||||
-rw-r--r-- | test/executable/suite.rb | 8 |
9 files changed, 110 insertions, 162 deletions
diff --git a/bin/coderay b/bin/coderay index 70baa99..2a4ba3b 100755 --- a/bin/coderay +++ b/bin/coderay @@ -1,10 +1,10 @@ #!/usr/bin/env ruby require 'coderay' -$options, $args = ARGV.partition { |arg| arg[/^-[hv]$|--\w+/] } -subcommand = $args.detect { |arg| arg[/^\w/] } +$options, args = ARGV.partition { |arg| arg[/^-[hv]$|--\w+/] } +subcommand = args.detect { |arg| arg[/^\w/] } subcommand = nil if subcommand && File.exist?(subcommand) -$args.delete subcommand +args.delete subcommand def option? *options !($options & options).empty? @@ -22,24 +22,46 @@ end def help puts <<-HELP -Usage: - coderay [-<language>] [input] [-<format>] [output] +This is CodeRay #{CodeRay::VERSION}, a syntax highlighting tool for selected languages. -Examples: - 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 +usage: + coderay [-language] [input] [-format] [output] + +defaults: + language detect from input file name or shebang; fall back to plain text + input STDIN + format detect from output file name or use terminal; fall back to HTML page + output STDOUT + +common: + coderay file.rb # highlight file to terminal + coderay file.rb > file.html # highlight file to HTML page + coderay file.rb -div > file.html # highlight file to HTML snippet + +configure output: + coderay file.py output.json # output tokens as JSON + coderay file.py -loc # count lines of code in Python file + +configure input: + coderay -python file # specify the input language + coderay -ruby # take input from STDIN + +more: + coderay stylesheet [style] # print CSS stylesheet HELP end def commands puts <<-COMMANDS - General: - help print some help - version print CodeRay version - commands print this list + general: + highlight code highlighting (default command) stylesheet print the CSS stylesheet with the given name + + about: + list [of] list all available plugins (or just the scanners|encoders) + commands print this list + help show some help + version print CodeRay version COMMANDS end @@ -57,13 +79,15 @@ when 'highlight', nil version help else - signature = $args.map { |arg| arg[/^-/] ? '-' : 'f' }.join - names = $args.map { |arg| arg.sub(/^-/, '') } + 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 /^f-f?$/ + input_file, output_filetype, output_file, = *names when /^-ff?$/ input_filetype, input_file, output_file, = *names when /^-f-f?$/ @@ -100,8 +124,28 @@ when 'highlight', nil puts output end end +when 'list' + arg = args.first && args.first.downcase + if [nil, 's', 'sc', 'scanner', 'scanners'].include? arg + puts 'input languages (Scanners):' + scanners = CodeRay::Scanners.all_plugins.map do |plugin| + aliases = (plugin.aliases - [nil]).map { |key| "-#{key}" }.sort_by { |key| key.size } + " #{plugin.lang}: #{plugin.title}#{" (.#{plugin.file_extension}; #{aliases.join(', ')})" unless aliases.empty?}" + end + puts scanners.sort + puts + end + + if [nil, 'e', 'en', 'enc', 'encoder', 'encoders'].include? arg + puts 'output formats (Encoders):' + encoders = CodeRay::Encoders.all_plugins.map do |plugin| + aliases = (plugin.aliases - [nil]).map { |key| "-#{key}" }.sort_by { |key| key.size } + " #{plugin.plugin_id}: #{plugin.title}#{" (.#{plugin.file_extension}; #{aliases.join(', ')})" unless aliases.empty?}" + end + puts encoders.sort + end when 'stylesheet' - puts CodeRay::Encoders[:html]::CSS.new($args.first).stylesheet + puts CodeRay::Encoders[:html]::CSS.new(args.first).stylesheet when 'commands' commands when 'help' diff --git a/etc/CodeRay.tmproj b/etc/CodeRay.tmproj index b53014e..8c3f1a5 100644 --- a/etc/CodeRay.tmproj +++ b/etc/CodeRay.tmproj @@ -2,8 +2,6 @@ <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <dict> - <key>currentDocument</key> - <string>../lib/coderay/tokens.rb</string> <key>documents</key> <array> <dict> @@ -180,117 +178,7 @@ <key>firstVisibleLine</key> <integer>3</integer> </dict> - <key>../lib/coderay/encoders/html.rb</key> - <dict> - <key>caret</key> - <dict> - <key>column</key> - <integer>22</integer> - <key>line</key> - <integer>99</integer> - </dict> - <key>firstVisibleColumn</key> - <integer>0</integer> - <key>firstVisibleLine</key> - <integer>78</integer> - </dict> - <key>../lib/coderay/scanner.rb</key> - <dict> - <key>caret</key> - <dict> - <key>column</key> - <integer>31</integer> - <key>line</key> - <integer>204</integer> - </dict> - <key>columnSelection</key> - <false/> - <key>firstVisibleColumn</key> - <integer>0</integer> - <key>firstVisibleLine</key> - <integer>177</integer> - <key>selectFrom</key> - <dict> - <key>column</key> - <integer>18</integer> - <key>line</key> - <integer>204</integer> - </dict> - <key>selectTo</key> - <dict> - <key>column</key> - <integer>34</integer> - <key>line</key> - <integer>204</integer> - </dict> - </dict> - <key>../lib/coderay/scanners/diff.rb</key> - <dict> - <key>caret</key> - <dict> - <key>column</key> - <integer>52</integer> - <key>line</key> - <integer>110</integer> - </dict> - <key>columnSelection</key> - <false/> - <key>firstVisibleColumn</key> - <integer>0</integer> - <key>firstVisibleLine</key> - <integer>86</integer> - <key>selectFrom</key> - <dict> - <key>column</key> - <integer>48</integer> - <key>line</key> - <integer>110</integer> - </dict> - <key>selectTo</key> - <dict> - <key>column</key> - <integer>52</integer> - <key>line</key> - <integer>110</integer> - </dict> - </dict> - <key>../lib/coderay/tokens.rb</key> - <dict> - <key>caret</key> - <dict> - <key>column</key> - <integer>36</integer> - <key>line</key> - <integer>244</integer> - </dict> - <key>firstVisibleColumn</key> - <integer>0</integer> - <key>firstVisibleLine</key> - <integer>206</integer> - </dict> - <key>../test/unit/tokens.rb</key> - <dict> - <key>caret</key> - <dict> - <key>column</key> - <integer>26</integer> - <key>line</key> - <integer>72</integer> - </dict> - <key>firstVisibleColumn</key> - <integer>0</integer> - <key>firstVisibleLine</key> - <integer>63</integer> - </dict> </dict> - <key>openDocuments</key> - <array> - <string>../lib/coderay/encoders/html.rb</string> - <string>../lib/coderay/scanners/diff.rb</string> - <string>../lib/coderay/scanner.rb</string> - <string>../lib/coderay/tokens.rb</string> - <string>../test/unit/tokens.rb</string> - </array> <key>showFileHierarchyDrawer</key> <true/> <key>windowFrame</key> diff --git a/lib/coderay/encoder.rb b/lib/coderay/encoder.rb index e495ca0..85a2456 100644 --- a/lib/coderay/encoder.rb +++ b/lib/coderay/encoder.rb @@ -34,12 +34,17 @@ module CodeRay # downcase class name instead. def const_missing sym if sym == :FILE_EXTENSION - plugin_id.to_s + (@plugin_id || name[/\w+$/].downcase).to_s else super end end + # The default file extension for output file of this encoder class. + def file_extension + self::FILE_EXTENSION + end + end # Subclasses are to store their default options in this constant. @@ -85,9 +90,9 @@ module CodeRay # more clear to you. alias highlight encode - # Return the default file extension for outputs of this encoder. + # The default file extension for this encoder. def file_extension - self.class::FILE_EXTENSION + self.class.file_extension end def << token diff --git a/lib/coderay/encoders/_map.rb b/lib/coderay/encoders/_map.rb index b4b766a..c9a20cc 100644 --- a/lib/coderay/encoders/_map.rb +++ b/lib/coderay/encoders/_map.rb @@ -8,8 +8,7 @@ module Encoders :plain => :text, :plaintext => :text, :remove_comments => :comment_filter, - :stats => :statistic, - :tex => :latex + :stats => :statistic # No default because Tokens#nonsense would not raise NoMethodError. diff --git a/lib/coderay/encoders/page.rb b/lib/coderay/encoders/page.rb index 9f3538e..8db45cd 100644 --- a/lib/coderay/encoders/page.rb +++ b/lib/coderay/encoders/page.rb @@ -1,6 +1,6 @@ module CodeRay module Encoders - + load :html # Wraps the output into a HTML page, using CSS classes and @@ -8,17 +8,17 @@ module Encoders # # See Encoders::HTML for available options. class Page < HTML - + FILE_EXTENSION = 'html' - + register_for :page - + DEFAULT_OPTIONS = HTML::DEFAULT_OPTIONS.merge \ :css => :class, :wrap => :page, :line_numbers => :table - + end - + end end diff --git a/lib/coderay/for_redcloth.rb b/lib/coderay/for_redcloth.rb index 1946f8e..f9df32b 100644 --- a/lib/coderay/for_redcloth.rb +++ b/lib/coderay/for_redcloth.rb @@ -45,7 +45,7 @@ module CodeRay if !opts[:lang] && RedCloth::VERSION.to_s >= '4.2.0' # simulating pre-4.2 behavior if opts[:text].sub!(/\A\[(\w+)\]/, '') - if CodeRay::Scanners[$1].plugin_id == :text + if CodeRay::Scanners[$1].lang == :text opts[:text] = $& + opts[:text] else opts[:lang] = $1 diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb index e2b6616..b0dda0c 100644 --- a/lib/coderay/helpers/plugin.rb +++ b/lib/coderay/helpers/plugin.rb @@ -152,11 +152,25 @@ module CodeRay plugin_hash.values.grep(Class) end + # Loads the map file (see map). + # + # This is done automatically when plugin_path is called. + def load_plugin_map + mapfile = path_to '_map' + @plugin_map_loaded = true + if File.exist? mapfile + require mapfile + true + else + false + end + end + protected # Return a plugin hash that automatically loads plugins. def make_plugin_hash - map_loaded = false + @plugin_map_loaded ||= false Hash.new do |h, plugin_id| id = validate_id(plugin_id) path = path_to id @@ -164,15 +178,14 @@ module CodeRay raise LoadError, "#{path} not found" unless File.exist? path require path rescue LoadError => boom - if map_loaded + if @plugin_map_loaded if h.has_key?(nil) # default plugin h[nil] else raise PluginNotFound, 'Could not load plugin %p: %s' % [id, boom] end else - load_map - map_loaded = true + load_plugin_map h[plugin_id] end else @@ -186,14 +199,6 @@ module CodeRay end end - # Loads the map file (see map). - # - # This is done automatically when plugin_path is called. - def load_map - mapfile = path_to '_map' - require mapfile if File.exist? mapfile - end - # Returns the expected path to the plugin file for the given id. def path_to plugin_id File.join plugin_path, "#{plugin_id}.rb" @@ -230,6 +235,8 @@ module CodeRay # See CodeRay::PluginHost for examples. module Plugin + attr_reader :plugin_id + # Register this class for the given +id+. # # Example: @@ -262,9 +269,12 @@ module CodeRay self::PLUGIN_HOST end - # Returns the plugin id used by the engine. - def plugin_id - @plugin_id ||= name[/\w+$/].downcase + def aliases + plugin_host.load_plugin_map + plugin_host.plugin_hash.inject [] do |aliases, (key, value)| + aliases << key if plugin_host[key] == self + aliases + end end end diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb index ec2b3aa..0e0723c 100644 --- a/lib/coderay/scanner.rb +++ b/lib/coderay/scanner.rb @@ -81,7 +81,7 @@ module CodeRay end # The typical filename suffix for this scanner's language. - def file_extension extension = plugin_id + def file_extension extension = lang @file_extension ||= extension.to_s end @@ -91,7 +91,9 @@ module CodeRay end # The lang of this Scanner class, which is equal to its Plugin ID. - alias lang plugin_id + def lang + @plugin_id + end protected diff --git a/test/executable/suite.rb b/test/executable/suite.rb index fd40909..ef2ebb3 100644 --- a/test/executable/suite.rb +++ b/test/executable/suite.rb @@ -40,7 +40,7 @@ class TestCodeRayExecutable < Test::Unit::TestCase end should 'print version and help' do assert_match(/CodeRay #{CodeRay::VERSION}/, coderay('')) - assert_match(/Usage:/, coderay('')) + assert_match(/usage:/, coderay('')) end end @@ -55,13 +55,13 @@ class TestCodeRayExecutable < Test::Unit::TestCase context 'help' do should 'be printed with -h' do - assert_match(/^Usage:/, coderay('-h')) + assert_match(/^usage:/, coderay('-h')) end should 'be printed with --help' do - assert_match(/^Usage:/, coderay('--help')) + assert_match(/^usage:/, coderay('--help')) end should 'be printed with subcommand help' do - assert_match(/^Usage:/, coderay('help')) + assert_match(/^usage:/, coderay('help')) end end |