CodeRay::Encoders::Terminal

Outputs code highlighted for a color terminal.

Note: This encoder is in beta. It currently doesn’t use the Styles.

Alias: term

Authors & License

By Rob Aldred (robaldred.co.uk)

Based on idea by Nathan Weizenbaum (nex-3.com)

MIT License (www.opensource.org/licenses/mit-license.php)

Constants

TOKEN_COLORS

Public Instance Methods

begin_group(kind) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 126
126:       def begin_group kind
127:         @opened << kind
128:         @out << open_token(kind)
129:       end
Also aliased as: begin_line
begin_line(kind) click to toggle source
Alias for: begin_group
end_group(kind) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 132
132:       def end_group kind
133:         if @opened.empty?
134:           # nothing to close
135:         else
136:           @opened.pop
137:           @out << ansi_clear
138:           @out << open_token(@opened.last)
139:         end
140:       end
end_line(kind) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 142
142:       def end_line kind
143:         if @opened.empty?
144:           # nothing to close
145:         else
146:           @opened.pop
147:           # whole lines to be highlighted,
148:           # eg. added/modified/deleted lines in a diff
149:           @out << "\t" * 100 + ansi_clear
150:           @out << open_token(@opened.last)
151:         end
152:       end
text_token(text, kind) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 106
106:       def text_token text, kind
107:         if color = (@subcolors || TOKEN_COLORS)[kind]
108:           if Hash === color
109:             if color[:self]
110:               color = color[:self]
111:             else
112:               @out << text
113:               return
114:             end
115:           end
116:           
117:           @out << ansi_colorize(color)
118:           @out << text.gsub("\n", ansi_clear + "\n" + ansi_colorize(color))
119:           @out << ansi_clear
120:           @out << ansi_colorize(@subcolors[:self]) if @subcolors && @subcolors[:self]
121:         else
122:           @out << text
123:         end
124:       end

Protected Instance Methods

setup(options) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 98
 98:       def setup(options)
 99:         super
100:         @opened = []
101:         @subcolors = nil
102:       end

Private Instance Methods

ansi_clear() click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 174
174:       def ansi_clear
175:         ansi_colorize(0)
176:       end
ansi_colorize(color) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 171
171:       def ansi_colorize(color)
172:         Array(color).map { |c| "\e[#{c}m" }.join
173:       end
open_token(kind) click to toggle source
     # File lib/coderay/encoders/terminal.rb, line 156
156:       def open_token kind
157:         if color = TOKEN_COLORS[kind]
158:           if Hash === color
159:             @subcolors = color
160:             ansi_colorize(color[:self]) if color[:self]
161:           else
162:             @subcolors = {}
163:             ansi_colorize(color)
164:           end
165:         else
166:           @subcolors = nil
167:           ''
168:         end
169:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.