diff options
Diffstat (limited to 'lib/coderay/tokens_proxy.rb')
-rw-r--r-- | lib/coderay/tokens_proxy.rb | 29 |
1 files changed, 23 insertions, 6 deletions
diff --git a/lib/coderay/tokens_proxy.rb b/lib/coderay/tokens_proxy.rb index 2a20435..31ff39b 100644 --- a/lib/coderay/tokens_proxy.rb +++ b/lib/coderay/tokens_proxy.rb @@ -1,7 +1,23 @@ module CodeRay - class TokensProxy < Struct.new :input, :lang, :options, :block + # The result of a scan operation is a TokensProxy, but should act like Tokens. + # + # This proxy makes it possible to use the classic CodeRay.scan.encode API + # while still providing the benefits of direct streaming. + class TokensProxy + attr_accessor :input, :lang, :options, :block + + # Create a new TokensProxy with the arguments of CodeRay.scan. + def initialize input, lang, options = {}, block = nil + @input = input + @lang = lang + @options = options + @block = block + end + + # Call CodeRay.encode if +encoder+ is a Symbol; + # otherwise, convert the receiver to tokens and call encoder.encode_tokens. def encode encoder, options = {} if encoder.respond_to? :to_sym CodeRay.encode(input, lang, encoder, options) @@ -10,29 +26,30 @@ module CodeRay end end + # Tries to call encode; + # delegates to tokens otherwise. def method_missing method, *args, &blk - encode method, *args + encode method.to_sym, *args rescue PluginHost::PluginNotFound tokens.send(method, *args, &blk) end + # The (cached) result of the tokenized input; a Tokens instance. def tokens @tokens ||= scanner.tokenize(input) end + # A (cached) scanner instance to use for the scan task. def scanner @scanner ||= CodeRay.scanner(lang, options, &block) end + # Overwrite Struct#each. def each *args, &blk tokens.each(*args, &blk) self end - def count - tokens.count - end - end end |