comparison vendor/gems/coderay-1.0.0/lib/coderay/tokens_proxy.rb @ 909:cbb26bc654de redmine-1.3

Update to Redmine 1.3-stable branch (Redmine SVN rev 8964)
author Chris Cannam
date Fri, 24 Feb 2012 19:09:32 +0000
parents
children
comparison
equal deleted inserted replaced
908:c6c2cbd0afee 909:cbb26bc654de
1 module CodeRay
2
3 # The result of a scan operation is a TokensProxy, but should act like Tokens.
4 #
5 # This proxy makes it possible to use the classic CodeRay.scan.encode API
6 # while still providing the benefits of direct streaming.
7 class TokensProxy
8
9 attr_accessor :input, :lang, :options, :block
10
11 # Create a new TokensProxy with the arguments of CodeRay.scan.
12 def initialize input, lang, options = {}, block = nil
13 @input = input
14 @lang = lang
15 @options = options
16 @block = block
17 end
18
19 # Call CodeRay.encode if +encoder+ is a Symbol;
20 # otherwise, convert the receiver to tokens and call encoder.encode_tokens.
21 def encode encoder, options = {}
22 if encoder.respond_to? :to_sym
23 CodeRay.encode(input, lang, encoder, options)
24 else
25 encoder.encode_tokens tokens, options
26 end
27 end
28
29 # Tries to call encode;
30 # delegates to tokens otherwise.
31 def method_missing method, *args, &blk
32 encode method.to_sym, *args
33 rescue PluginHost::PluginNotFound
34 tokens.send(method, *args, &blk)
35 end
36
37 # The (cached) result of the tokenized input; a Tokens instance.
38 def tokens
39 @tokens ||= scanner.tokenize(input)
40 end
41
42 # A (cached) scanner instance to use for the scan task.
43 def scanner
44 @scanner ||= CodeRay.scanner(lang, options, &block)
45 end
46
47 # Overwrite Struct#each.
48 def each *args, &blk
49 tokens.each(*args, &blk)
50 self
51 end
52
53 end
54
55 end