comparison vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb @ 0:513646585e45

* Import Redmine trunk SVN rev 3859
author Chris Cannam
date Fri, 23 Jul 2010 15:52:44 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:513646585e45
1 ($:.unshift '../..'; require 'coderay') unless defined? CodeRay
2 module CodeRay
3 module Encoders
4
5 # = JSON Encoder
6 class JSON < Encoder
7
8 register_for :json
9 FILE_EXTENSION = 'json'
10
11 protected
12 def setup options
13 begin
14 require 'json'
15 rescue LoadError
16 require 'rubygems'
17 require 'json'
18 end
19 @out = []
20 end
21
22 def text_token text, kind
23 { :type => 'text', :text => text, :kind => kind }
24 end
25
26 def block_token action, kind
27 { :type => 'block', :action => action, :kind => kind }
28 end
29
30 def finish options
31 @out.to_json
32 end
33
34 end
35
36 end
37 end
38
39 if $0 == __FILE__
40 $VERBOSE = true
41 $: << File.join(File.dirname(__FILE__), '..')
42 eval DATA.read, nil, $0, __LINE__ + 4
43 end
44
45 __END__
46 require 'test/unit'
47 $:.delete '.'
48 require 'rubygems' if RUBY_VERSION < '1.9'
49
50 class JSONEncoderTest < Test::Unit::TestCase
51
52 def test_json_output
53 tokens = CodeRay.scan <<-RUBY, :ruby
54 puts "Hello world!"
55 RUBY
56 require 'json'
57 assert_equal [
58 {"type"=>"text", "text"=>"puts", "kind"=>"ident"},
59 {"type"=>"text", "text"=>" ", "kind"=>"space"},
60 {"type"=>"block", "action"=>"open", "kind"=>"string"},
61 {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
62 {"type"=>"text", "text"=>"Hello world!", "kind"=>"content"},
63 {"type"=>"text", "text"=>"\"", "kind"=>"delimiter"},
64 {"type"=>"block", "action"=>"close", "kind"=>"string"},
65 {"type"=>"text", "text"=>"\n", "kind"=>"space"}
66 ], JSON.load(tokens.json)
67 end
68
69 end