Sha256: 0c383b05bf021edb2c131269f167e9ed23103ba9bab68ed47625782fbd8c901a

Contents?: true

Size: 1.5 KB

Versions: 6985

Compression:

Stored size: 1.5 KB

Contents

module CodeRay
  
  # 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)
      else
        encoder.encode_tokens tokens, options
      end
    end
    
    # Tries to call encode;
    # delegates to tokens otherwise.
    def method_missing method, *args, &blk
      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
    
  end
  
end

Version data entries

6,985 entries across 6,972 versions & 75 rubygems

Version Path
qiita_org-0.1.28 gems/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
qiita_org-0.1.27 gems/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
qiita_org-0.1.26 gems/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
qiita_org-0.1.25 gems/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
passbase-1.1.0 vendor/bundle/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
passbase-1.0.3 vendor/bundle/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
passbase-1.0.2 vendor/bundle/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
passbase-1.0.1 vendor/bundle/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
passbase-1.0.0 vendor/bundle/ruby/2.7.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.3.0/gems/coderay-1.1.1/lib/coderay/tokens_proxy.rb
logstash-filter-csharp-0.2.1 vendor/bundle/jruby/2.5.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.3.0/gems/coderay-1.1.1/lib/coderay/tokens_proxy.rb
logstash-filter-csharp-0.2.0 vendor/bundle/jruby/2.5.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
metanorma-cli-1.3.4 gems/ruby/2.6.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
metanorma-cli-1.3.3.1 gems/ruby/2.6.0/gems/coderay-1.1.3/lib/coderay/tokens_proxy.rb
logstash-output-scalyr-0.1.5 vendor/bundle/jruby/2.5.0/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb
files.com-1.0.1 vendor/bundle/ruby/2.5.0/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb
logstash-output-scalyr-0.1.4 vendor/bundle/jruby/2.5.0/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb
logstash-output-scalyr-0.1.3 vendor/bundle/jruby/2.5.0/gems/coderay-1.1.2/lib/coderay/tokens_proxy.rb
coderay-1.1.3 lib/coderay/tokens_proxy.rb