Class: CodeRay::Encoders::TokenKindFilter

Inherits:
Filter show all
Defined in:
lib/coderay/encoders/token_kind_filter.rb

Overview

A Filter that selects tokens based on their token kind.

Options

:exclude

One or many symbols (in an Array) which shall be excluded.

Default: []

:include

One or many symbols (in an array) which shall be included.

Default: :all, which means all tokens are included.

Exclusion wins over inclusion.

See also: CommentFilter

Direct Known Subclasses

CommentFilter, LinesOfCode

Constant Summary collapse

DEFAULT_OPTIONS =
{
  :exclude => [],
  :include => :all
}

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, #initialize, #token

Methods included from Plugin

#aliases, #plugin_host, #register_for, #title

Constructor Details

This class inherits a constructor from CodeRay::Encoders::Encoder

Instance Method Details

#begin_group(kind) ⇒ Object

Add the token group to the output stream if kind matches the conditions.

If it does not, all tokens inside the group are excluded from the stream, even if their kinds match.



66
67
68
69
70
71
72
73
74
# File 'lib/coderay/encoders/token_kind_filter.rb', line 66

def begin_group kind
  if @group_excluded
    @group_excluded += 1
  elsif include_group? kind
    super
  else
    @group_excluded = 1
  end
end

#begin_line(kind) ⇒ Object

See begin_group.



77
78
79
80
81
82
83
84
85
# File 'lib/coderay/encoders/token_kind_filter.rb', line 77

def begin_line kind
  if @group_excluded
    @group_excluded += 1
  elsif include_group? kind
    super
  else
    @group_excluded = 1
  end
end

#end_group(kind) ⇒ Object

Take care of re-enabling the delegation of tokens to the output stream if an exluded group has ended.



89
90
91
92
93
94
95
96
# File 'lib/coderay/encoders/token_kind_filter.rb', line 89

def end_group kind
  if @group_excluded
    @group_excluded -= 1
    @group_excluded = false if @group_excluded.zero?
  else
    super
  end
end

#end_line(kind) ⇒ Object

See end_group.



99
100
101
102
103
104
105
106
# File 'lib/coderay/encoders/token_kind_filter.rb', line 99

def end_line kind
  if @group_excluded
    @group_excluded -= 1
    @group_excluded = false if @group_excluded.zero?
  else
    super
  end
end

#text_token(text, kind) ⇒ Object

Add the token to the output stream if kind matches the conditions.



57
58
59
# File 'lib/coderay/encoders/token_kind_filter.rb', line 57

def text_token text, kind
  super if !@group_excluded && include_text_token?(text, kind)
end