diff options
author | murphy <murphy@rubychan.de> | 2011-05-21 03:03:31 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2011-05-21 03:03:31 +0000 |
commit | 5efcf487985e7348ed3d39f33b7185028a6e3716 (patch) | |
tree | ddaa71a4884a5349d985526f88f11a7514c7b6d7 /lib/coderay/helpers | |
parent | c5a193f08ddef1ba1cb5e1f9b0789b2f587022c4 (diff) | |
download | coderay-5efcf487985e7348ed3d39f33b7185028a6e3716.tar.gz |
various fixes and cleanups in CodeRay's helpers
Diffstat (limited to 'lib/coderay/helpers')
-rw-r--r-- | lib/coderay/helpers/file_type.rb | 86 | ||||
-rw-r--r-- | lib/coderay/helpers/plugin.rb | 26 | ||||
-rw-r--r-- | lib/coderay/helpers/word_list.rb | 191 |
3 files changed, 118 insertions, 185 deletions
diff --git a/lib/coderay/helpers/file_type.rb b/lib/coderay/helpers/file_type.rb index 2cbaa6d..61f0049 100644 --- a/lib/coderay/helpers/file_type.rb +++ b/lib/coderay/helpers/file_type.rb @@ -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 @@ module CodeRay 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 diff --git a/lib/coderay/helpers/plugin.rb b/lib/coderay/helpers/plugin.rb index f4f6739..e2b6616 100644 --- a/lib/coderay/helpers/plugin.rb +++ b/lib/coderay/helpers/plugin.rb @@ -120,14 +120,12 @@ module CodeRay 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 @@ module CodeRay 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,7 +230,8 @@ module CodeRay # 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 @@ -247,9 +239,9 @@ module CodeRay # 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 diff --git a/lib/coderay/helpers/word_list.rb b/lib/coderay/helpers/word_list.rb index 3af52cf..7f8eba6 100644 --- a/lib/coderay/helpers/word_list.rb +++ b/lib/coderay/helpers/word_list.rb @@ -1,138 +1,77 @@ module CodeRay - -# = WordList -# -# <b>A Hash subclass designed for mapping word lists to token types.</b> -# -# Copyright (c) 2006 by murphy (Kornelius Kalnbach) <murphy rubychan de> -# -# License:: LGPL / ask the author -# Version:: 1.1 (2006-Oct-19) -# -# A WordList is a Hash with some additional features. -# It is intended to be used for keyword recognition. -# -# WordList is highly optimized to be used in Scanners, -# typically to decide whether a given ident is a special token. -# -# For case insensitive words use CaseIgnoringWordList. -# -# Example: -# -# # define word arrays -# RESERVED_WORDS = %w[ -# asm break case continue default do else -# ... -# ] -# -# PREDEFINED_TYPES = %w[ -# int long short char void -# ... -# ] -# -# PREDEFINED_CONSTANTS = %w[ -# EOF NULL ... -# ] -# -# # make a WordList -# IDENT_KIND = WordList.new(:ident). -# add(RESERVED_WORDS, :reserved). -# add(PREDEFINED_TYPES, :predefined_type). -# add(PREDEFINED_CONSTANTS, :predefined_constant) -# -# ... -# -# def scan_tokens tokens, options -# ... -# -# elsif scan(/[A-Za-z_][A-Za-z_0-9]*/) -# # use it -# kind = IDENT_KIND[match] -# ... -class WordList < Hash - - # Creates a new WordList with +default+ as default value. - # - # You can activate +caching+ to store the results for every [] request. + + # = WordList # - # With caching, methods like +include?+ or +delete+ may no longer behave - # as you expect. Therefore, it is recommended to use the [] method only. - def initialize default = false, caching = false, &block - if block - raise ArgumentError, 'Can\'t combine block with caching.' if caching - super(&block) - else - if caching - super() do |h, k| - h[k] = h.fetch k, default - end - else - super default - end - end - end - - # Add words to the list and associate them with +kind+. + # <b>A Hash subclass designed for mapping word lists to token types.</b> # - # Returns +self+, so you can concat add calls. - def add words, kind = true - words.each do |word| - self[word] = kind + # Copyright (c) 2006-2011 by murphy (Kornelius Kalnbach) <murphy rubychan de> + # + # License:: LGPL / ask the author + # Version:: 2.0 (2011-05-08) + # + # A WordList is a Hash with some additional features. + # It is intended to be used for keyword recognition. + # + # WordList is optimized to be used in Scanners, + # typically to decide whether a given ident is a special token. + # + # For case insensitive words use CaseIgnoringWordList. + # + # Example: + # + # # define word arrays + # RESERVED_WORDS = %w[ + # asm break case continue default do else + # ] + # + # PREDEFINED_TYPES = %w[ + # int long short char void + # ] + # + # # make a WordList + # IDENT_KIND = WordList.new(:ident). + # add(RESERVED_WORDS, :reserved). + # add(PREDEFINED_TYPES, :predefined_type) + # + # ... + # + # def scan_tokens tokens, options + # ... + # + # elsif scan(/[A-Za-z_][A-Za-z_0-9]*/) + # # use it + # kind = IDENT_KIND[match] + # ... + class WordList < Hash + + # Create a new WordList with +default+ as default value. + def initialize default = false + super default end - self - end - -end - - -# A CaseIgnoringWordList is like a WordList, only that -# keys are compared case-insensitively. -# -# Ignoring the text case is realized by sending the +downcase+ message to -# all keys. -# -# Caching usually makes a CaseIgnoringWordList faster, but it has to be -# activated explicitely. -class CaseIgnoringWordList < WordList - - # Creates a new case-insensitive WordList with +default+ as default value. - # - # You can activate caching to store the results for every [] request. - # This speeds up subsequent lookups for the same word, but also - # uses memory. - def initialize default = false, caching = false - if caching - super(default, false) do |h, k| - h[k] = h.fetch k.downcase, default - end - else - super(default, false) - extend Uncached + + # Add words to the list and associate them with +value+. + # + # Returns +self+, so you can concat add calls. + def add words, value = true + words.each { |word| self[word] = value } + self end + end - module Uncached # :nodoc: + + # A CaseIgnoringWordList is like a WordList, only that + # keys are compared case-insensitively (normalizing keys using +downcase+). + class CaseIgnoringWordList < WordList + def [] key - super(key.downcase) + super key.downcase end - end - - # Add +words+ to the list and associate them with +kind+. - def add words, kind = true - words.each do |word| - self[word.downcase] = kind + + def []= key, value + super key.downcase, value end - self + end - -end - + end - -__END__ -# check memory consumption -END { - ObjectSpace.each_object(CodeRay::CaseIgnoringWordList) do |wl| - p wl.inject(0) { |memo, key, value| memo + key.size + 24 } - end -}
\ No newline at end of file |