Permalink
Browse files

various fixes and cleanups in CodeRay's helpers

  • Loading branch information...
1 parent c5a193f commit 5efcf487985e7348ed3d39f33b7185028a6e3716 @korny korny committed May 21, 2011
View
@@ -168,7 +168,7 @@ def scan code, lang, options = {}, &block
#
# If +lang+ is :auto or omitted, the CodeRay::FileType module is used to
# determine it. If it cannot find out what type it is, it uses
- # CodeRay::Scanners::Plaintext.
+ # CodeRay::Scanners::Text.
#
# Calls CodeRay.scan.
#
@@ -179,7 +179,7 @@ def scan_file filename, lang = :auto, options = {}, &block
file = IO.read filename
if lang == :auto
require 'coderay/helpers/file_type'
- lang = FileType.fetch filename, :plaintext, true
+ lang = FileType.fetch filename, :text, true
end
scan file, lang, options = {}, &block
end
@@ -2,9 +2,11 @@ module CodeRay
module Encoders
map \
- :loc => :lines_of_code,
+ :loc => :lines_of_code,
:term => :terminal,
+ :tty => :terminal,
:plain => :text,
+ :plaintext => :text,
:remove_comments => :comment_filter,
:stats => :statistic,
:tex => :latex
@@ -6,7 +6,7 @@ module Encoders
# Alias: +stats+
class Statistic < Encoder
- register_for :stats, :statistic
+ register_for :statistic
attr_reader :type_stats, :real_token_count # :nodoc:
@@ -4,7 +4,7 @@ module Encoders
# Concats the tokens into a single string, resulting in the original
# code string if no tokens were removed.
#
- # Alias: +plain+
+ # Alias: +plain+, +plaintext+
#
# == Options
#
@@ -13,31 +13,31 @@ module Encoders
#
# Default: empty String
class Text < Encoder
-
+
register_for :text
-
+
FILE_EXTENSION = 'txt'
-
+
DEFAULT_OPTIONS = {
:separator => nil
}
-
+
def text_token text, kind
super
@out << @sep if @sep
end
-
+
protected
def setup options
super
@sep = options[:separator]
end
-
+
def finish options
super.chomp @sep
end
-
+
end
-
+
end
end
@@ -45,7 +45,7 @@ def code(opts) # :nodoc:
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 == :plaintext
+ if CodeRay::Scanners[$1].plugin_id == :text
opts[:text] = $& + opts[:text]
else
opts[:lang] = $1
@@ -7,13 +7,13 @@ module CodeRay
# == Usage
#
# # determine the type of the given
- # lang = FileType[ARGV.first]
+ # lang = FileType[file_name]
#
- # # return :plaintext if the file type is unknown
- # lang = FileType.fetch ARGV.first, :plaintext
+ # # return :text if the file type is unknown
+ # lang = FileType.fetch file_name, :text
#
# # try the shebang line, too
- # lang = FileType.fetch ARGV.first, :plaintext, true
+ # lang = FileType.fetch file_name, :text, true
module FileType
UnknownFileType = Class.new Exception
@@ -77,46 +77,48 @@ def shebang filename
end
TypeFromExt = {
- 'c' => :c,
- 'clj' => :clojure,
- 'css' => :css,
- 'diff' => :diff,
- 'dpr' => :delphi,
- 'gemspec' => :ruby,
- 'groovy' => :groovy,
- 'gvy' => :groovy,
- 'h' => :c,
- 'htm' => :html,
- 'html' => :html,
+ 'c' => :c,
+ 'cfc' => :xml,
+ 'cfm' => :xml,
+ 'clj' => :clojure,
+ 'css' => :css,
+ 'diff' => :diff,
+ 'dpr' => :delphi,
+ 'gemspec' => :ruby,
+ 'groovy' => :groovy,
+ 'gvy' => :groovy,
+ 'h' => :c,
+ 'htm' => :html,
+ 'html' => :html,
'html.erb' => :rhtml,
- 'java' => :java,
- 'js' => :java_script,
- 'json' => :json,
- 'mab' => :ruby,
- 'pas' => :delphi,
- 'patch' => :diff,
- 'php' => :php,
- 'php3' => :php,
- 'php4' => :php,
- 'php5' => :php,
- 'py' => :python,
- 'py3' => :python,
- 'pyw' => :python,
- 'rake' => :ruby,
+ 'java' => :java,
+ 'js' => :java_script,
+ 'json' => :json,
+ 'mab' => :ruby,
+ 'pas' => :delphi,
+ 'patch' => :diff,
+ 'php' => :php,
+ 'php3' => :php,
+ 'php4' => :php,
+ 'php5' => :php,
+ 'py' => :python,
+ 'py3' => :python,
+ 'pyw' => :python,
+ 'rake' => :ruby,
'raydebug' => :raydebug,
- 'rb' => :ruby,
- 'rbw' => :ruby,
- 'rhtml' => :rhtml,
- 'rjs' => :ruby,
- 'rpdf' => :ruby,
- 'rxml' => :ruby,
- 'sch' => :scheme,
- 'sql' => :sql,
- 'ss' => :scheme,
- 'xhtml' => :xhtml,
- 'xml' => :xml,
- 'yaml' => :yaml,
- 'yml' => :yaml,
+ 'rb' => :ruby,
+ 'rbw' => :ruby,
+ 'rhtml' => :rhtml,
+ 'rjs' => :ruby,
+ 'rpdf' => :ruby,
+ 'rxml' => :ruby,
+ 'sch' => :scheme,
+ 'sql' => :sql,
+ 'ss' => :scheme,
+ 'xhtml' => :xhtml,
+ 'xml' => :xml,
+ 'yaml' => :yaml,
+ 'yml' => :yaml,
}
for cpp_alias in %w[cc cpp cp cxx c++ C hh hpp h++ cu]
TypeFromExt[cpp_alias] = :cpp
@@ -120,14 +120,12 @@ def default id = nil
end
end
- # Every plugin must register itself for one or more
- # +ids+ by calling register_for, which calls this method.
+ # Every plugin must register itself for +id+ by calling register_for,
+ # which calls this method.
#
# See Plugin#register_for.
- def register plugin, *ids
- for id in ids
- plugin_hash[validate_id(id)] = plugin
- end
+ def register plugin, id
+ plugin_hash[validate_id(id)] = plugin
end
# A Hash of plugion_id => Plugin pairs.
@@ -154,13 +152,6 @@ def all_plugins
plugin_hash.values.grep(Class)
end
- # Returns an array of all plugin titles.
- #
- # Note: This loads all plugins using load_all.
- def all_titles
- all_plugins.map { |plugin| plugin.title }
- end
-
protected
# Return a plugin hash that automatically loads plugins.
@@ -239,17 +230,18 @@ def validate_id id
# See CodeRay::PluginHost for examples.
module Plugin
- # Register this class for the given langs.
+ # Register this class for the given +id+.
+ #
# Example:
# class MyPlugin < PluginHost::BaseClass
# register_for :my_id
# ...
# end
#
# See PluginHost.register.
- def register_for *ids
- @plugin_id = ids.first
- plugin_host.register self, *ids
+ def register_for id
+ @plugin_id = id
+ plugin_host.register self, id
end
# Returns the title of the plugin, or sets it to the
Oops, something went wrong.

0 comments on commit 5efcf48

Please sign in to comment.