Sha256: c504295c35696b15698a70a82452e0984e9820431f3c7a8d200522655878e031

Contents?: true

Size: 1.53 KB

Versions: 13

Compression:

Stored size: 1.53 KB

Contents

($:.unshift '../..'; require 'coderay') unless defined? CodeRay
module CodeRay
module Encoders
  
  # = JSON Encoder
  class JSON < Encoder
    
    register_for :json
    FILE_EXTENSION = 'json'
    
  protected
    def setup options
      begin
        require 'json'
      rescue LoadError
        require 'rubygems'
        require 'json'
      end
      @out = []
    end
    
    def text_token text, kind
      { :type => 'text', :text => text, :kind => kind }
    end
    
    def block_token action, kind
      { :type => 'block', :action => action, :kind => kind }
    end
    
    def finish options
      @out.to_json
    end
    
  end
  
end
end

if $0 == __FILE__
  $VERBOSE = true
  $: << File.join(File.dirname(__FILE__), '..')
  eval DATA.read, nil, $0, __LINE__ + 4
end

__END__
require 'test/unit'
$:.delete '.'
require 'rubygems' if RUBY_VERSION < '1.9'

class JSONEncoderTest < Test::Unit::TestCase
  
  def test_json_output
    tokens = CodeRay.scan <<-RUBY, :ruby
puts "Hello world!"
    RUBY
    require 'json'
    assert_equal [
      {"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"},
      {"type"=>"text", "text"=>"\n", "kind"=>"space"}
    ], JSON.load(tokens.json)
  end
  
end

Version data entries

13 entries across 13 versions & 2 rubygems

Version Path
coderay-0.9.8 ./lib/coderay/encoders/json.rb
coderay-0.9.8.pre ./lib/coderay/encoders/json.rb
coderay-0.9.7 ./lib/coderay/encoders/json.rb
coderay-0.9.7.pre ./lib/coderay/encoders/json.rb
coderay-0.9.6 ./lib/coderay/encoders/json.rb
coderay-0.9.6.pre2 ./lib/coderay/encoders/json.rb
coderay-0.9.6.pre ./lib/coderay/encoders/json.rb
coderay-0.9.5 ./lib/coderay/encoders/json.rb
coderay-0.9.4 ./lib/coderay/encoders/json.rb
coderay-0.9.3 ./lib/coderay/encoders/json.rb
coderay-0.9.2 ./lib/coderay/encoders/json.rb
coderay-0.9.1 ./lib/coderay/encoders/json.rb
coderay-beta-0.9.1 ./lib/coderay/encoders/json.rb