Sha256: b84105e4254fbdc369dadb8c4ec26745bc66718fa5224f81f43b745d17146017

Contents?: true

Size: 1.79 KB

Versions: 6940

Compression:

Stored size: 1.79 KB

Contents

module CodeRay
module Encoders
  
  # A simple JSON Encoder.
  # 
  # Example:
  #  CodeRay.scan('puts "Hello world!"', :ruby).json
  # yields
  #  [
  #    {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
  #    {"type"=>"text", "text"=>" ", "kind"=>"space"},
  #    {"type"=>"block", "action"=>"open", "kind"=>"string"},
  #    {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
  #    {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
  #    {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
  #    {"type"=>"block", "action"=>"close", "kind"=>"string"},
  #  ]
  class JSON < Encoder
    
    begin
      require 'json'
    rescue LoadError
      begin
        require 'rubygems' unless defined? Gem
        gem 'json'
        require 'json'
      rescue LoadError
        $stderr.puts "The JSON encoder needs the JSON library.\n" \
          "Please gem install json."
        raise
      end
    end
    
    register_for :json
    FILE_EXTENSION = 'json'
    
  protected
    def setup options
      super
      
      @first = true
      @out << '['
    end
    
    def finish options
      @out << ']'
    end
    
    def append data
      if @first
        @first = false
      else
        @out << ','
      end
      
      @out << data.to_json
    end
    
  public
    def text_token text, kind
      append :type => 'text', :text => text, :kind => kind
    end
    
    def begin_group kind
      append :type => 'block', :action => 'open', :kind => kind
    end
    
    def end_group kind
      append :type => 'block', :action => 'close', :kind => kind
    end
    
    def begin_line kind
      append :type => 'block', :action => 'begin_line', :kind => kind
    end
    
    def end_line kind
      append :type => 'block', :action => 'end_line', :kind => kind
    end
    
  end
  
end
end

Version data entries

6,940 entries across 6,928 versions & 73 rubygems

Version Path
lazy_record-0.1.4 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
tdiary-5.0.4 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
mdg-1.0.1 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
lazy_record-0.1.3 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
lazy_record-0.1.2 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
lazy_record-0.1.1 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
lazy_record-0.1.0 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-input-fifo-0.9.1 vendor/bundle/jruby/1.9/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-input-fifo-0.9.0 vendor/bundle/jruby/1.9/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-input-salesforce-3.0.0 vendor/jruby/1.9/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
abaci-0.3.0 vendor/bundle/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
ivanvc-logstash-input-s3-3.1.1.4 vendor/local/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
ivanvc-logstash-input-s3-3.1.1.3 vendor/local/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
ivanvc-logstash-input-s3-3.1.1.2 vendor/local/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
gameboard-3.1.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
gameboard-3.0.0 vendor/bundle/ruby/2.3.0/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-filter-delta-1.1.0 vendor/bundle/jruby/2.2.0/gems/coderay-1.1.1/lib/coderay/encoders/json.rb
logstash-filter-delta-1.0.1 vendor/bundle/jruby/2.2.0/gems/coderay-1.1.1/lib/coderay/encoders/json.rb