summaryrefslogtreecommitdiff
path: root/lib/coderay/tokens_proxy.rb
blob: b333e57279393e38d9c3e9210548cae76ae59d2e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module CodeRay
  
  class TokensProxy < Struct.new :input, :lang, :options, :block
    
    def method_missing method, *args, &blk
      encode method, *args
    rescue PluginHost::PluginNotFound
      tokens.send(method, *args, &blk)
    end
    
    def tokens
      @tokens ||= scanner.tokenize(input)
    end
    
    def each *args, &blk
      tokens.each(*args, &blk)
    end
    
    def count
      tokens.count
    end
    
    def scanner
      @scanner ||= CodeRay.scanner(lang, options, &block)
    end
    
    def encode encoder, options = {}
      if encoder.respond_to? :to_sym
        CodeRay.encode(input, lang, encoder, options)
      else
        encoder.encode_tokens tokens, options
      end
    end
    
  end
  
end