summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2011-06-25 18:23:07 +0000
committermurphy <murphy@rubychan.de>2011-06-25 18:23:07 +0000
commit498df2b7654c210c8f47e2757efc33cd94689b57 (patch)
treebda6e10d3eadbf22f1a1a1a2d4e8d0cf7844a3b0 /lib
parentd6fe4e777a4f543c8828dbf77e955ab38e6c2803 (diff)
downloadcoderay-498df2b7654c210c8f47e2757efc33cd94689b57.tar.gz
coderay list subcommand and cleanups/fixes in Plugin helper (issue #45)
Diffstat (limited to 'lib')
-rw-r--r--lib/coderay/encoder.rb11
-rw-r--r--lib/coderay/encoders/_map.rb3
-rw-r--r--lib/coderay/encoders/page.rb12
-rw-r--r--lib/coderay/for_redcloth.rb2
-rw-r--r--lib/coderay/helpers/plugin.rb40
-rw-r--r--lib/coderay/scanner.rb6
6 files changed, 45 insertions, 29 deletions
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