summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2010-06-29 07:11:21 +0000
committermurphy <murphy@rubychan.de>2010-06-29 07:11:21 +0000
commita1330de9438827abcdc331c85dbd7e358b0e118a (patch)
tree7f3fa69a87b529f815525e0a24e85504907d5753 /lib
parent13a98cec5c7f19e8e7231899645b8f51464becb4 (diff)
downloadcoderay-a1330de9438827abcdc331c85dbd7e358b0e118a.tar.gz
Use autoload instead of require (speeds up CodeRay startup) for CodeRay, Scanner, Tokens, and Encoders::HTML.
Diffstat (limited to 'lib')
-rw-r--r--lib/coderay.rb28
-rw-r--r--lib/coderay/encoders/html.rb14
-rw-r--r--lib/coderay/scanner.rb8
-rw-r--r--lib/coderay/tokens.rb62
4 files changed, 37 insertions, 75 deletions
diff --git a/lib/coderay.rb b/lib/coderay.rb
index ad0bb5e..ecd0572 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -129,12 +129,20 @@ module CodeRay
# Revision: Subversion Revision number (generated on rake gem:make)
VERSION = '1.0.0'
- require 'coderay/tokens'
- require 'coderay/token_kinds'
- require 'coderay/scanner'
- require 'coderay/encoder'
- require 'coderay/duo'
- require 'coderay/style'
+ # Tokens
+ autoload :Tokens, 'coderay/tokens'
+
+ # Plugin system
+ autoload :PluginHost, 'coderay/helpers/plugin'
+ autoload :Plugin, 'coderay/helpers/plugin'
+
+ # Plugins
+ autoload :Scanners, 'coderay/scanner'
+ autoload :Encoders, 'coderay/encoder'
+ autoload :Styles, 'coderay/style'
+
+ # Convenience access / reusable Encoder/Scanner pair
+ autoload :Duo, 'coderay/duo'
class << self
@@ -266,11 +274,3 @@ module CodeRay
end
end
-
-# Run a test script.
-if $0 == __FILE__
- $stderr.print 'Press key to print demo.'; gets
- # Just use this file as an example of Ruby code.
- code = File.read(__FILE__)[/module CodeRay.*/m]
- print CodeRay.scan(code, :ruby).html
-end
diff --git a/lib/coderay/encoders/html.rb b/lib/coderay/encoders/html.rb
index bd48a72..02b3550 100644
--- a/lib/coderay/encoders/html.rb
+++ b/lib/coderay/encoders/html.rb
@@ -110,8 +110,10 @@ module Encoders
:hint => false,
}
-
- helper :output, :numbering, :css
+
+ # TODO: Make Plugin use autoload, too.
+ helper :output, :css
+ autoload :Numbering, 'coderay/encoders/html/numbering'
attr_reader :css
@@ -144,9 +146,9 @@ module Encoders
end
end
- TRANSPARENT_TOKEN_KINDS = [
+ TRANSPARENT_TOKEN_KINDS = Set[
:delimiter, :modifier, :content, :escape, :inline_delimiter,
- ].to_set
+ ]
# Generate a hint about the given +kinds+ in a +hint+ style.
#
@@ -226,7 +228,9 @@ module Encoders
@out.extend Output
@out.css = @css
- @out.number! options[:line_numbers], options
+ if options[:line_numbers]
+ Numbering.number! @out, options[:line_numbers], options
+ end
@out.wrap! options[:wrap]
@out.apply_title! options[:title]
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index cc1f665..beff642 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -1,7 +1,9 @@
module CodeRay
- require 'coderay/helpers/plugin'
-
+ autoload :WordList, 'coderay/helpers/word_list'
+ # FIXME: Rename CaseIgnoringWordList to WordList::CaseIgnoring.
+ autoload :CaseIgnoringWordList, 'coderay/helpers/word_list'
+
# = Scanners
#
# This module holds the Scanner class and its subclasses.
@@ -50,8 +52,6 @@ module CodeRay
# Raised if a Scanner fails while scanning
ScanError = Class.new(Exception)
- require 'coderay/helpers/word_list'
-
# The default options for all scanner classes.
#
# Define @default_options for subclasses.
diff --git a/lib/coderay/tokens.rb b/lib/coderay/tokens.rb
index bf3ff59..93d2dbd 100644
--- a/lib/coderay/tokens.rb
+++ b/lib/coderay/tokens.rb
@@ -1,5 +1,8 @@
module CodeRay
+ # GZip library for writing and reading token dumps.
+ autoload :GZip, 'coderay/helpers/gzip'
+
# = Tokens TODO: Rewrite!
#
# The Tokens class represents a list of tokens returnd from
@@ -49,6 +52,8 @@ module CodeRay
# to load them from a file, and still use any Encoder that CodeRay provides.
class Tokens < Array
+ autoload :AbbreviationForKind, 'coderay/token_kinds'
+
# The Scanner instance that created the tokens.
attr_accessor :scanner
@@ -73,8 +78,8 @@ module CodeRay
# Turn into a string using Encoders::Text.
#
# +options+ are passed to the encoder if given.
- def to_s options = {}
- encode :text, options
+ def to_s
+ encode Encoders::Encoder.new
end
# Redirects unknown methods to encoder calls.
@@ -251,7 +256,7 @@ module CodeRay
parts << Tokens.new while parts.size < sizes.size
parts
end
-
+
# Dumps the object into a String that can be saved
# in files or databases.
#
@@ -268,9 +273,8 @@ module CodeRay
#
# See GZip module.
def dump gzip_level = 7
- require 'coderay/helpers/gzip_simple'
dump = Marshal.dump self
- dump = dump.gzip gzip_level
+ dump = GZip.gzip dump, gzip_level
dump.extend Undumping
end
@@ -296,8 +300,7 @@ module CodeRay
# The result is commonly a Tokens object, but
# this is not guaranteed.
def Tokens.load dump
- require 'coderay/helpers/gzip_simple'
- dump = dump.gunzip
+ dump = GZip.gunzip dump
@dump = Marshal.load dump
end
@@ -311,48 +314,3 @@ module CodeRay
end
end
-
-if $0 == __FILE__
- $VERBOSE = true
- $: << File.join(File.dirname(__FILE__), '..')
- eval DATA.read, nil, $0, __LINE__ + 4
-end
-
-__END__
-require 'test/unit'
-
-class TokensTest < Test::Unit::TestCase
-
- def test_creation
- assert CodeRay::Tokens < Array
- tokens = nil
- assert_nothing_raised do
- tokens = CodeRay::Tokens.new
- end
- assert_kind_of Array, tokens
- end
-
- def test_adding_tokens
- tokens = CodeRay::Tokens.new
- assert_nothing_raised do
- tokens.text_token 'string', :type
- tokens.text_token '()', :operator
- end
- assert_equal tokens.size, 4
- assert_equal tokens.count, 2
- end
-
- def test_dump_undump
- tokens = CodeRay::Tokens.new
- assert_nothing_raised do
- tokens.text_token 'string', :type
- tokens.text_token '()', :operator
- end
- tokens2 = nil
- assert_nothing_raised do
- tokens2 = tokens.dump.undump
- end
- assert_equal tokens, tokens2
- end
-
-end \ No newline at end of file