summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2016-02-13 17:14:48 +0100
committerKornelius Kalnbach <murphy@rubychan.de>2016-02-13 17:14:48 +0100
commit5cc8c52f7ca0dc31eb85d065579d66dd0f0cd540 (patch)
tree47682e4141e1c52d888281cb3dd9a6a57f6e0513
parent0ad3ddc26da51b3bdd0454859f894761a0155c1b (diff)
parent12a467ca21faad604655d5f2e7b87b42d7a30b4a (diff)
downloadcoderay-5cc8c52f7ca0dc31eb85d065579d66dd0f0cd540.tar.gz
Merge pull request #190 from rubychan/autoload
Cleanup Folder structure
-rw-r--r--lib/coderay.rb18
-rw-r--r--lib/coderay/encoders.rb18
-rw-r--r--lib/coderay/encoders/encoder.rb (renamed from lib/coderay/encoder.rb)11
-rw-r--r--lib/coderay/helpers/plugin.rb219
-rw-r--r--lib/coderay/helpers/plugin_host.rb221
-rw-r--r--lib/coderay/scanners.rb27
-rw-r--r--lib/coderay/scanners/java.rb2
-rw-r--r--lib/coderay/scanners/scanner.rb (renamed from lib/coderay/scanner.rb)18
-rw-r--r--lib/coderay/styles.rb15
-rw-r--r--lib/coderay/styles/style.rb (renamed from lib/coderay/style.rb)7
10 files changed, 292 insertions, 264 deletions
diff --git a/lib/coderay.rb b/lib/coderay.rb
index f759ed6..c3de20b 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -134,7 +134,7 @@ module CodeRay
File.join CODERAY_PATH, *path
end
- require 'coderay/version'
+ autoload :VERSION, 'coderay/version'
# helpers
autoload :FileType, coderay_path('helpers', 'file_type')
@@ -145,13 +145,13 @@ module CodeRay
autoload :TokenKinds, coderay_path('token_kinds')
# Plugin system
- autoload :PluginHost, coderay_path('helpers', 'plugin')
+ autoload :PluginHost, coderay_path('helpers', 'plugin_host')
autoload :Plugin, coderay_path('helpers', 'plugin')
# Plugins
- autoload :Scanners, coderay_path('scanner')
- autoload :Encoders, coderay_path('encoder')
- autoload :Styles, coderay_path('style')
+ autoload :Scanners, coderay_path('scanners')
+ autoload :Encoders, coderay_path('encoders')
+ autoload :Styles, coderay_path('styles')
# convenience access and reusable Encoder/Scanner pair
autoload :Duo, coderay_path('duo')
@@ -166,7 +166,7 @@ module CodeRay
#
# See also demo/demo_simple.
def scan code, lang, options = {}, &block
- TokensProxy.new code, lang, options, block
+ CodeRay::TokensProxy.new code, lang, options, block
end
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
@@ -181,7 +181,7 @@ module CodeRay
# require 'coderay'
# page = CodeRay.scan_file('some_c_code.c').html
def scan_file filename, lang = :auto, options = {}, &block
- lang = FileType.fetch filename, :text, true if lang == :auto
+ lang = CodeRay::FileType.fetch filename, :text, true if lang == :auto
code = File.read filename
scan code, lang, options, &block
end
@@ -258,7 +258,7 @@ module CodeRay
# ]
# #-> 2 out of 4 tokens have the kind :integer.
def encoder format, options = {}
- Encoders[format].new options
+ CodeRay::Encoders[format].new options
end
# Finds the Scanner class for +lang+ and creates an instance, passing
@@ -266,7 +266,7 @@ module CodeRay
#
# See Scanner.new.
def scanner lang, options = {}, &block
- Scanners[lang].new '', options, &block
+ CodeRay::Scanners[lang].new '', options, &block
end
# Extract the options for the scanner from the +options+ hash.
diff --git a/lib/coderay/encoders.rb b/lib/coderay/encoders.rb
new file mode 100644
index 0000000..6599186
--- /dev/null
+++ b/lib/coderay/encoders.rb
@@ -0,0 +1,18 @@
+module CodeRay
+
+ # This module holds the Encoder class and its subclasses.
+ # For example, the HTML encoder is named CodeRay::Encoders::HTML
+ # can be found in coderay/encoders/html.
+ #
+ # Encoders also provides methods and constants for the register
+ # mechanism and the [] method that returns the Encoder class
+ # belonging to the given format.
+ module Encoders
+
+ extend PluginHost
+ plugin_path File.dirname(__FILE__), 'encoders'
+
+ autoload :Encoder, CodeRay.coderay_path('encoders', 'encoder')
+
+ end
+end
diff --git a/lib/coderay/encoder.rb b/lib/coderay/encoders/encoder.rb
index d2d6c7e..fa5695d 100644
--- a/lib/coderay/encoder.rb
+++ b/lib/coderay/encoders/encoder.rb
@@ -1,17 +1,6 @@
module CodeRay
-
- # This module holds the Encoder class and its subclasses.
- # For example, the HTML encoder is named CodeRay::Encoders::HTML
- # can be found in coderay/encoders/html.
- #
- # Encoders also provides methods and constants for the register
- # mechanism and the [] method that returns the Encoder class
- # belonging to the given format.
module Encoders
- extend PluginHost
- plugin_path File.dirname(__FILE__), 'encoders'
-
# = Encoder
#
# The Encoder base class. Together with Scanner and
diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb
index 9a724ff..4567943 100644
--- a/lib/coderay/helpers/plugin.rb
+++ b/lib/coderay/helpers/plugin.rb
@@ -1,224 +1,5 @@
module CodeRay
- # = PluginHost
- #
- # A simple subclass/subfolder plugin system.
- #
- # Example:
- # class Generators
- # extend PluginHost
- # plugin_path 'app/generators'
- # end
- #
- # class Generator
- # extend Plugin
- # PLUGIN_HOST = Generators
- # end
- #
- # class FancyGenerator < Generator
- # register_for :fancy
- # end
- #
- # Generators[:fancy] #-> FancyGenerator
- # # or
- # CodeRay.require_plugin 'Generators/fancy'
- # # or
- # Generators::Fancy
- module PluginHost
-
- # Raised if Encoders::[] fails because:
- # * a file could not be found
- # * the requested Plugin is not registered
- PluginNotFound = Class.new LoadError
- HostNotFound = Class.new LoadError
-
- PLUGIN_HOSTS = []
- PLUGIN_HOSTS_BY_ID = {} # dummy hash
-
- # Loads all plugins using list and load.
- def load_all
- for plugin in list
- load plugin
- end
- end
-
- # Returns the Plugin for +id+.
- #
- # Example:
- # yaml_plugin = MyPluginHost[:yaml]
- def [] id, *args, &blk
- plugin = validate_id(id)
- begin
- plugin = plugin_hash.[](plugin, *args, &blk)
- end while plugin.is_a? String
- plugin
- end
-
- alias load []
-
- # Tries to +load+ the missing plugin by translating +const+ to the
- # underscore form (eg. LinesOfCode becomes lines_of_code).
- def const_missing const
- id = const.to_s.
- gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
- gsub(/([a-z\d])([A-Z])/,'\1_\2').
- downcase
- load id
- end
-
- class << self
-
- # Adds the module/class to the PLUGIN_HOSTS list.
- def extended mod
- PLUGIN_HOSTS << mod
- end
-
- end
-
- # The path where the plugins can be found.
- def plugin_path *args
- unless args.empty?
- @plugin_path = File.expand_path File.join(*args)
- end
- @plugin_path ||= ''
- end
-
- # Map a plugin_id to another.
- #
- # Usage: Put this in a file plugin_path/_map.rb.
- #
- # class MyColorHost < PluginHost
- # map :navy => :dark_blue,
- # :maroon => :brown,
- # :luna => :moon
- # end
- def map hash
- for from, to in hash
- from = validate_id from
- to = validate_id to
- plugin_hash[from] = to unless plugin_hash.has_key? from
- end
- end
-
- # Define the default plugin to use when no plugin is found
- # for a given id, or return the default plugin.
- #
- # See also map.
- #
- # class MyColorHost < PluginHost
- # map :navy => :dark_blue
- # default :gray
- # end
- #
- # MyColorHost.default # loads and returns the Gray plugin
- def default id = nil
- if id
- id = validate_id id
- raise "The default plugin can't be named \"default\"." if id == :default
- plugin_hash[:default] = id
- else
- load :default
- end
- end
-
- # Every plugin must register itself for +id+ by calling register_for,
- # which calls this method.
- #
- # See Plugin#register_for.
- def register plugin, id
- plugin_hash[validate_id(id)] = plugin
- end
-
- # A Hash of plugion_id => Plugin pairs.
- def plugin_hash
- @plugin_hash ||= (@plugin_hash = make_plugin_hash).tap { load_plugin_map }
- end
-
- # Returns an array of all .rb files in the plugin path.
- #
- # The extension .rb is not included.
- def list
- Dir[path_to('*')].select do |file|
- File.basename(file)[/^(?!_)\w+\.rb$/]
- end.map do |file|
- File.basename(file, '.rb').to_sym
- end
- end
-
- # Returns an array of all Plugins.
- #
- # Note: This loads all plugins using load_all.
- def all_plugins
- load_all
- 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'
- if File.exist? mapfile
- require mapfile
- true
- else
- false
- end
- end
-
- protected
-
- # Return a plugin hash that automatically loads plugins.
- def make_plugin_hash
- Hash.new do |h, plugin_id|
- id = validate_id(plugin_id)
- path = path_to id
- begin
- require path
- rescue LoadError => boom
- if h.has_key?(:default)
- h[:default]
- else
- raise PluginNotFound, '%p could not load plugin %p: %s' % [self, id, boom]
- end
- else
- # Plugin should have registered by now
- if h.has_key? id
- h[id]
- else
- raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
- end
- end
- end
- 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"
- end
-
- # Converts +id+ to a valid plugin ID String, or returns +nil+.
- #
- # Raises +ArgumentError+ for all other objects, or if the
- # given String includes non-alphanumeric characters (\W).
- def validate_id id
- case id
- when Symbol
- id.to_s
- when String
- if id[/\w+/] == id
- id.downcase
- else
- raise ArgumentError, "Invalid id given: #{id}"
- end
- else
- raise ArgumentError, "Symbol or String expected, but #{id.class} given."
- end
- end
-
- end
-
-
# = Plugin
#
# Plugins have to include this module.
diff --git a/lib/coderay/helpers/plugin_host.rb b/lib/coderay/helpers/plugin_host.rb
new file mode 100644
index 0000000..e9bc17c
--- /dev/null
+++ b/lib/coderay/helpers/plugin_host.rb
@@ -0,0 +1,221 @@
+module CodeRay
+
+ # = PluginHost
+ #
+ # A simple subclass/subfolder plugin system.
+ #
+ # Example:
+ # class Generators
+ # extend PluginHost
+ # plugin_path 'app/generators'
+ # end
+ #
+ # class Generator
+ # extend Plugin
+ # PLUGIN_HOST = Generators
+ # end
+ #
+ # class FancyGenerator < Generator
+ # register_for :fancy
+ # end
+ #
+ # Generators[:fancy] #-> FancyGenerator
+ # # or
+ # CodeRay.require_plugin 'Generators/fancy'
+ # # or
+ # Generators::Fancy
+ module PluginHost
+
+ # Raised if Encoders::[] fails because:
+ # * a file could not be found
+ # * the requested Plugin is not registered
+ PluginNotFound = Class.new LoadError
+ HostNotFound = Class.new LoadError
+
+ PLUGIN_HOSTS = []
+ PLUGIN_HOSTS_BY_ID = {} # dummy hash
+
+ # Loads all plugins using list and load.
+ def load_all
+ for plugin in list
+ load plugin
+ end
+ end
+
+ # Returns the Plugin for +id+.
+ #
+ # Example:
+ # yaml_plugin = MyPluginHost[:yaml]
+ def [] id, *args, &blk
+ plugin = validate_id(id)
+ begin
+ plugin = plugin_hash.[](plugin, *args, &blk)
+ end while plugin.is_a? String
+ plugin
+ end
+
+ alias load []
+
+ # Tries to +load+ the missing plugin by translating +const+ to the
+ # underscore form (eg. LinesOfCode becomes lines_of_code).
+ def const_missing const
+ id = const.to_s.
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
+ downcase
+ load id
+ end
+
+ class << self
+
+ # Adds the module/class to the PLUGIN_HOSTS list.
+ def extended mod
+ PLUGIN_HOSTS << mod
+ end
+
+ end
+
+ # The path where the plugins can be found.
+ def plugin_path *args
+ unless args.empty?
+ @plugin_path = File.expand_path File.join(*args)
+ end
+ @plugin_path ||= ''
+ end
+
+ # Map a plugin_id to another.
+ #
+ # Usage: Put this in a file plugin_path/_map.rb.
+ #
+ # class MyColorHost < PluginHost
+ # map :navy => :dark_blue,
+ # :maroon => :brown,
+ # :luna => :moon
+ # end
+ def map hash
+ for from, to in hash
+ from = validate_id from
+ to = validate_id to
+ plugin_hash[from] = to unless plugin_hash.has_key? from
+ end
+ end
+
+ # Define the default plugin to use when no plugin is found
+ # for a given id, or return the default plugin.
+ #
+ # See also map.
+ #
+ # class MyColorHost < PluginHost
+ # map :navy => :dark_blue
+ # default :gray
+ # end
+ #
+ # MyColorHost.default # loads and returns the Gray plugin
+ def default id = nil
+ if id
+ id = validate_id id
+ raise "The default plugin can't be named \"default\"." if id == :default
+ plugin_hash[:default] = id
+ else
+ load :default
+ end
+ end
+
+ # Every plugin must register itself for +id+ by calling register_for,
+ # which calls this method.
+ #
+ # See Plugin#register_for.
+ def register plugin, id
+ plugin_hash[validate_id(id)] = plugin
+ end
+
+ # A Hash of plugion_id => Plugin pairs.
+ def plugin_hash
+ @plugin_hash ||= (@plugin_hash = make_plugin_hash).tap { load_plugin_map }
+ end
+
+ # Returns an array of all .rb files in the plugin path.
+ #
+ # The extension .rb is not included.
+ def list
+ Dir[path_to('*')].select do |file|
+ File.basename(file)[/^(?!_)\w+\.rb$/]
+ end.map do |file|
+ File.basename(file, '.rb').to_sym
+ end
+ end
+
+ # Returns an array of all Plugins.
+ #
+ # Note: This loads all plugins using load_all.
+ def all_plugins
+ load_all
+ 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'
+ if File.exist? mapfile
+ require mapfile
+ true
+ else
+ false
+ end
+ end
+
+ protected
+
+ # Return a plugin hash that automatically loads plugins.
+ def make_plugin_hash
+ Hash.new do |h, plugin_id|
+ id = validate_id(plugin_id)
+ path = path_to id
+ begin
+ require path
+ rescue LoadError => boom
+ if h.has_key?(:default)
+ h[:default]
+ else
+ raise PluginNotFound, '%p could not load plugin %p: %s' % [self, id, boom]
+ end
+ else
+ # Plugin should have registered by now
+ if h.has_key? id
+ h[id]
+ else
+ raise PluginNotFound, "No #{self.name} plugin for #{id.inspect} found in #{path}."
+ end
+ end
+ end
+ 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"
+ end
+
+ # Converts +id+ to a valid plugin ID String, or returns +nil+.
+ #
+ # Raises +ArgumentError+ for all other objects, or if the
+ # given String includes non-alphanumeric characters (\W).
+ def validate_id id
+ case id
+ when Symbol
+ id.to_s
+ when String
+ if id[/\w+/] == id
+ id.downcase
+ else
+ raise ArgumentError, "Invalid id given: #{id}"
+ end
+ else
+ raise ArgumentError, "Symbol or String expected, but #{id.class} given."
+ end
+ end
+
+ end
+
+end
diff --git a/lib/coderay/scanners.rb b/lib/coderay/scanners.rb
new file mode 100644
index 0000000..3c7e594
--- /dev/null
+++ b/lib/coderay/scanners.rb
@@ -0,0 +1,27 @@
+require 'strscan'
+
+module CodeRay
+
+ autoload :WordList, coderay_path('helpers', 'word_list')
+
+ # = Scanners
+ #
+ # This module holds the Scanner class and its subclasses.
+ # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
+ # can be found in coderay/scanners/ruby.
+ #
+ # Scanner also provides methods and constants for the register
+ # mechanism and the [] method that returns the Scanner class
+ # belonging to the given lang.
+ #
+ # See PluginHost.
+ module Scanners
+
+ extend PluginHost
+ plugin_path File.dirname(__FILE__), 'scanners'
+
+ autoload :Scanner, CodeRay.coderay_path('scanners', 'scanner')
+
+ end
+
+end
diff --git a/lib/coderay/scanners/java.rb b/lib/coderay/scanners/java.rb
index b282864..962154e 100644
--- a/lib/coderay/scanners/java.rb
+++ b/lib/coderay/scanners/java.rb
@@ -36,7 +36,7 @@ module Scanners
add(BuiltinTypes::List, :predefined_type).
add(BuiltinTypes::List.select { |builtin| builtin[/(Error|Exception)$/] }, :exception).
add(DIRECTIVES, :directive) # :nodoc:
-
+
ESCAPE = / [bfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x # :nodoc:
UNICODE_ESCAPE = / u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x # :nodoc:
STRING_CONTENT_PATTERN = {
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanners/scanner.rb
index b3f7e17..efa710d 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanners/scanner.rb
@@ -1,25 +1,7 @@
# encoding: utf-8
-require 'strscan'
module CodeRay
-
- autoload :WordList, coderay_path('helpers', 'word_list')
-
- # = Scanners
- #
- # This module holds the Scanner class and its subclasses.
- # For example, the Ruby scanner is named CodeRay::Scanners::Ruby
- # can be found in coderay/scanners/ruby.
- #
- # Scanner also provides methods and constants for the register
- # mechanism and the [] method that returns the Scanner class
- # belonging to the given lang.
- #
- # See PluginHost.
module Scanners
- extend PluginHost
- plugin_path File.dirname(__FILE__), 'scanners'
-
# = Scanner
#
diff --git a/lib/coderay/styles.rb b/lib/coderay/styles.rb
new file mode 100644
index 0000000..d8fa8aa
--- /dev/null
+++ b/lib/coderay/styles.rb
@@ -0,0 +1,15 @@
+module CodeRay
+
+ # This module holds the Style class and its subclasses.
+ #
+ # See Plugin.
+ module Styles
+
+ extend PluginHost
+ plugin_path File.dirname(__FILE__), 'styles'
+
+ autoload :Style, CodeRay.coderay_path('styles', 'style')
+
+ end
+
+end
diff --git a/lib/coderay/style.rb b/lib/coderay/styles/style.rb
index df4704f..a335386 100644
--- a/lib/coderay/style.rb
+++ b/lib/coderay/styles/style.rb
@@ -1,11 +1,6 @@
module CodeRay
-
- # This module holds the Style class and its subclasses.
- #
- # See Plugin.
+
module Styles
- extend PluginHost
- plugin_path File.dirname(__FILE__), 'styles'
# Base class for styles.
#