Class: CodeRay::Encoders::Debug

Inherits:
Encoder
  • Object
show all
Defined in:
lib/coderay/encoders/debug.rb

Overview

Debug Encoder

Fast encoder producing simple debug output.

It is readable and diff-able and is used for testing.

You cannot fully restore the tokens information from the output, because consecutive :space tokens are merged. Use Tokens#dump for caching purposes.

See also: Scanners::Debug

Constant Summary collapse

FILE_EXTENSION =
'raydebug'

Constants inherited from Encoder

Encoder::DEFAULT_OPTIONS

Instance Attribute Summary

Attributes inherited from Encoder

#options, #scanner

Attributes included from Plugin

#plugin_id

Instance Method Summary collapse

Methods inherited from Encoder

#<<, const_missing, #encode, #encode_tokens, file_extension, #file_extension, #token

Methods included from Plugin

#aliases, #plugin_host, #register_for, #title

Constructor Details

#initialize(options = {}) ⇒ Debug

Returns a new instance of Debug.



21
22
23
24
# File 'lib/coderay/encoders/debug.rb', line 21

def initialize options = {}
  super
  @opened = []
end

Instance Method Details

#begin_group(kind) ⇒ Object



36
37
38
39
# File 'lib/coderay/encoders/debug.rb', line 36

def begin_group kind
  @opened << kind
  @out << kind.to_s << '<'
end

#begin_line(kind) ⇒ Object



50
51
52
# File 'lib/coderay/encoders/debug.rb', line 50

def begin_line kind
  @out << kind.to_s << '['
end

#end_group(kind) ⇒ Object



41
42
43
44
45
46
47
48
# File 'lib/coderay/encoders/debug.rb', line 41

def end_group kind
  if @opened.last != kind
    puts @out
    raise "we are inside #{@opened.inspect}, not #{kind}"
  end
  @opened.pop
  @out << '>'
end

#end_line(kind) ⇒ Object



54
55
56
# File 'lib/coderay/encoders/debug.rb', line 54

def end_line kind
  @out << ']'
end

#text_token(text, kind) ⇒ Object



26
27
28
29
30
31
32
33
34
# File 'lib/coderay/encoders/debug.rb', line 26

def text_token text, kind
  if kind == :space
    @out << text
  else
    # TODO: Escape (
    text = text.gsub(/[)\\]/, '\\\\\0')  # escape ) and \
    @out << kind.to_s << '(' << text << ')'
  end
end