Class CodeRay::Encoders::JSON
In: lib/coderay/encoders/json.rb
Parent: Encoder

A simple JSON Encoder.

Example:

 CodeRay.scan('puts "Hello world!"', :ruby).json

yields

 [
   {"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"},
 ]

Methods

Constants

FILE_EXTENSION = 'json'

Public Instance methods

[Source]

    # File lib/coderay/encoders/json.rb, line 64
64:     def begin_group kind
65:       append :type => 'block', :action => 'open', :kind => kind
66:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 72
72:     def begin_line kind
73:       append :type => 'block', :action => 'begin_line', :kind => kind
74:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 68
68:     def end_group kind
69:       append :type => 'block', :action => 'close', :kind => kind
70:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 76
76:     def end_line kind
77:       append :type => 'block', :action => 'end_line', :kind => kind
78:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 60
60:     def text_token text, kind
61:       append :type => 'text', :text => text, :kind => kind
62:     end

Protected Instance methods

[Source]

    # File lib/coderay/encoders/json.rb, line 49
49:     def append data
50:       if @first
51:         @first = false
52:       else
53:         @out << ','
54:       end
55:       
56:       @out << data.to_json
57:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 45
45:     def finish options
46:       @out << ']'
47:     end

[Source]

    # File lib/coderay/encoders/json.rb, line 38
38:     def setup options
39:       super
40:       
41:       @first = true
42:       @out << '['
43:     end

[Validate]