| Class | CodeRay::Encoders::Term |
| In: |
lib/coderay/encoders/term.rb
|
| Parent: | Encoder |
| TOKEN_COLORS | = | { :annotation => '35', :attribute_name => '33', :attribute_name_fat => '33', :attribute_value => '31', :attribute_value_fat => '31', :bin => '1;35', :char => {:self => '36', :delimiter => '34'}, :class => '1;35', :class_variable => '36', :color => '32', :comment => '37', :complex => '34', :constant => ['34', '4'], :decoration => '35', :definition => '1;32', :directive => ['32', '4'], :doc => '46', :doctype => '1;30', :doc_string => ['31', '4'], :entity => '33', :error => ['1;33', '41'], :exception => '1;31', :float => '1;35', :function => '1;34', :global_variable => '42', :hex => '1;36', :important => '1;31', :include => '33', :integer => '1;34', :interpreted => '1;35', :key => '35', :label => '1;4', :local_variable => '33', :oct => '1;35', :operator_name => '1;29', :pre_constant => '1;36', :pre_type => '1;30', :predefined => ['4', '1;34'], :preprocessor => '36', :pseudo_class => '34', :regexp => { :content => '31', :delimiter => '1;29', :modifier => '35', :function => '1;29' |
# File lib/coderay/encoders/term.rb, line 92
92: def setup(options)
93: @out = ''
94: @opened = [nil]
95: @subcolors = nil
96: end
# File lib/coderay/encoders/term.rb, line 102
102: def token text, type = :plain
103: case text
104:
105: when nil
106: # raise 'Token with nil as text was given: %p' % [[text, type]]
107:
108: when String
109:
110: if color = (@subcolors || TOKEN_COLORS)[type]
111: color = color[:self] || return if Hash === color
112:
113: @out << col(color) + text.gsub("\n", col(0) + "\n" + col(color)) + col(0)
114: @out << col(@subcolors[:self]) if @subcolors && @subcolors[:self]
115: else
116: @out << text
117: end
118:
119: # token groups, eg. strings
120: when :open
121: @opened[0] = type
122: if color = TOKEN_COLORS[type]
123: if Hash === color
124: @subcolors = color
125: @out << col(color[:self]) if color[:self]
126: else
127: @subcolors = {}
128: @out << col(color)
129: end
130: end
131: @opened << type
132: when :close
133: if @opened.empty?
134: # nothing to close
135: else
136: @out << col(0) if (@subcolors || {})[:self]
137: @subcolors = nil
138: @opened.pop
139: end
140:
141: # whole lines to be highlighted, eg. a added/modified/deleted lines in a diff
142: when :begin_line
143:
144: when :end_line
145:
146: else
147: raise 'unknown token kind: %p' % [text]
148: end
149: end