summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2010-06-29 06:42:51 +0000
committermurphy <murphy@rubychan.de>2010-06-29 06:42:51 +0000
commitdd29372ee9351d5417507f6133068f7be6758f93 (patch)
tree0ef9ad9469f810c9e038e96b08827e463cade482 /lib
parente63682824ef4110152381bdb9b831378fe7baa4c (diff)
downloadcoderay-dd29372ee9351d5417507f6133068f7be6758f93.tar.gz
Improved JSON encoder: Loading of json library in JRUBY, error messages, "early death" by loading it when the encoder is loaded.
Diffstat (limited to 'lib')
-rw-r--r--lib/coderay/encoders/json.rb49
1 files changed, 14 insertions, 35 deletions
diff --git a/lib/coderay/encoders/json.rb b/lib/coderay/encoders/json.rb
index bb09809..3215868 100644
--- a/lib/coderay/encoders/json.rb
+++ b/lib/coderay/encoders/json.rb
@@ -1,4 +1,3 @@
-($:.unshift '../..'; require 'coderay') unless defined? CodeRay
module CodeRay
module Encoders
@@ -18,17 +17,25 @@ module Encoders
# ]
class JSON < Encoder
+ begin
+ require 'json'
+ rescue LoadError
+ begin
+ require 'rubygems'
+ gem "json#{'-jruby' if defined? JRUBY_VERSION}"
+ 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
- begin
- require 'json'
- rescue LoadError
- require 'rubygems'
- require 'json'
- end
@out = []
end
@@ -60,31 +67,3 @@ module Encoders
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
- json = CodeRay.scan('puts "Hello world!"', :ruby).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"},
- ], JSON.load(json)
- end
-
-end \ No newline at end of file