diff options
author | murphy <murphy@rubychan.de> | 2009-12-30 07:06:56 +0000 |
---|---|---|
committer | murphy <murphy@rubychan.de> | 2009-12-30 07:06:56 +0000 |
commit | 75cd42c31a6b4bf3a148a8a1bb72009e5bcbda46 (patch) | |
tree | 668ba35a4540910546420edfecc0ed486e01f6ce /lib/coderay/encoders | |
parent | 470c12e7b2e25b10c67776ed85d67051b655fa06 (diff) | |
download | coderay-75cd42c31a6b4bf3a148a8a1bb72009e5bcbda46.tar.gz |
JSON encoder: New format to preserve all information and be more accessible for JavaScript programmers (each token is an object). Also a refactorization. Added tests.
Diffstat (limited to 'lib/coderay/encoders')
-rw-r--r-- | lib/coderay/encoders/json.rb | 49 |
1 files changed, 47 insertions, 2 deletions
diff --git a/lib/coderay/encoders/json.rb b/lib/coderay/encoders/json.rb index 6d789c6..072f334 100644 --- a/lib/coderay/encoders/json.rb +++ b/lib/coderay/encoders/json.rb @@ -1,3 +1,4 @@ +($:.unshift '../..'; require 'coderay') unless defined? CodeRay module CodeRay module Encoders @@ -8,12 +9,56 @@ module Encoders FILE_EXTENSION = 'json' protected - def compile tokens, options + def setup options require 'json' - @out = tokens.to_a.to_json + @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
\ No newline at end of file |