diff options
Diffstat (limited to 'lib/coderay')
-rw-r--r-- | lib/coderay/encoders/_map.rb | 4 | ||||
-rw-r--r-- | lib/coderay/encoders/statistic.rb | 2 | ||||
-rw-r--r-- | lib/coderay/encoders/text.rb | 18 | ||||
-rw-r--r-- | lib/coderay/for_redcloth.rb | 2 | ||||
-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 | ||||
-rw-r--r-- | lib/coderay/scanners/_map.rb | 5 | ||||
-rw-r--r-- | lib/coderay/scanners/plaintext.rb | 26 | ||||
-rw-r--r-- | lib/coderay/scanners/text.rb | 26 |
10 files changed, 161 insertions, 225 deletions
diff --git a/lib/coderay/encoders/_map.rb b/lib/coderay/encoders/_map.rb index 7c6712f..b4b766a 100644 --- a/lib/coderay/encoders/_map.rb +++ b/lib/coderay/encoders/_map.rb @@ -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 diff --git a/lib/coderay/encoders/statistic.rb b/lib/coderay/encoders/statistic.rb index 4a6a845..e52f28f 100644 --- a/lib/coderay/encoders/statistic.rb +++ b/lib/coderay/encoders/statistic.rb @@ -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: diff --git a/lib/coderay/encoders/text.rb b/lib/coderay/encoders/text.rb index 54a00dc..84e2215 100644 --- a/lib/coderay/encoders/text.rb +++ b/lib/coderay/encoders/text.rb @@ -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 diff --git a/lib/coderay/for_redcloth.rb b/lib/coderay/for_redcloth.rb index 2af9082..1946f8e 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 == :plaintext + if CodeRay::Scanners[$1].plugin_id == :text opts[:text] = $& + opts[:text] else opts[:lang] = $1 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 diff --git a/lib/coderay/scanners/_map.rb b/lib/coderay/scanners/_map.rb index 517e388..d1b1a29 100644 --- a/lib/coderay/scanners/_map.rb +++ b/lib/coderay/scanners/_map.rb @@ -12,11 +12,12 @@ module Scanners :nitro => :nitro_xhtml, :pascal => :delphi, :patch => :diff, - :plain => :plaintext, + :plain => :text, + :plaintext => :text, :xhtml => :html, :yml => :yaml - default :plain + default :text end end diff --git a/lib/coderay/scanners/plaintext.rb b/lib/coderay/scanners/plaintext.rb deleted file mode 100644 index b8ea5f5..0000000 --- a/lib/coderay/scanners/plaintext.rb +++ /dev/null @@ -1,26 +0,0 @@ -module CodeRay -module Scanners - - # Scanner for plain text. - # - # Yields just one token of the kind :plain. - # - # Alias: +plain+ - class Plaintext < Scanner - - register_for :plaintext, :plain - title 'Plain text' - - KINDS_NOT_LOC = [:plain] # :nodoc: - - protected - - def scan_tokens encoder, options - encoder.text_token string, :plain - encoder - end - - end - -end -end diff --git a/lib/coderay/scanners/text.rb b/lib/coderay/scanners/text.rb new file mode 100644 index 0000000..bde9029 --- /dev/null +++ b/lib/coderay/scanners/text.rb @@ -0,0 +1,26 @@ +module CodeRay + module Scanners + + # Scanner for plain text. + # + # Yields just one token of the kind :plain. + # + # Alias: +plaintext+, +plain+ + class Text < Scanner + + register_for :text + title 'Plain text' + + KINDS_NOT_LOC = [:plain] # :nodoc: + + protected + + def scan_tokens encoder, options + encoder.text_token string, :plain + encoder + end + + end + + end +end |