summaryrefslogtreecommitdiffstats
path: root/vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb
diff options
context:
space:
mode:
authorJean-Philippe Lang <jp_lang@yahoo.fr>2010-03-16 20:29:12 +0000
committerJean-Philippe Lang <jp_lang@yahoo.fr>2010-03-16 20:29:12 +0000
commit0097770626893fa2bd67cb12c6d75db4a3dda317 (patch)
treeb5f1e83f363f49f99579562fe1bf69f94f3a40ef /vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb
parente6c8760ad77a6d4256f11e409734b0501450e588 (diff)
downloadredmine-0097770626893fa2bd67cb12c6d75db4a3dda317.tar.gz
redmine-0097770626893fa2bd67cb12c6d75db4a3dda317.zip
Upgrade CodeRay to 0.9.2 (#3359).
git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@3592 e93f8b46-1217-0410-a6f0-8f06a7374b81
Diffstat (limited to 'vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb')
-rw-r--r--vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb69
1 files changed, 69 insertions, 0 deletions
diff --git a/vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb b/vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb
new file mode 100644
index 000000000..7aa077c28
--- /dev/null
+++ b/vendor/plugins/coderay-0.9.2/lib/coderay/encoders/json.rb
@@ -0,0 +1,69 @@
+($:.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 \ No newline at end of file