diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2011-08-19 03:09:35 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2011-08-19 03:09:35 +0200 |
commit | 75bc5455af8c3c3381066aac3d5fff42264cac6f (patch) | |
tree | 589d2c912ddd94c517eb794bcdf3257f8348f3c0 /lib/coderay/encoders/json.rb | |
parent | fdd17b6a09efb275238a3baef275465d31452f2a (diff) | |
download | coderay-75bc5455af8c3c3381066aac3d5fff42264cac6f.tar.gz |
Major rewrite of encoders to support IO output; fixed some minor scanner bugs; cleanups; dropped NitroXHTML scanner; improved tests
Diffstat (limited to 'lib/coderay/encoders/json.rb')
-rw-r--r-- | lib/coderay/encoders/json.rb | 27 |
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/coderay/encoders/json.rb b/lib/coderay/encoders/json.rb index ccad554..0a95397 100644 --- a/lib/coderay/encoders/json.rb +++ b/lib/coderay/encoders/json.rb @@ -36,32 +36,45 @@ module Encoders protected def setup options - @out = [] + super + + @first = true + @out << '[' end def finish options - @out.to_json + @out << ']' + end + + def append data + if @first + @first = false + else + @out << ',' + end + + @out << data.to_json end public def text_token text, kind - @out << { :type => 'text', :text => text, :kind => kind } + append :type => 'text', :text => text, :kind => kind end def begin_group kind - @out << { :type => 'block', :action => 'open', :kind => kind } + append :type => 'block', :action => 'open', :kind => kind end def end_group kind - @out << { :type => 'block', :action => 'close', :kind => kind } + append :type => 'block', :action => 'close', :kind => kind end def begin_line kind - @out << { :type => 'block', :action => 'begin_line', :kind => kind } + append :type => 'block', :action => 'begin_line', :kind => kind end def end_line kind - @out << { :type => 'block', :action => 'end_line', :kind => kind } + append :type => 'block', :action => 'end_line', :kind => kind end end |